On some apps, when a new activity is entered, a key figure (such as the amount) is displayed with an animated number that is constantly growing. At first, the idea was to use Thead+handler, given an animation's total duration and the duration of the refresh interval, based on the formula (the number starts at 0, each increment is an array divided by the number of animation executions, and the number of animation executions equals the total animation time divided by the refresh interval);
The TextView string is reset every once in a while to the added value until the end of the animation shows the final result.
Actually have a certain understanding of the Android animation should know valueanimator this class, we can use it to very good implementation of the desired effect, and do not need our own to blunt control how much more refresh display. We encapsulate a custom view to achieve our needs, based on the Valueanimator property approach and the actual needs.
Let's look at what I've achieved.
Next the key code:
Custom view, in fact, is to inherit a TextView, code implementation is very simple, the code also has comments
/*** Created by Dingchao on 2018/3/27.*/ Public classDctextviewrunnumberextendsTextView {/*** Delay*/ Private Final intDELAY = 20; /*** The number of decimal digits reserved By default is 2*/ Private Final intDecimals_count = 2; Private Final intStart_run = 101; Private Final intStop_run = 102; /*** Number of runs*/ Private Final intRun_count = 40; Private floatSpeed ; Private floatStartnum; Private floatEndnum; /*** Number of decimal digits reserved*/ Private intDecimals =Decimals_count; /*** Number of runs per run*/ Private intRuncount =Run_count; /*** Animation delay*/ Private intDelaymillis =DELAY; Private Booleanisaniming; PrivateHandler Mhandler =NewHandler () { Public voidhandlemessage (Message msg) {if(Msg.what = =Start_run) { if(speed==0){ if(endnum!=0) { speed=getspeed (); Startnum=Speed ; }Else{ return ; }} isaniming= !running (); if(isaniming) {sendemptymessagedelayed (Start_run, Delaymillis); }Else{ speed= 0; Startnum= 0; } } }; }; PublicDctextviewrunnumber (Context context) {Super(context); } PublicDctextviewrunnumber (Context context, AttributeSet attrs) {Super(context, attrs); } PublicDctextviewrunnumber (context context, AttributeSet attrs,intdefstyleattr) { Super(context, attrs, defstyleattr); } /*** Start the digital bounce animation *@returnwhether the animation is finished*/ Private Booleanrunning () {SetText (Withdec (string.valueof (startnum) )+ ""); Startnum+=Speed ; if(Startnum >=endnum) {SetText (Withdec (string.valueof (endnum))+ ""); return true; } return false; } /*** Calculation speed *@return */ Private floatgetspeed () {floatSpeedfloat = Withdec (string.valueof (endnum/runcount)). Floatvalue (); returnspeedfloat; } /*** Determine if non-negative *@return */ Private Booleanisnumber (String num) {if("". Equals (num) | | num==NULL) return false; Pattern Pattern= Pattern.compile ("^\\d+$|\\d+\\.\\d+$"); Matcher Matcher=pattern.matcher (num); returnMatcher.find (); } /*** Rounding up reserved decimals *@paramnum *@return */ PrivateBigDecimal withdec (String num) {return NewBigDecimal (num). Setscale (decimals, bigdecimal.round_half_up); } /*** Set the displayed number *@paramNum*/ Public voidsetshownum (String num) {setshownum (num,decimals_count); } /*** Set the displayed number *@paramnum *@paramdecimals the number of decimal digits to keep*/ Public voidSetshownum (String num,intdecimals) { if(!isnumber (num)) { return; } setText (num); Setdecimals (decimals); } /*** Start Running*/ Public voidStartrun () {if(isaniming) {return ; } if(Isnumber (GetText (). toString ())) {Endnum=Withdec (GetText (). toString ()). Floatvalue (); Mhandler.sendemptymessage (Start_run); } } Public intgetdecimals () {returndecimals; } /*** set reserved decimal places 0: Decimals not reserved *@paramDecimals*/ Public voidSetdecimals (intdecimals) { if(decimals>=0){ This. Decimals =decimals; } setText (Withdec (GetText (). toString ())+""); } Public intGetruncount () {returnRuncount; } /*** Set the number of animations to run *@paramRuncount*/ Public voidSetruncount (intRuncount) { if(runcount<=0){ return ; } This. Runcount =Runcount; } Public intGetdelaymillis () {returnDelaymillis; } /*** Set Animation delay *@paramDelaymillis*/ Public voidSetdelaymillis (intDelaymillis) { This. Delaymillis =Delaymillis; }
Next see how to use, in Mainactivity.java
Public classMainactivityextendsappcompatactivity {PrivateDctextviewrunnumber Numberrunview; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Numberrunview=(Dctextviewrunnumber) Findviewbyid (R.id.numberrunview); Numberrunview.setshownum ("711", 0);//terminating number, decimal point, here is 0 so no decimal pointNumberrunview.setruncount (50);//the number of times the animation was executed, 50 executions completed//numberrunview.setshownum ("221.918899");Numberrunview.startrun ();// } /** * @paramView*/ Public voidRunclick (view view) {Numberrunview.startrun (); }}
Activity_main.xml put it on, all right?
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context=". Mainactivity "> <Cn.up.com.textviewrun.DcTextViewRunNumberAndroid:id= "@+id/numberrunview"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:layout_margintop= "20DP"Android:text= "123"android:textsize= "30SP" /> <ButtonAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:onclick= "Runclick"Android:text= "Run" /></LinearLayout>
The main is the three files, the code is all posted out, the effect is also possible. Do not repeat the development of the wheel, and some changes can be directly used.
Android TextView Digital Growth animation effect