Have seen several digital animation processing articles, this is I found the simplest is also the best understanding, specifically to come out and share with you.
The core of the code is mainly to rewrite the TextView this control, do some of their own processing.
PackageCom.lance.widget;ImportJava.text.DecimalFormat;ImportJava.util.concurrent.ExecutorService;Importjava.util.concurrent.Executors;ImportAndroid.content.Context;ImportAndroid.os.Handler;ImportAndroid.os.Message;ImportAndroid.util.AttributeSet;ImportAndroid.util.Log;ImportAndroid.widget.TextView;/*** One, set the number of frames to jump, setframes (int frames), the default is 25 frames two, set the number format, SetFormat (String * pattern), specific to DecimalFormat class API three, need animation effect, Playnumber (double * number) method instead of SetText () method, decimals if more than 3 decimal places are rounded to retain 2 decimal places * *@authorLance **/ Public classRunningtextviewextendsTextView { Public DoubleContent//the last number shown Private intframes = 25;//total number of frames jumped, default 25 hops Private DoubleNownumber = 0.00;//the time displayed PrivateExecutorservice Thread_pool; PrivateHandler Handler; PrivateDecimalFormat Formater;//format time, retain two decimal places PublicRunningtextview (context context, AttributeSet attrs,intDefstyle) { Super(context, attrs, Defstyle); Init (); } PublicRunningtextview (Context context, AttributeSet attrs) {Super(context, attrs); Init (); } PublicRunningtextview (Context context) {Super(context); Init (); } Public intGetframes () {returnframes; } //set the number of frames Public voidSetframes (intframes) { This. frames =frames; } /*** Set number format, specific to DecimalFormat class API *@parampattern*/ Public voidSetFormat (String pattern) {Formater=NewDecimalFormat (pattern); } //Initialize Private voidinit () {Thread_pool= Executors.newfixedthreadpool (2);//Thread Pool of 2 threadsFormater =NewDecimalFormat ("00.00");//up to two decimal places, and not enough for two-bit integers with 0 placeholders. You can set DecimalFormat again by SetFormat to format numbersHandler =NewHandler () {@Override Public voidhandlemessage (Message msg) {Super. Handlemessage (msg); Runningtextview. This. SetText (Formater.format (Nownumber). toString ());//update the displayed numbersNownumber + = double.parsedouble (msg.obj.toString ());//jumping arg1 So many digital intervals//log.v ("Nownumber value Added after", Nownumber + ""); // if(Nownumber <content) {Message MSG2=Handler.obtainmessage (); Msg2.obj=Msg.obj; Handler.sendmessage (MSG2);//continue sending notifications change UI}Else{Runningtextview. This. SetText (Formater.format (content). toString ());//the last displayed number, the animation stops } } }; } /*** How to play digital animation * *@paramMoneynumber*/ Public voidPlaynumber (DoubleMoneynumber) { if(Moneynumber = = 0) {Runningtextview. This. SetText ("0.00"); return; } content= Moneynumber;//set the last number to displayNownumber = 0.00;//The default is 0 to start the animationThread_pool.execute (NewRunnable () {@Override Public voidrun () {Message msg=Handler.obtainmessage (); Doubletemp = content/frames; Msg.obj= Temp < 0.01? 0.01:temp;//if the interval of each frame is smaller than 1, it is set to 1//LOG.V ("Number of hops per frame:", "" + msg.obj.toString ());Handler.sendmessage (msg);//Send notifications Change UI } }); }}
The main principle is to use handler to carry on the continuous accumulation of numbers
PackageCom.lance.runningtextview;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.EditText;ImportAndroid.widget.Toast; Public classMainactivityextendsActivityImplementsOnclicklistener {PrivateButton MBTRMB; PrivateButton mbtdollar; PrivateButton Mbturo; PrivateCom.lance.widget.RunningTextView Mrunningtextview; PrivateEditText Medit; PrivateButton Mbtplay; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Bindviews (); //set the number format, specific to the DecimalFormat class APIMrunningtextview.setformat ("00.00"); Mbtplay.setonclicklistener ( This); Mbtrmb.setonclicklistener ( This); Mbtdollar.setonclicklistener ( This); Mbturo.setonclicklistener ( This); } Private voidbindviews () {MBTRMB=(Button) Findviewbyid (R.ID.BTRMB); Mbtdollar=(Button) Findviewbyid (r.id.btdollar); Mbturo=(Button) Findviewbyid (R.id.bturo); Mrunningtextview=(Com.lance.widget.RunningTextView) Findviewbyid (R.id.runningtextview); Medit=(EditText) Findviewbyid (R.id.edit); Mbtplay=(Button) Findviewbyid (R.id.btplay); } @Override Public voidOnClick (View v) {Switch(V.getid ()) { CaseR.id.btplay://play a digital animationString temp =Medit.gettext (). toString (); LOG.V ("Temp:", temp); if(!temp.equals ("")){ DoubleNumber =double.parsedouble (temp); Mrunningtextview.playnumber (number); } Break; CaseR.id.btrmb:toast.maketext ( This, "Use the Symbol¥", Toast.length_short). Show (); Mrunningtextview.setformat ("¥00.00"); Break; CaseR.id.bturo:toast.maketext ( This, "Use the symbol€", Toast.length_short). Show (); Mrunningtextview.setformat ("€00.00"); Break; CaseR.id.btdollar:toast.maketext ( This, "Use the symbol $", Toast.length_short). Show (); Mrunningtextview.setformat ("$00.00"); Break; } }}
Android fake Balance treasure digital animation effect