Android使滚动的数字更好看

共 3680字,需浏览 8分钟

 ·

2022-03-05 21:41

有的时候我们需要一个:



滚动的数字效果才能让用户更有提升感!

1、新建一个接口规定其方法

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);}

2、把数字格式化,并加入数字变换动画
public class RiseNumberTextView extends android.support.v7.widget.AppCompatTextView 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 { 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; }}

其中使用到了Utils工具,里面的format方法
 /**     * 格式化     */    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;    }

3、最后使用也非常简单,先withNumber后start
riseNumberTextView.withNumber(num).start();

到这里就结束啦。
浏览 150
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报