Custom TextView Marquee

Source: Internet
Author: User

This article mainly introduces the TextView the controllable marquee effect realization.

Android comes with the TextView add a few properties can realize the marquee effect, presumably so

        Android:ellipsize= "marquee"         android:focusable= "true"         android:focusableintouchmode= " True    

A marquee effect is achieved when the TextView gets focus.

This article is to implement a method that does not have the focus and can control the start and end of the marquee.

1, the main use of void Android.view.View.scrollTo (int x, int y) method in the background constantly scroll to achieve text movement effect.

Call Scrollto (currentscrollx,0) in the Run method in runnable to move the text, increase the text position currentscrollx+=2, and then postdelayed (this, 5) Enter the Run method again to call Scrollto (currentscrollx,0).

1 currentscrollx + = 2; 2             ScrollTo (currentscrollx, 0); 3             Postdelayed (this, 5);

Plus get text width stop control and appear from the right of the text box after the following:

1  Public voidrun () {2         if(!ismeasure) {//text width just need to get it once.3 gettextwidth (); 4Ismeasure =true;5         }6LOG.I ("Don't eat breakfast", "Run GetWidth:" + This. GetWidth () + "TextWidth:" +textWidth);7 8         if( This. GetWidth () <textWidth) {9Currentscrollx + = 2;TenScrollTo (CURRENTSCROLLX, 0); One             if(isstop) { ALOG.I ("Don't eat Breakfast", "isstop"); -                 return; -             } the             if(Getscrollx () >=textWidth) { -LOG.I ("Don't eat Breakfast", "scrollTo"); -CURRENTSCROLLX =- This. getwidth (); -ScrollTo (CURRENTSCROLLX, 0); +                 //return; -             } +Postdelayed ( This, 5); A         } at}

where This.getwidth () < textwidth is used to determine whether the text width is greater than the width of the text box. Get Text Width code:

1 Private void gettextwidth () {2          This . Getpaint (); 3          This . GetText (). toString (); 4         TextWidth = (int) paint.measuretext (str); 5     }

Note: Get the text width after measure, so be in the Run method.

Here's how to control the movement:

1 //Start scrolling2      Public voidStartscroll () {3 invalidate ();4LOG.I ("Don't eat Breakfast", "getwidth:" + This. GetWidth () + "TextWidth:" +textWidth);5Isstop =false;6          This. Removecallbacks ( This);7LOG.I ("Don't eat Breakfast", "Startscroll");8Post This);9     }Ten  One     //Stop scrolling A      Public voidStopscroll () { -Isstop =true; -ScrollTo (0, 0); theCURRENTSCROLLX =-2; -     } -  -     //Scroll from the beginning +      Public voidstartFor0 () { -CURRENTSCROLLX =-2; + Startscroll (); A}

Small Demo Link: http://pan.baidu.com/s/1o61F1z0

Custom TextView Marquee

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.