Recently in the development of an application, need to use the text of the marquee effect, the diagram of the easy, on-line search, but long time can not find, and later wrote a, very simple, the code is as follows:
ImportAndroid.content.Context;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Paint;ImportAndroid.os.Parcel;Importandroid.os.Parcelable;ImportAndroid.util.AttributeSet;ImportAndroid.view.Display;ImportAndroid.view.View;ImportAndroid.view.WindowManager;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.TextView;/*** * TODO single line text marquee control * *@authorTianlu *@version1.0 * Create at:2010-2-16 PM 09:35:03*/ Public classAutoscrolltextviewextendsTextViewImplementsOnclicklistener { Public Final StaticString TAG = Autoscrolltextview.class. Getsimplename (); Private floatTextLength = 0f;//Text Length Private floatViewwidth =0f; Private floatStep = 0f;//the horizontal axis of text Private floaty = 0f;//Ordinate of text Private floatTemp_view_plus_text_length = 0.0f;//temporary variable for calculation Private floatTemp_view_plus_two_text_length = 0.0f;//temporary variable for calculation Public BooleanIsstarting =false;//whether to start scrolling PrivatePaint paint =NULL;//Drawing Style PrivateString text = "";//text content PublicAutoscrolltextview (Context context) {Super(context); Initview (); } PublicAutoscrolltextview (Context context, AttributeSet attrs) {Super(context, attrs); Initview (); } PublicAutoscrolltextview (context context, AttributeSet attrs,intDefstyle) { Super(context, attrs, Defstyle); Initview (); } /*** Initialize the control*/ Private voidInitview () {Setonclicklistener ( This); } /*** Text initialization, after each change of text content or text effects, etc. need to be reinitialized*/ Public voidInit (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 (); } @Override Publicparcelable onsaveinstancestate () {parcelable superstate=Super. Onsaveinstancestate (); savedstate SS=Newsavedstate (superstate); Ss.step=step; Ss.isstarting=isstarting; returnSS; } @Override Public voidonrestoreinstancestate (parcelable state) {if(! (stateinstanceofsavedstate)) { Super. Onrestoreinstancestate (state); return; } savedstate SS=(savedstate) state; Super. Onrestoreinstancestate (Ss.getsuperstate ()); Step=Ss.step; Isstarting=ss.isstarting; } Public Static classSavedstateextendsBasesavedstate { Public BooleanIsstarting =false; Public floatStep = 0.0f; Savedstate (parcelable superstate) {Super(superstate); } @Override Public voidWritetoparcel (Parcel out,intflags) { Super. Writetoparcel (out, flags); Out.writebooleanarray (New Boolean[]{isstarting}]; Out.writefloat (step); } Public Static FinalParcelable.creator<savedstate>CREATOR=NewParcelable.creator<savedstate>() { PublicSavedstate[] NewArray (intsize) { return NewSavedstate[size]; } @Override Publicsavedstate Createfromparcel (Parcel in) {return Newsavedstate (in); } }; Privatesavedstate (Parcel in) {Super(in); Boolean[] B =NULL; In.readbooleanarray (b); if(b! =NULL&& b.length > 0) isstarting= B[0]; Step=in.readfloat (); } } /*** Start scrolling*/ Public voidStartscroll () {isstarting=true; Invalidate (); } /*** Stop scrolling*/ Public voidStopscroll () {isstarting=false; Invalidate (); } @Override Public voidOnDraw (canvas canvas) {canvas.drawtext (text, Temp_view_plus_text_length-step, y, paint); if(!isstarting) { return; } Step+ = 0.5; if(Step >temp_view_plus_two_text_length) Step=textLength; Invalidate (); } @Override Public voidOnClick (View v) {if(isstarting) stopscroll (); ElseStartscroll (); }}
The realization of the width of the judgment, text automatic scrolling and start and stop scrolling and other functions.
The configuration in the UI XML is as follows:
<Cn.tigertian.ui.AutoScrollTextViewAndroid:id= "@+id/textviewnotic"Android:layout_height= "30px"Android:layout_width= "Fill_parent"Android:text= "@string/test_notice_1"Android:textcolor= "#000"Android:inputtype= "text"Android:background= "#EEE"android:textsize= "20px"></Cn.tigertian.ui.AutoScrollTextView>
Here's how to use this activity:
// start the announcement scroll bar Autoscrolltextview = (Autoscrolltextview) Findviewbyid (r.id.textviewnotice); Autoscrolltextview.init (Getwindowmanager ()); Autoscrolltextview.startscroll ();
If you want to change the text content of the marquee or text effect, then after the call to the SetText method, you need to call the init (WindowManager WindowManager) method, re-initialization and calculation of related parameters.
Http://www.blogjava.net/TiGERTiAN/archive/2010/02/13/312840.html
Android Text marquee control (text Auto scroll control)