手机的支付宝每次打开都有一个数字从0到特定数字的增加或减少, 表示数值的更新. Android也提供了CountDownTimer去实现类似的效果. 要自己来实现, 原理也并不复杂,主要用Handler去定时刷新数字, 和CountDownTimer类似. 于是结合Handler和TextView即可实现.
效果图, GIF图看起来不连贯. 运行代码效果会好一些.
例子不复杂, 所以直接上代码了,提供了自增自减, 继续自增或继续自减, 和取消的接口方法.
public class DecalAddTextView extends TextView{ private String TAG = "DecalAddTextView"; private DecimalFormat format = new DecimalFormat("0.000"); //真实值 private double realNum; //自增或自减量 private double variable; //TextView显示的值 private double tvValue; private boolean ifCancel = false; private int decalOpType = 0; private static int PLUS = 1; private static int MINUS = 2; private static int FINISHED = 3; private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { if(msg.what == PLUS) { if(tvValue < realNum) { setText(format.format(tvValue)); if(!ifCancel) { tvValue += variable; mHandler.sendEmptyMessageDelayed(PLUS, 30); } } else { decalOpType = FINISHED; setText(format.format(realNum)); } } else if(msg.what == MINUS) { if(tvValue > 0) { setText(format.format(tvValue)); if(!ifCancel) { tvValue -= variable; mHandler.sendEmptyMessageDelayed(MINUS, 30); } } else { decalOpType = FINISHED; setText("0.000"); } } } }; public DecalAddTextView(Context context){ this(context, null); } public DecalAddTextView(Context context, AttributeSet attrs){ this(context, attrs, 0); } public DecalAddTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public void setValue(double d) { realNum = d; } /** * 开始自增 */ public void startDecalPlus() { decalOpType = PLUS; tvValue = 0.000; //控制变化 variable = realNum / 30.000; mHandler.sendEmptyMessage(PLUS); } /** * 开始自减 */ public void startDecalMinus() { decalOpType = MINUS; tvValue = realNum; variable = realNum / 30.000; mHandler.sendEmptyMessage(MINUS); } public void cancel() { ifCancel = true; } public void continueDecalNum() { if(decalOpType == PLUS) { mHandler.sendEmptyMessage(PLUS); } else if(decalOpType == MINUS) { mHandler.sendEmptyMessage(MINUS); } else { Log.e(TAG, "tvValue may be zero that PLUS or MINUS's method can not be invoked..."); } } }
原文地址:http://blog.csdn.net/stzy00/article/details/45674307