Android Imitation Balance Treasure Digital animation implementation

Source: Internet
Author: User
Tags float number

Introduction

Long time No blog, recently released the company's products, and finally have a bit of leisure, take advantage of this free time, today to write a digital animation effect, may be in the later time, perhaps we will use to, it is worth the collection, directly to the past with the good.


A: Do not want to think, there is a picture of the truth, look at the cool, in the following source




Second: Example source code Analysis①: First define the interface
Package com.demo.tools.view;/** * Digital Animation customization *  * @author Zengtao July 17, 2015 Morning 11:48:27  * */public interface Risenumb erbase {public void start ();p ublic risenumbertextview withnumber (float number);p ublic Risenumbertextview Withnumber ( Float number, Boolean flag);p ublic risenumbertextview withnumber (int number);p ublic Risenumbertextview setduration ( Long duration);p ublic void Setonend (Risenumbertextview.endlistener callback);}


②: Custom Controls
Package Com.demo.tools.view;import Android.animation.valueanimator;import Android.content.context;import Android.util.attributeset;import Android.widget.textview;import com.demo.tools.utils;/** * Digital Animation Customization * * @author Zengtao July 17, 2015 Morning 11:48:03 * */public class Risenumbertextview extends TextView implements Risenumberbase {private static FINA l int STOPPED = 0;private static final int RUNNING = 1;private int mplayingstate = stopped;private float number;private fl Oat fromnumber;private Long Duration = 1000;/** * 1.int 2.float */private int numbertype = 2;private Boolean flags = TRUE; Private Endlistener Mendlistener = null;final static int[] sizetable = {9, 99, 999, 9999, 99999, 999999, 9999999, 9999999 9, 999999999, integer.max_value};p ublic Risenumbertextview (context context) {super (context);} Public Risenumbertextview (context context, AttributeSet attr) {Super (context, attr);} Public Risenumbertextview (context context, AttributeSet attr, int defstyle) {Super (context, attr, Defstyle);}public interface Endlistener {public void Onendfinish ();} public boolean isrunning () {return (mplayingstate = = RUNNING);} private void Runfloat () {Valueanimator valueanimator = valueanimator.offloat (fromnumber, number); Valueanimator.setduration (duration); Valueanimator.addupdatelistener (new Valueanimator.animatorupdatelistener () { @Overridepublic void Onanimationupdate (Valueanimator valueanimator) {if (flags) {SetText (Utils.format (", # #0.00"). Format (double.parsedouble (Valueanimator.getanimatedvalue (). toString ())) + ""); if (Valueanimator.getanimatedvalue ( ). ToString (). Equalsignorecase (number + "")) {SetText (Utils.format (", # #0. xx"). Format (double.parsedouble (number + "") ));}} else {SetText (Utils.format ("# #0. xx"). Format (double.parsedouble (Valueanimator.getanimatedvalue (). toString ())) + "" if (Valueanimator.getanimatedvalue (). toString (). Equalsignorecase (number + "")) {SetText (Utils.format ("# #0.00"). Format (double.parsedouble (number + ")));}} if (Valueanimator.getanimatedfraction () >= 1) {MPlaYingstate = Stopped;if (Mendlistener! = null) Mendlistener.onendfinish ();}}); Valueanimator.start ();} private void Runint () {Valueanimator valueanimator = valueanimator.ofint ((int) fromnumber, (int) number); Valueanimator.setduration (duration); Valueanimator.addupdatelistener (new Valueanimator.animatorupdatelistener () { @Overridepublic void Onanimationupdate (Valueanimator valueanimator) {SetText (Valueanimator.getanimatedvalue (). ToString ()); if (Valueanimator.getanimatedfraction () >= 1) {mplayingstate = Stopped;if (Mendlistener! = null) Mendlistener.onendfinish ();}}); Valueanimator.start ();} static int sizeofint (int x) {for (int i = 0;; i++) if (x <= sizetable[i]) return i + 1;} @Overrideprotected void Onfinishinflate () {super.onfinishinflate ();} @Overridepublic void Start () {if (!isrunning ()) {mplayingstate = running;if (Numbertype = = 1) runint (); Elserunfloat ();}} @Overridepublic Risenumbertextview Withnumber (float number, Boolean flag) {This.number = Number;this.flags = Flag; Numbertype = 2;frOmnumber = 0;return This;} @Overridepublic Risenumbertextview Withnumber (float number) {System.out.println (number); this.number = number; Numbertype = 2;fromnumber = 0;return this;} @Overridepublic risenumbertextview withnumber (int number) {This.number = Number;numbertype = 1;fromnumber = 0;return this ;} @Overridepublic Risenumbertextview setduration (long duration) {this.duration = Duration;return this;} @Overridepublic void Setonend (Endlistener callback) {Mendlistener = callback;}}


③ the authoring of tool classes
Package Com.demo.tools;import Java.math.roundingmode;import java.text.decimalformat;/** * profile * @author Zengtao July 17, 2015 Morning 11:47:44  * */public class Utils {/** * formatted */private static DecimalFormat Dfs = null;public static Decima Lformat format (String pattern) {if (Dfs = = null) {DFS = new DecimalFormat ();} Dfs.setroundingmode (Roundingmode.floor);d fs.applypattern (pattern); return DFS;}}


Three: main interface call
Package Com.demo.ui;import Android.app.activity;import Android.os.bundle;import android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import com.demo.tools.view.risenumbertextview;/** * Digital Animation * * @author Zengtao July 17, 2015 Morning 11:48:54 * */public class Mainactivity extends Activity {private RISENUMBERTEXTV Iew Number1, Number2, Number3, Number4, number5;private Button start, @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.main_activity); InitView ();} private void Initview () {number1 = (Risenumbertextview) Findviewbyid (r.id.number1); number2 = (Risenumbertextview) Findviewbyid (r.id.number2); Number3 = (Risenumbertextview) Findviewbyid (r.id.number3); number4 = (Risenumbertextview) Findviewbyid (r.id.number4); number5 = (Risenumbertextview) Findviewbyid (R.ID.NUMBER5); start = (Button) Findviewbyid ( R.id.start); Start.setonclicklistener (listener);} Onclicklistener listener = new Onclicklistener () {@overridepublic void OnClick (View v) {if (v = = start) {InitData ();}}}; private void InitData () {number1.withnumber (12.3f). Start (); Number2.withnumber (16.3f). Start (); Number3.withnumber ( 10.3f). Start (); Number4.withnumber (18.3f). Start (); Number5.withnumber (17.3f). Start ();}


Four: summarythe above is completed, a digital animation of the custom, looks very simple, so xxx, rinsed, directly can, if you like, who have the need, you can directly take to use.
demo:http://download.csdn.net/detail/u011546655/8908663

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Imitation Balance Treasure Digital animation implementation

Related Article

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.