设为首页 加入收藏

TOP

Android之增长的数字动画(一)
2015-02-02 14:32:45 来源: 作者: 【 】 浏览:27
Tags:Android 增长 数字 动画

本案例是使用开源组件RiseNumberTextView来实现类似手机支付宝中增长的数字效果,首先我们来看一下开源组件中的两个类分别如下:


package com.bear.risenumbertest.lib;


public interface RiseNumberBase {
? ? public void start();
? ? public RiseNumberTextView withNumber(float number);
? ? public RiseNumberTextView withNumber(int number);
? ? public RiseNumberTextView setDuration(long duration);
? ? public void setOnEnd(RiseNumberTextView.EndListener callback);
}


另一个类:


package com.bear.risenumbertest.lib;


import com.nineoldandroids.animation.ValueAnimator;


import java.text.DecimalFormat;


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=1500;
? ? /**
? ? * 1.int 2.float
? ? */
? ? private int numberType=2;


? ? private DecimalFormat? fnum;


? ? 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) {


? ? ? ? ? ? ? ? setText(fnum.format(Float.parseFloat(valueAnimator.getAnimatedValue().toString())));
? ? ? ? ? ? ? ? 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();
? ? ? ? fnum=? new DecimalFormat("##0.00");
? ? }


? ? @Override
? ? public void start() {


? ? ? ? if (!isRunning()) {
? ? ? ? ? ? mPlayingState = RUNNING;
? ? ? ? ? ? if (numberType==1)
? ? ? ? ? ? ? ? runInt();
? ? ? ? ? ? else
? ? ? ? ? ? ? ? runFloat();
? ? ? ? }
? ? }



? ? @Override
? ? public RiseNumberTextView withNumber(float number) {


? ? ? ? this.number=number;
? ? ? ? numberType=2;
? ? ? ? if (number>1000){
? ? ? ? ? ? fromNumber=number-(float)Math.pow(10,si

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Object-C中的initialize 与 单例 下一篇Object-C与Java通用的AES加密解密

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: