NumberRunningTextView.java 6.2 KB
Newer Older
C
chaychan 已提交
1 2
package com.chaychan.viewlib;

C
chaychan 已提交
3
import android.animation.ValueAnimator;
C
chaychan 已提交
4 5 6 7 8 9 10 11
import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.TextView;

import com.chaychan.viewlib.utils.StringUtils;

C
chaychan 已提交
12
import java.math.BigDecimal;
C
chaychan 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25
import java.text.DecimalFormat;

/**
 * 数字滚动的textView
 */
public class NumberRunningTextView extends TextView {

    private static final int MONEY_TYPE = 0;
    private static final int NUM_TYPE = 1;

    private int textType;//内容的类型,默认是金钱类型
    private boolean useCommaFormat;//是否使用每三位数字一个逗号的格式,让数字显得比较好看,默认使用
    private boolean runWhenChange;//是否当内容有改变才使用动画,默认是
C
chaychan 已提交
26 27 28
    private int duration;//动画的周期,默认为800ms
    private int minNum;//显示数字最少要达到这个数字才滚动 默认为1
    private float minMoney;//显示金额最少要达到这个数字才滚动 默认为0.3
C
chaychan 已提交
29 30 31 32

    private DecimalFormat formatter = new DecimalFormat("0.00");// 格式化金额,保留两位小数
    private String preStr;

C
chaychan 已提交
33

C
chaychan 已提交
34 35 36 37 38 39 40 41 42 43 44 45
    public NumberRunningTextView(Context context) {
        this(context, null);
    }

    public NumberRunningTextView(Context context, AttributeSet attrs) {
        this(context, attrs, android.R.attr.textViewStyle);
    }

    public NumberRunningTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.NumberRunningTextView);
C
chaychan 已提交
46
        duration = ta.getInt(R.styleable.NumberRunningTextView_duration, 1000);
C
chaychan 已提交
47 48 49
        textType = ta.getInt(R.styleable.NumberRunningTextView_textType, MONEY_TYPE);
        useCommaFormat = ta.getBoolean(R.styleable.NumberRunningTextView_useCommaFormat, true);
        runWhenChange = ta.getBoolean(R.styleable.NumberRunningTextView_runWhenChange,true);
C
chaychan 已提交
50 51
        minNum = ta.getInt(R.styleable.NumberRunningTextView_minNum, 3);
        minMoney = ta.getFloat(R.styleable.NumberRunningTextView_minMoney,0.1f);
52 53

        ta.recycle();
C
chaychan 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    }


    /**
     * 设置需要滚动的金钱(必须为正数)或整数(必须为正数)的字符串
     *
     * @param str
     */
    public void setContent(String str) {
        //如果是当内容改变的时候才执行滚动动画,判断内容是否有变化
        if (runWhenChange){
            if (TextUtils.isEmpty(preStr)){
                //如果上一次的str为空
                preStr = str;
                useAnimByType(str);
                return;
            }

            //如果上一次的str不为空,判断两次内容是否一致
            if (preStr.equals(str)){
                //如果两次内容一致,则不做处理
                return;
            }

            preStr = str;//如果两次内容不一致,记录最新的str
        }

        useAnimByType(str);
    }

    private void useAnimByType(String str) {
        if (textType == MONEY_TYPE) {
            playMoneyAnim(str);
C
chaychan 已提交
87
        } else if (textType == NUM_TYPE){
C
chaychan 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100
            playNumAnim(str);
        }
    }


    /**
     * 播放金钱数字动画的方法
     *
     * @param moneyStr
     */
    public void playMoneyAnim(String moneyStr) {
        String money = moneyStr.replace(",", "").replace("-", "");//如果传入的数字已经是使用逗号格式化过的,或者含有符号,去除逗号和负号
        try {
C
chaychan 已提交
101 102
            BigDecimal bigDecimal = new BigDecimal(money);
            float finalFloat = bigDecimal.floatValue();
C
chaychan 已提交
103
            if (finalFloat < minMoney) {
C
chaychan 已提交
104
                //如果传入的为0,则直接使用setText()
C
chaychan 已提交
105
                setText(moneyStr);
C
chaychan 已提交
106 107
                return;
            }
C
chaychan 已提交
108
            ValueAnimator floatAnimator =  ValueAnimator.ofObject(new BigDecimalEvaluator(),new BigDecimal(0), bigDecimal);
C
chaychan 已提交
109 110
            floatAnimator.setDuration(duration);
            floatAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
C
chaychan 已提交
111
                @Override
C
chaychan 已提交
112
                public void onAnimationUpdate(ValueAnimator animation) {
C
chaychan 已提交
113 114
                    BigDecimal currentNum = (BigDecimal) animation.getAnimatedValue();
                    String str = formatter.format(Double.parseDouble(currentNum.toString()));//格式化成两位小数
C
chaychan 已提交
115 116 117 118 119 120 121 122
                    // 更新显示的内容
                    if (useCommaFormat) {
                        //使用每三位数字一个逗号的格式
                        String formatStr = StringUtils.addComma(str);//三位一个逗号格式的字符串
                        setText(formatStr);
                    } else {
                        setText(str);
                    }
C
chaychan 已提交
123 124
                }
            });
C
chaychan 已提交
125
            floatAnimator.start();
C
chaychan 已提交
126 127
        } catch (NumberFormatException e) {
            e.printStackTrace();
C
chaychan 已提交
128
            this.setText(moneyStr);//如果转换Double失败则直接用setText
C
chaychan 已提交
129 130 131 132 133 134 135 136 137 138 139
        }
    }

    /**
     * 播放数字动画的方法
     *
     * @param numStr
     */
    public void playNumAnim(String numStr) {
        String num = numStr.replace(",", "").replace("-", "");//如果传入的数字已经是使用逗号格式化过的,或者含有符号,去除逗号和负号
        try {
C
chaychan 已提交
140 141
            int finalNum = Integer.parseInt(num);
            if (finalNum < minNum) {
C
chaychan 已提交
142
                //由于是整数,每次是递增1,所以如果传入的数字比帧数小,则直接使用setText()
C
chaychan 已提交
143
                this.setText(numStr);
C
chaychan 已提交
144 145
                return;
            }
C
chaychan 已提交
146 147 148
            ValueAnimator intAnimator = new ValueAnimator().ofInt(0, finalNum);
            intAnimator.setDuration(duration);
            intAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
C
chaychan 已提交
149
                @Override
C
chaychan 已提交
150 151 152
                public void onAnimationUpdate(ValueAnimator animation) {
                    int currentNum = (int) animation.getAnimatedValue();
                    setText(String.valueOf(currentNum));
C
chaychan 已提交
153 154
                }
            });
C
chaychan 已提交
155
            intAnimator.start();
C
chaychan 已提交
156 157
        } catch (NumberFormatException e) {
            e.printStackTrace();
C
chaychan 已提交
158
            setText(numStr);//如果转换Double失败则直接用setText
C
chaychan 已提交
159 160 161
        }
    }
}