[Android]类似淘宝的数字自增自减文本
2015-05-12 20:13
274 查看
手机的支付宝每次打开都有一个数字从0到特定数字的增加或减少, 表示数值的更新. Android也提供了CountDownTimer去实现类似的效果. 要自己来实现, 原理也并不复杂,主要用Handler去定时刷新数字, 和CountDownTimer类似. 于是结合Handler和TextView即可实现.
效果图, GIF图看起来不连贯. 运行代码效果会好一些.
例子不复杂, 所以直接上代码了,提供了自增自减, 继续自增或继续自减, 和取消的接口方法.
效果图, 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..."); } } }
相关文章推荐
- Android数字跑动效果NumberRunView 类似支付宝数字跑动
- Android 类似未读短信图标显示数字效果的分析
- 改变手机浏览器(iPhone/Android)上文本输入框的默认弹出键盘(数字)
- Android项目类似淘宝 电商 搜索功能,监听软键盘搜索事件,延迟自动搜索,以及时间排序的搜索历史记录的实现
- Android属性动画实现TextView类似支付宝余额数字滚动
- AndroidRichText 让Textview轻松的支持富文本(图像ImageSpan、点击效果等等类似QQ微信聊天)
- android MPAndroidChart饼图实现图例后加数字或文本(定制图例)
- VerticalBannerView 是一个 android 平台下的自定义控件,通常用来展示广告,类似淘宝头条
- Android 消息数字提示,类似微信,BadgeView
- Android下edittext限制文本指定位置数字不能超出多少
- Android 类似淘宝 电商 搜索功能,监听软键盘搜索事件,延迟自动搜索,以及时间排序的搜索历史记录的实现
- Android自定义控件实现一个带文本与数字的圆形进度条
- [Android实例] 类似电表数字改变自定义控件
- Android 类似未读短信图标显示数字效果的分析
- Android项目类似淘宝 电商 搜索功能,监听软键盘搜索事件,延迟自动搜索,以及时间排序的搜索历史记录的实现
- android自定义视图之类似淘宝商品详情
- 关于Android类似qq和微信那种在桌面图标右上角更新数字的研究
- Android实现类似微信的文本输入框 效果
- 类似淘宝购物时,尺寸,颜色选择的radiobutton样式,android总怎么实现
- android实现图片右上方出现数字,类似邮箱的未读邮件效果