Custom textview headlight effect, which can control startup, stop, and speed (including source code)

Source: Internet
Author: User

The Running Horse lights in Android are not very effective and cannot control the speed, stop and start immediately, and are also affected by the focus. Because the project needs to use a controllable high running horse lamp effect, so I wrote a custom textview

Note: When the layout file references this view, paddingleft and paddingrigh must both be 0dp. To add these two attributes, you can modify them on your own.Code.

Android: ellipsize = "marquee" Android: singleline = "true" must be added

 

 Public   Class Marqueetext Extends Textview Implements  Runnable {  Private   Int Currentscrollx; // Current scroll position          Private   Boolean Isstop = False  ;  Private   Int  Textwidth;  Private   Boolean Ismeasure = False  ;  Public  Marqueetext (context ){  Super (Context );  //  Todo auto-generated constructor stub  }  Public  Marqueetext (context, attributeset attrs ){  Super  (Context, attrs );}  Public Marqueetext (context, attributeset attrs, Int  Defstyle ){  Super (Context, attrs, defstyle) ;}@ override  Protected   Void  Ondraw (canvas ){  //  Todo auto-generated method stub                  Super  . Ondraw (canvas );  If (! Ismeasure ){ //  You only need to retrieve the text width once.  Gettextwidth (); ismeasure = True ;}}  /**  * Get Text width  */          Private   Void  Gettextwidth () {paint = This  . Getpaint (); string Str = This  . Gettext (). tostring (); textwidth = ( Int  ) Paint. measuretext (STR) ;}@ override Public   Void  Run () {currentscrollx -= 2; //  Scroll speed Scrollto (currentscrollx, 0 );  If  (Isstop ){  Return  ;}  If (Getscrollx () <= -( This  . Getwidth () {scrollto (textwidth, 0 ); Currentscrollx = Textwidth;  //  Return;  } Postdelayed (  This , 5 );}  //  Start rolling          Public   Void  Startscroll () {isstop = False ;  This . Removecallbacks ( This  ); Post (  This  );}  //  Stop rolling          Public   Void  Stopscroll () {isstop = True  ;}  //  Scroll from scratch         Public   Void  Startfor0 () {currentscrollx = 0 ; Startscroll ();}} 

Layout file:

<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Xmlns: Tools = "Http://schemas.android.com/tools" Android: layout_width = "Fill_parent" Android: layout_height = "Fill_parent" Android: Orientation = "Vertical"> < Button Android: ID = "@ + ID/start" Android: layout_width = "Wrap_content" Android: layout_height = "Wrap_content" Android: onclick = "Start" Android: Text = "Start"/> < Button Android: ID = "@ + ID/stop" Android: layout_width = "Wrap_content" Android: layout_height = "Wrap_content" Android: onclick = "Stop" Android: Text = "Stop"/> <Button Android: ID = "@ + ID/startfor0" Android: layout_width = "Wrap_content" Android: layout_height = "Wrap_content" Android: onclick = "Startfor0" Android: Text = "Start from scratch"/> < Simtice. Demo. marqueetext. marqueetext Android: ID = "@ + ID/test" Android: layout_width = "Fill_parent" Android: layout_height = "Wrap_content" Android: Background = "#339320"Android: ellipsize = "Marquee" Android: singleline = "True" Android: Text = "This is the true text marquee effect. This is the true text marquee effect" Android: textcolor = "#000000" Android: textsize = "20dp"> </simtice. Demo. marqueetext. marqueetext> </linearlayout> Mainactivity 
 Public   Class Mainactivity Extends  Activity {  Private Marqueetext test; @ override  Public   Void  Oncreate (bundle savedinstancestate ){  Super  . Oncreate (savedinstancestate); setcontentview (R. layout. activity_main); Test = (Marqueetext) This  . Findviewbyid (R. Id. Test );}  Public   Void  Start (view v) {test. startscroll ();}  Public  Void  Stop (view v) {test. stopscroll ();}  Public   Void  Startfor0 (view v) {test. startfor0 ();}} 

 

Original article: http://www.eoeandroid.com/thread-247067-1-1.html

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.