NumberRunningTextView.java 6.0 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 12 13 14 15 16 17 18 19 20 21 22 23 24
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;

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 已提交
25 26 27
    private int duration;//动画的周期,默认为800ms
    private int minNum;//显示数字最少要达到这个数字才滚动 默认为1
    private float minMoney;//显示金额最少要达到这个数字才滚动 默认为0.3
C
chaychan 已提交
28 29 30 31

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

C
chaychan 已提交
32

C
chaychan 已提交
33 34 35 36 37 38 39 40 41 42 43 44
    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 已提交
45
        duration = ta.getInt(R.styleable.NumberRunningTextView_duration, 1000);
C
chaychan 已提交
46 47 48
        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 已提交
49 50
        minNum = ta.getInt(R.styleable.NumberRunningTextView_minNum, 3);
        minMoney = ta.getFloat(R.styleable.NumberRunningTextView_minMoney,0.1f);
51 52

        ta.recycle();
C
chaychan 已提交
53 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
    }


    /**
     * 设置需要滚动的金钱(必须为正数)或整数(必须为正数)的字符串
     *
     * @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 已提交
86
        } else if (textType == NUM_TYPE){
C
chaychan 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99
            playNumAnim(str);
        }
    }


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

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