Custom Autoscrolltextview inherit to TextView, set the x-axis offset of the text in OnDraw () plus 1 and refresh to achieve the effect of moving text from right to left.
public class Autoscrolltextview extends TextView implements Onclicklistener {private String Tag = "Autoscrolltextview";/* * Text length */private float textLength = 0f;/** screen or space width */private float viewwidth = 0f;/** Text horizontal axis */private float step = 0f;/* * Text ordinate */private float y = 0f;/** temporary variable used for calculation */private float temp_view_plus_text_length = 0.0f;/** temporary variable for calculation */private FL Oat Temp_view_plus_two_text_length = 0.0f;/** whether to start scrolling */public boolean isstarting = false;/** drawing style */private paint paint = null;/** text content */private String text = "";p ublic Autoscrolltextview (context context) {super (context); Initview ();} Public Autoscrolltextview (context context, AttributeSet Attrs) {Super (context, attrs); Initview ();} Public Autoscrolltextview (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle); Initview ( );} private void Initview () {paint = Getpaint (); Setonclicklistener (this);} public void init (WindowManager windowmanager) {l.i (Tag, "init paint = = = =" +paint); text = GetText (). toString ();p AInt.setcolor (Color.rgb (251, 163,)); textLength = Paint.measuretext (text); L.E ("LG", "text = = =" +text); viewwidth = GetWidth (); if (viewwidth = = 0) {if (WindowManager! = NULL) {Display display = Windo Wmanager.getdefaultdisplay (); viewwidth = Display.getwidth ();}} Step = Textlength;temp_view_plus_text_length = viewwidth + textlength;temp_view_plus_two_text_length = viewWidth + TextLength * 2;y = gettextsize () + Getpaddingtop ();} @Overridepublic parcelable onsaveinstancestate () {parcelable superstate = super.onsaveinstancestate (); savedstate ss = new savedstate (superstate); ss.step = step;ss.isstarting = Isstarting;return ss;} @Overridepublic void Onrestoreinstancestate (parcelable state) {if (!) ( State instanceof savedstate) {super.onrestoreinstancestate (state); return;} savedstate ss = (savedstate) state;super.onrestoreinstancestate (Ss.getsuperstate ()); step = Ss.step;isstarting = ss.isstarting;} public static class Savedstate extends Basesavedstate {public Boolean isstarting = false;public float step = 0.0f; Savedstate (parcelable superstate) {super (superstate);} @Overridepublic void Writetoparcel (Parcel out, int. flags) {Super.writetoparcel (out, flags); Out.writebooleanarray (new Boolean[] {isstarting}); Out.writefloat (step);} public static final parcelable.creator<savedstate> Creator = new parcelable.creator<savedstate> () {public Savedstate[] NewArray (int size) {return new savedstate[size];} @Overridepublic savedstate Createfromparcel (Parcel in) {return new savedstate (in);}}; @SuppressWarnings ("unused") private savedstate (Parcel in) {super (in); boolean[] B = Null;in.readbooleanarray (b); if (b! = Null && b.length > 0) isstarting = B[0];step = In.readfloat ();}} public void Startscroll () {isstarting = True;invalidate ();} public void Stopscroll () {isstarting = False;invalidate ();} @Overridepublic void OnDraw (canvas canvas) {canvas.drawtext (text, Temp_view_plus_text_length-step, y, paint); isstarting) {return;} Step + = 1;//0.5 is text scrolling speed. if (Step > Temp_view_plus_two_teXt_length) Step = Textlength;invalidate ();} @Overridepublic void OnClick (View arg0) {//TODO auto-generated method stubif (isstarting) stopscroll (); Elsestartscroll ( );}}
Used in XML:
<com.demo.view.autoscrolltextview android:id= "@+id/auto_textview" android:layout_width= "Match_ Parent " android:layout_height=" wrap_content " android:gravity=" center_horizontal " android:padding=" 5DP "/>
Use in Java:
Private Autoscrolltextview AUTO_TV;AUTO_TV = (autoscrolltextview) Findviewbyid (R.id.auto_textview); Auto_tv.setText ( msg); Auto_tv.init (Getwindowmanager ()); Auto_tv.startscroll ();
Android text self-scrolling textview