欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 仿余额宝数字跳动动画效果完整代码

程序员文章站 2022-05-26 22:45:38
一:想都不用想的,有图有真相,看着爽了,在看下面源码 二:实例源码分析 ①:首先定义接口 package com.demo.tools.view; /...

一:想都不用想的,有图有真相,看着爽了,在看下面源码

Android 仿余额宝数字跳动动画效果完整代码

二:实例源码分析

①:首先定义接口

package com.demo.tools.view; 
/** 
 * 数字动画自定义 
 * 
 * @author zengtao 2015年7月17日 上午11:48:27 
 * 
 */ 
public interface risenumberbase { 
  public void start(); 
  public risenumbertextview withnumber(float number); 
  public risenumbertextview withnumber(float number, boolean flag); 
  public risenumbertextview withnumber(int number); 
  public risenumbertextview setduration(long duration); 
  public void setonend(risenumbertextview.endlistener callback); 
} 

②:自定义控件

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; 
/** 
 * 数字动画自定义 
 * 
 * @author zengtao 2015年7月17日 上午11:48:03 
 * 
 */ 
public class risenumbertextview extends textview implements risenumberbase { 
  private static final int stopped = 0; 
  private static final int running = 1; 
  private int mplayingstate = stopped; 
  private float number; 
  private float 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, 99999999, 999999999, integer.max_value }; 
  public 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() { 
      @override 
      public 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.00").format(double.parsedouble(number + ""))); 
          } 
        } else { 
          settext(utils.format("##0.00").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() { 
      @override 
      public 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; 
  } 
  @override 
  protected void onfinishinflate() { 
    super.onfinishinflate(); 
  } 
  @override 
  public void start() { 
    if (!isrunning()) { 
      mplayingstate = running; 
      if (numbertype == 1) 
        runint(); 
      else 
        runfloat(); 
    } 
  } 
  @override 
  public risenumbertextview withnumber(float number, boolean flag) { 
    this.number = number; 
    this.flags = flag; 
    numbertype = 2; 
    fromnumber = 0; 
    return this; 
  } 
  @override 
  public risenumbertextview withnumber(float number) { 
    system.out.println(number); 
    this.number = number; 
    numbertype = 2; 
    fromnumber = 0; 
    return this; 
  } 
  @override 
  public risenumbertextview withnumber(int number) { 
    this.number = number; 
    numbertype = 1; 
    fromnumber = 0; 
    return this; 
  } 
  @override 
  public risenumbertextview setduration(long duration) { 
    this.duration = duration; 
    return this; 
  } 
  @override 
  public void setonend(endlistener callback) { 
    mendlistener = callback; 
  } 
} 

③工具类的编写

package com.demo.tools; 
import java.math.roundingmode; 
import java.text.decimalformat; 
/** 
 * 配置文件 
 * @author zengtao 2015年7月17日 上午11:47:44 
 * 
 */ 
public class utils { 
  /** 
   * 格式化 
   */ 
  private static decimalformat dfs = null; 
  public static decimalformat format(string pattern) { 
    if (dfs == null) { 
      dfs = new decimalformat(); 
    } 
    dfs.setroundingmode(roundingmode.floor); 
    dfs.applypattern(pattern); 
    return dfs; 
  } 
} 

三:主界面调用

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; 
/** 
 * 数字动画 
 * 
 * @author zengtao 2015年7月17日 上午11:48:54 
 * 
 */ 
public class mainactivity extends activity { 
  private risenumbertextview number1, number2, number3, number4, number5; 
  private button start; 
  @override 
  protected 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() { 
    @override 
    public 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(); 
  } 
} 

四:总结

demo下载地址:http://xiazai.jb51.net/201711/yuanma/demo.rar

以上所述是小编给大家介绍的android 仿余额宝数字跳动动画效果完整代码,希望对大家有所帮助