Online TextView do the marquee, most of them are to use the focus, and the number of words to go beyond the width of the rolling area to achieve scrolling, use is very inconvenient.
Here is a truly controllable scrolling
(1) No focus required
(2) Any number of words
(3) Scroll from the right side of the scroll area, disappear on the left, and then come out from the right.
On the Code
1. layout file
<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Tools:context= "${relativepackage}.${activityclass}" > <ButtonAndroid:id= "@+id/btn1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Change scrolling content 1"/> <ButtonAndroid:id= "@+id/btn2"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_torightof= "@id/btn1"Android:text= "Change scrolling content 2"/> <ButtonAndroid:id= "@+id/btn3"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_torightof= "@id/btn2"Android:text= "Exit program"/> <Com.using.margindemo.MarqueeTextViewAndroid:id= "@+id/tvscroll"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:textsize= "40DP" > </Com.using.margindemo.MarqueeTextView></Relativelayout>
2. Scrolling controls
PackageCom.using.margindemo;ImportAndroid.content.Context;ImportAndroid.graphics.Canvas;ImportAndroid.os.Handler;ImportAndroid.os.Message;ImportAndroid.util.AttributeSet;ImportAndroid.widget.TextView; Public classMarqueetextviewextendsTextView {/**whether to stop scrolling*/ Private BooleanMstopmarquee; PrivateString MText;//text content Private floatMcoordinatex = 1280;//Current scroll position Private floatMtextwidth;//Text Width Private intMscrollwidth = 1280;//Scrolling area width Private intSpeed = 1;//scrolling Speed Public floatGetCurrentPosition () {returnMcoordinatex; } Public voidSetCurrentPosition (floatMcoordinatex) { This. Mcoordinatex =Mcoordinatex; } Public intgetscrollwidth () {returnMscrollwidth; } Public voidSetscrollwidth (intmscrollwidth) { This. Mscrollwidth =Mscrollwidth; } Public intgetspeed () {returnSpeed ; } Public voidSetspeed (intSpeed ) { This. Speed =Speed ; } PublicMarqueetextview (Context context, AttributeSet attrs) {Super(context, attrs); } Public voidSetText (String text) { This. MText =text; Mtextwidth=getpaint (). Measuretext (MText); //mtextwidth = A; if(Mhandler.hasmessages (0)) Mhandler.removemessages (0); Mhandler.sendemptymessagedelayed (0, 10); } @Overrideprotected voidOnattachedtowindow () {Mstopmarquee=false; if(!IsEmpty (MText)) mhandler.sendemptymessagedelayed (0, 2000); Super. Onattachedtowindow (); } Public Static BooleanisEmpty (String str) {returnstr = =NULL|| Str.length () = = 0; } @Overrideprotected voidOndetachedfromwindow () {Mstopmarquee=true; if(Mhandler.hasmessages (0)) Mhandler.removemessages (0); Super. Ondetachedfromwindow (); } @Overrideprotected voidOnDraw (canvas canvas) {Super. OnDraw (canvas); if(!IsEmpty (MText)) Canvas.drawtext (MText, Mcoordinatex,150, Getpaint ()); } PrivateHandler Mhandler =NewHandler () {@Override Public voidhandlemessage (Message msg) {Switch(msg.what) { Case0: if(Mcoordinatex < (-mtextwidth)) {//The text scrolls out, from the right side of the scrolling area .Mcoordinatex =Mscrollwidth; Invalidate (); if(!Mstopmarquee) {sendemptymessagedelayed (0, 500); } } Else{Mcoordinatex-=Speed ; Invalidate (); if(!Mstopmarquee) {sendemptymessagedelayed (0, 30); } } Break; } Super. Handlemessage (msg); } };}
3.
PackageCom.using.margindemo;ImportAndroid.app.ActionBar;Importandroid.app.Activity;Importandroid.content.res.Configuration;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.view.ViewGroup.MarginLayoutParams;ImportAndroid.view.Window;ImportAndroid.view.WindowManager;ImportAndroid.widget.Button;ImportAndroid.widget.ImageView;Importandroid.widget.RelativeLayout; Public classMainactivityextendsActivity {PrivateImageView image; PrivateMarqueetextview Marqueetextview; PrivateButton button1; PrivateButton button2; PrivateButton Button3; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); This. Requestwindowfeature (Window.feature_no_title);//Hide title barSetcontentview (R.layout.activity_main); Marqueetextview=(Marqueetextview) Findviewbyid (R.id.tvscroll); Marginlayoutparams margin1=NewMarginlayoutparams (Marqueetextview.getlayoutparams ()); Margin1.setmargins (100, 200, 0, 0);//Set Scroll area position: 400 pixels on the left margin, 10 pixels from the top marginRelativelayout.layoutparams layoutParams1 =NewRelativelayout.layoutparams (margin1); Layoutparams1.height= 240;//Setting the height of the scroll areaLayoutparams1.width = 1000;//Set Scroll area widthMarqueetextview.setlayoutparams (LAYOUTPARAMS1); Marqueetextview.setscrollwidth (1000); Marqueetextview.setcurrentposition (100 + 1000);//set scrolling information from the right side of the scrolling areaMarqueetextview.setspeed (2); Marqueetextview.settext ("This is the real effect of the light!" "); Button1=(Button) Findviewbyid (R.ID.BTN1); Button2=(Button) Findviewbyid (R.ID.BTN2); Button3=(Button) Findviewbyid (R.ID.BTN3); Button1.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {Marqueetextview.settext ("This is the real effect of the light!" "); } }); Button2.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {Marqueetextview.settext ("Warm reminder: After registration or payment, please go to the rest area to wait, we will serve you as soon as possible, please pay attention to the screen call information, thank you!" "); } }); Button3.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {finish (); } }); }}
Demo download
Android Development: TextView truly controllable, horizontal scrolling without focus-this is the real marquee