Demo下载链接
项目中的小需求,完成类似于支付宝余额的数字滚动效果,找了网上的一个小demo,再加上郭婶的关于属性动画的文章,整理一部分代码分享给有需要的人.
下面贴出封装的TextView代码片段,主要是利用属性动画来更新TextView完成数字滚动更新.
package shidong.com.numberscrolldemo.view;
import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;
import java.text.DecimalFormat;
import shidong.com.numberscrolldemo.R;
/**
* NumberScrollTextView,用属性动画完成数字平滑滚动
* created by shidong
*/
public class NumberScrollTextView extends TextView implements IRiseNumber {
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 DecimalFormat fnum;
private EndListener mEndListener = null;
final static int[] sizeTable = {
9, 99, 999, 9999, 99999, 999999, 9999999,
99999999, 999999