Android-two ways to achieve merry effect

Source: Internet
Author: User

The first method (very common, very simple to set the properties of TextView in an XML layout file):

<textview android:id="@+id/tv_text"android:layout_width="match_parent"android:layout_height="wrap_content"android:ellipsize="Marquee"android:focusable="true"android:focusableintouchmode="true"android:marqueerepeatlimit="Marquee_forever"android:singleline="true"android:textsize="28SP"android:text="The road long its repair far XI, I will go up and down and Quest"android:textcolor="#00ff00"android:scrollhorizontally="true"/>

Important Code:

Set as a marquee display

Android:ellipsize= "Marquee"

Get focus
Android:focusable= "true"

You can get focus by TOUCTH.
Android:focusableintouchmode= "true"

Set Number of repetitions
android:marqueerepeatlimit= "Marquee_forever"

Single-line display text
Android:singleline= "true"

2. The second method, since most of the merry text will start on the right side of the phone screen, requires a custom control to implement the

Java code:

 PackageCom.example.cameratestdemo;ImportAndroid.content.Context;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Paint;ImportAndroid.os.Handler;ImportAndroid.util.AttributeSet;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.widget.TextView;/*** Implementation of custom control loop Merry * *@authorCyf inherited from TextView*/ Public classUtilextendsTextViewImplementsRunnable {Private Static FinalString TAG = "Marqueetextview"; //set the number of times the marquee repeats    Private intCircletimes = 3; //How many times have the records been repeated?    Private inthascircled = 0; Private intCurrentscrollpos = 0; //the time required to go through the lantern (in seconds)    Private intCirclespeed = 10; //the width of the text    Private intTextWidth = 0; Private Booleanismeasured =false; //handler mechanism    PrivateHandler Handler; Private BooleanFlag =false; //Construction Method     PublicUtil (Context context, AttributeSet attrs) {Super(context, attrs); //TODO auto-generated Constructor stub         This. Removecallbacks ( This); Post ( This); }    /*** Brush Tool*/@Overrideprotected voidOnDraw (canvas canvas) {Super. OnDraw (canvas); if(!ismeasured)            {gettextwidth (); Ismeasured=true; }} @Override Public voidSetvisibility (intvisibility) {        //Initialize member variables at two-time entryFlag =false; Ismeasured=false;  This. hascircled = 0; Super. setvisibility (visibility); } @Override Public voidrun () {//Start scroll positionCurrentscrollpos + = 1; ScrollTo (Currentscrollpos,0); //log.i (TAG, "pos" +currentscrollpos); //Judging scrolling once        if(Currentscrollpos >=textWidth) {            //appears from the right side of the screenCurrentscrollpos =- This. getwidth (); //The number of times a record is set for scrolling is the same as scrolling, and the control can hide the            if(Hascircled >= This. Circletimes) {                 This. setvisibility (View.gone); Flag=true; } hascircled+ = 1; }        if(!flag) {            //Scrolling time intervalPostdelayed ( This, Circlespeed); }    }    /*** Get text display length*/    Private voidGettextwidth () {Paint Paint= This. Getpaint (); String Str= This. GetText (). toString ();        LOG.I (TAG, str); if(str = =NULL) {TextWidth= 0; } textWidth= (int) Paint.measuretext (str); }    /*** Set the number of scrolling times, the number of times after the setting is not visible *@paramCircletimes*/     Public voidSetcircletimes (intcircletimes) {         This. Circletimes =Circletimes; }     Public voidSetspeed (intSpeed ) {         This. Circlespeed =Speed ; }     Public voidstartscrollshow () {if( This. getvisibility () = =View.gone) This. setvisibility (view.visible);  This. Removecallbacks ( This); Post ( This); }    Private voidStopscroll () {handler.removecallbacks ( This); }}

In the layout file, you use a custom control:

<com. Example.cameratestdemo.Util Android:id= "@+id/tv_text"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:ellipsize= "Marquee"android:focusable= "true"Android:focusableintouchmode= "true"Android:marqueerepeatlimit= "Marquee_forever"android:scrollhorizontally= "true"Android:singleline= "true"Android:text= "The road is long its repair far, I will go up and down and quest"Android:textcolor= "#00ff00"android:textsize= "28SP" >    </Com.example.cameratestdemo.Util>

SOURCE Download:

Http://yunpan.cn/c3kL7ILLL7tCt Access Password bd75

Welcome to give advice, hope can bring you help, thank you

Android-two ways to achieve merry effect

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.