Android 仿支付宝数字变换,滚动

    xiaoxiao2021-03-25  66

    看了一些别人写的,结合自己的优化,优化,50行代码。仅支持int类型,自己做记录,不喜勿喷谢谢。

    public class RunNumAnimation extends android.support.v7.widget.AppCompatTextView { private int duration; private int number; public RunNumAnimation(Context context) { super(context); } //构造函数必须写,否则报错。 public RunNumAnimation(Context context, AttributeSet attrs) { super(context, attrs); } private void setDuration(int duration) { this.duration = duration; } private void setNumber(int number) { this.number = number; } public void start(int num, int duration) { setDuration(duration); setNumber(num); runNum(); } private void runNum() { final ValueAnimator valueAnimator = ValueAnimator.ofInt(0, number);//从0开始,number是你要传的数据 valueAnimator.setDuration(duration); valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { setText(valueAnimator.getAnimatedValue().toString()); } }); valueAnimator.start(); } }

    private RunNumAnimation run_num

    run_num = (RunNumAnimation) findViewById(R.id.run_num);//注意类型不是TextView run_num.start(1024,1500);//对外提供start方法,1024是你要传的数据,1500是时长。

    转载请注明原文地址: https://ju.6miu.com/read-50146.html

    最新回复(0)