Layout file:
<linearlayout android:layout_width= "fill_parent" android:layout_height= "60dip" > < Com.example.guanggao.AutoScrollTextView android:id= "@+id/tv_notice" android:layout_width= "Fill_parent" android:layout_height= "fill_parent" android:textsize= "25sp"/> </LinearLayout>
Code:
Autoscrolltextview:
public class Autoscrolltextview extends TextView {public final static String TAG = AutoScrollTextView.class.getSimpleName ();p rivate float textLength = 0f;//text length private float viewwidth = 0f;private float step = 0f;//Text's horizontal axis private float y = 0f ///The ordinate of the text private float temp_view_plus_text_length = 0.0f;//used to calculate the temporary change?? private float temp_view_plus_two_text_length = 0.0f;//used for calculation of temporary change?? public Boolean isstarting = false;//?? Start rolling?? Private Paint paint = null;//drawing style private String text = "";//text content Public Autoscrolltextview (context context) {Super (Contex t); 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 () {//Setonclicklistener (this);} public void init (WindowManager windowmanager) {paint = Getpaint (); text = GetText (). toString (); textLength = Paint.measuretext (text); viewwidth = GetWidth (); if (Viewwidth = = 0) {if (WindowManager! = NULL) {Display display = Windowmanager.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);}}; 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) {if (!isstarting) {return;} Canvas.drawtext (text, Temp_view_plus_text_length-step, y, paint), step + = 2;//text scrolling speed?? if (Step > temp_view_plus_two_text_length) step = Textlength;invalidate ();} @Override//public void OnClick (View v) {//if (isstarting)//Stopscroll ();//else//Startscroll ();////}}
Mainactivity:
private void Getnotice () {//TODO auto-generated method Stubtv_notice.settext (text); Tv_notice.init (Getwindowmanager ()); Tv_notice.startscroll ();}
Android Marquee Effect