The Code is as follows:
Autoscrolltextview. Java
Package com.cn. lhq; <br/> Import android. content. context; <br/> Import android. graphics. canvas; <br/> Import android. graphics. paint; <br/> Import android. OS. parcel; <br/> Import android. OS. parcelable; <br/> Import android. util. attributeset; <br/> Import android. view. display; <br/> Import android. view. view; <br/> Import android. view. windowmanager; <br/> Import android. view. view. onclicklistener; <br/> import Droid. widget. textview; <br/>/** <br/> * todo single-line text marquee Control <br/> */<br/> public class autoscrolltextview extends textview implements onclicklistener {<br/>/ /public final static string tag = autoscrolltextview. class. getsimplename (); <br/> private float textlength = 0f; // text length <br/> private float viewwidth = 0f; <br/> private float step = 0f; // text abscissa <br/> private float y = 0f; // text ordinate <br/> private float Temp_view_plus_text_length = 0.0f; // temporary variable used for calculation <br/> private float temp_view_plus_two_text_length = 0.0f; // temporary variable used for calculation <br/> Public Boolean isstarting = false; // whether to Start scrolling <br/> private paint = NULL; // drawing style <br/> private string text = ""; // text content <br/> Public autoscrolltextview (context) {<br/> super (context); <br/> initview (); <br/>}< br/> Public autoscrolltextview (context, attributeset Attrs) {<br/> super (context, attrs); <br/> initview (); <br/>}< br/> Public autoscrolltextview (context, attributeset attrs, int defstyle) {<br/> super (context, attrs, defstyle); <br/> initview (); <br/>}< br/>/** <br/> * initialization Control <br/> */<br/> private void initview () {<br/> setonclicklistener (this); <br/>}< br/>/** <br/> * initialize the text, every time you change the text content or text effect, you need to reinitialize it <br/> */<br/> Public void Init (windowman Ager windowmanager) {<br/> paint = getpaint (); <br/> text = gettext (). tostring (); <br/> textlength = paint. measuretext (text); <br/> viewwidth = getwidth (); <br/> If (viewwidth = 0) {<br/> If (windowmanager! = NULL) {<br/> display = windowmanager. getdefadisplay display (); <br/> viewwidth = display. getwidth (); <br/>}< br/> step = textlength; <br/> temp_view_plus_text_length = viewwidth + textlength; <br/> temp_view_plus_two_text_length = viewwidth + textlength * 2; <br/> Y = gettextsize () + getpaddingtop (); <br/>}< br/> @ override <br/> Public parcelable onsaveinstancestate () {<br/> parcelable superst Ate = super. onsaveinstancestate (); <br/> savedstate Ss = new savedstate (superstate); <br/> SS. step = step; <br/> SS. isstarting = isstarting; <br/> return SS; <br/>}< br/> @ override <br/> Public void onrestoreinstancestate (parcelable state) {<br/> If (! (State instanceof savedstate) {<br/> super. onrestoreinstancestate (State); <br/> return; <br/>}< br/> savedstate Ss = (savedstate) State; <br/> super. onrestoreinstancestate (ss. getsuperstate (); <br/> step = ss. step; <br/> isstarting = ss. isstarting; <br/>}< br/> Public static class savedstate extends basesavedstate {<br/> Public Boolean isstarting = false; <br/> public float step = 0.0f; <br/> savedstate (P Arcelable superstate) {<br/> super (superstate); <br/>}< br/> @ override <br/> Public void writetoparcel (parcel out, int flags) {<br/> super. writetoparcel (Out, flags); <br/> out. writebooleanarray (new Boolean [] {isstarting}); <br/> out. writefloat (STEP); <br/>}< br/> Public static final parcelable. creator <savedstate> creator = new parcelable. creator <savedstate> () {<br/> Public savedstate [] newarray (INT s Ize) {<br/> return New savedstate [size]; <br/>}< br/> @ override <br/> Public savedstate createfromparcel (parcel in) {<br/> return New savedstate (in); <br/>}< br/>}; <br/> private savedstate (parcel in) {<br/> super (in); <br/> Boolean [] B = NULL; <br/> in. readbooleanarray (B); <br/> If (B! = NULL & B. length> 0) <br/> isstarting = B [0]; <br/> step = in. readfloat (); <br/>}< br/>/** <br/> * start rolling <br/> */<br/> Public void startscroll () {<br/> isstarting = true; <br/> invalidate (); <br/>}< br/>/** <br/> * stop rolling <br/> */<br/> Public void stopscroll () {<br/> isstarting = false; <br/> invalidate (); <br/>}< br/> @ override <br/> Public void ondraw (canvas) {<br/> canvas. drawtext (text, Temp_view_plus_text_length-step, Y, paint); <br/> If (! Isstarting) {<br/> return; <br/>}< br/> step + = 0.5; <br/> If (Step> temp_view_plus_two_text_length) <br/> step = textlength; <br/> invalidate (); <br/>}< br/> @ override <br/> Public void onclick (view V) {<br/> If (isstarting) <br/> stopscroll (); <br/> else <br/> startscroll (); <br/>}< br/>}
The function of width determination, automatic text scrolling, start and stop scrolling, and so on.
The configuration in Ui XML is as follows:
<Com.cn. lhq. autoscrolltextview <br/> Android: Id = "@ + ID/textviewnotice" <br/> Android: layout_height = "30px" <br/> Android: layout_width = "fill_parent" <br/> Android: text = "abcdefghijklmn" <br/> Android: textcolor = "#000" <br/> Android: inputtype = "text" <br/> Android: Background = "# Eee" <br/> Android: textsize = "20px"/>
The usage in the activity is as follows:
// Start the announcement scroll bar <br/> autoscrolltextview = (autoscrolltextview) findviewbyid (R. id. textviewnotice); <br/> autoscrolltextview. init (getwindowmanager (); <br/> autoscrolltextview. startscroll ();
If you want to change the text content or text effect of the marquee, after calling the settext method, you need to call the init (windowmanager) method to re-Initialize and calculate related parameters.