数字自动增长的TextView,可以避免float精度问题和长度




package com.bsi.myapplication;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @author QianMang
 * @Date 2018/6/7.
 * @Email [email protected]
 */
public class RiseNumberTextView extends android.support.v7.widget.AppCompatTextView {
    private static final int STOPPED = 0;

    private static final int RUNNING = 1;

    private int mPlayingState = STOPPED;

    private BigDecimal decimal; //接收的数据,可以避免float精度丢失或超过long类型数据

    private long duration = 1000; //动画执行时间,默认1000ms

    private EndListener mEndListener = null;

    private Format format = Format.FLOAT_FORMAT; //文本格式化类型,默认保留两位小数

    private RoundingMode mode = RoundingMode.FLOOR;

    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 run() {
        ValueAnimator valueAnimator = ValueAnimator.ofObject(new DecimalEvaluator(), new BigDecimal("0.00"), decimal);
        valueAnimator.setDuration(duration);

        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                BigDecimal bigDecimal = (BigDecimal) valueAnimator.getAnimatedValue();

                setText(Utils.format(format.getFormatMode()).format(bigDecimal));
                if (valueAnimator.getAnimatedFraction() >= 1) {
                    mPlayingState = STOPPED;
                    if (mEndListener != null)
                        mEndListener.onEndFinish();
                }
            }
        });
        valueAnimator.start();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
    }

    public void start() {

        if (!isRunning()) {
            mPlayingState = RUNNING;
            run();
        }
    }

    //设置最终数值
    public RiseNumberTextView withNumber(String strNumber) {
        try{
            decimal = new BigDecimal(strNumber);
        }catch (Exception e){
            decimal = new BigDecimal("0");
        }
        return this;
    }

    public RiseNumberTextView setDuration(long duration) {
        this.duration = duration;
        return this;
    }

    public void setOnEnd(EndListener callback) {
        mEndListener = callback;
    }

    //自定义估值器
    private class DecimalEvaluator implements TypeEvaluator {

        @Override
        public BigDecimal evaluate(float fraction, BigDecimal startValue, BigDecimal endValue) {

            return startValue.add(endValue.subtract(startValue)).multiply(new BigDecimal(fraction));
        }
    }

    public RiseNumberTextView setFormatMode(Format formatMode){
        this.format = formatMode;
        return this;
    }

    public static enum Format {
        INT_FORMAT("###,###"), FLOAT_FORMAT("###,##0.00");
        private String formatMode;
        private Format(String formatMode) {
            this.formatMode = formatMode;
        }

        public String getFormatMode() {
            return formatMode;
        }

    }
}

工具类

package com.bsi.myapplication;
import java.math.RoundingMode;
import java.text.DecimalFormat;
/**
 * @author QianMang
 * @Date 2018/6/10.
 * @Email [email protected]
 */
public class Utils {
    private static DecimalFormat dfs = new DecimalFormat();

    public static DecimalFormat format(String pattern) {
        dfs.setRoundingMode(RoundingMode.FLOOR);
        dfs.applyPattern(pattern);
        return dfs;
    }
}
 riseNumberTextView.withNumber("123456789012312345.12").setFormatMode(RiseNumberTextView.Format.FLOAT_FORMAT).setDuration(1000).start();

效果图:
数字自动增长的TextView,可以避免float精度问题和长度_第1张图片

你可能感兴趣的:(android)