1. Handler 与Message方法实现倒计时功能

关于Handler与Message消息机制的原理可查看:Android--Handler使用应运及消息机制处理原理分析

这个设计思路也是最经常使用的一种设计

比如: 当点击一个button触发事件,在事件中调用 handler的sendMessage的方法。那么在相应的handler的handleMessage中就会接收到这个消息。在这里里面再进行一些逻辑推断,再通过调用handler的 sendMessageDelayed这个延时发送消息的方法进行消息发送,同一时候更新相关的设置信息

</pre><pre>
/**
* 使用handler 与 message方法实现倒计时功能
*/ /**
* 倒计时总长
*/
long totalCount = 6000;
/**
* 倒计时时间间隔
*/
long flag = 1000; private void startCountDown4() {
Message msg = Message.obtain();
msg.what = 001;
mhHandler.sendMessage(msg);
} private Handler mhHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
if (msg.what == 001) {
if (totalCount > 0) {
/**
* 发送延迟1秒的消息
*/
Message msg1 = Message.obtain();
msg1.what = 001;
mhHandler.sendMessageDelayed(msg1, flag);
/**
* 更新显示UI
*/
textview.setText(totalCount / 1000 + "秒");
/**
* 更新倒计时总时间
*/
totalCount -= flag;
} } };
};

2 使用Handler的post与Runnable结合实现倒计时功能

运行handler.post();方法,方法中传入一个runnable实例对象。会运行 这个实例对象的run方法。在run方法中再进行想着逻辑的推断。然后调用handler.postDelayed方法实例延迟运行相关操作的方法,这里是运行了相同的操作。从而达到实现一个倒计时的功能

</pre><pre>
/**
* 使用handler的post方法与 runnable结合 实现倒计时的功能
*/
/**
* 倒计时总长
*/
long totalCount = 6000;
/**
* 倒计时时间间隔
*/
long flag = 1000;
public Handler handler = new Handler();
public Runnable countDownRunn = new Runnable() { @Override
public void run() {
if (totalCount > 0) {
handler.postDelayed(countDownRunn, flag);
totalCount -= flag;
textview.setText(totalCount / 1000 + "秒");
} }
}; public void startCountDown3() {
handler.post(countDownRunn);
}

3  使用子线程来实现倒计时

这里通过开启一个子线程,在线程中开启一个while循环,在这个循环中通过调用方法 seytemClock.sleep(time) 使用这个线程堵塞time时间,然后再进行相关的设置,从而达到倒计时的效果

	/**
* 倒计时总长
*/
long totalCount = 6000;
/**
* 倒计时时间间隔
*/
long flag = 1000; /**
* 使用子线程实现倒计时的功能
*/
public void startCountDown2() {
new Thread() {
public void run() {
while (totalCount > 0) {
/**
* 设置每隔flag时间间隔运行一次
*/
SystemClock.sleep(flag);
/**
* 更新页面显示时间
*/
MainActivity.this.runOnUiThread(new Runnable() { @Override
public void run() {
textview.setText(totalCount / 1000 + "秒");
}
}); totalCount -= flag;
}
};
}.start();
}

4 使用CountDownTimer类来实现倒计时功能

相对来说使用这个类的设计逻辑比較简单

	/**
* 使用CountDownTimer 类实现倒计时功能
*/ public void startCountDown() {
TimeCount timeCount = new TimeCount(60000, 1000);
timeCount.start(); } public class TimeCount extends CountDownTimer { /**
*
* @param millisInFuture
* 总倒计时时长 单位毫秒
* @param countDownInterval
* 倒计时时间间隔 单位毫秒
*/
public TimeCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
} @Override
public void onTick(long l) { textview.setText(l / 1000 + "秒");
} @Override
public void onFinish() { }
}

5 使用属性动画的方式来实现倒计时

	/**
* 使用属性动画的方式来实现
*
*/
int totalNumber = 6000;
private void startCountDown5(){
final ValueAnimator animator = ValueAnimator.ofInt(0,totalNumber/1000);
animator.setDuration(totalNumber);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Integer value = (Integer) animation.getAnimatedValue();
/**
* value 这里获取到的是递增获取到的时间 单位为秒
*
*/
textview.setText((totalCount-value*1000)/1000+"秒");
if (value*1000>(totalCount-1000)) {
animator.cancel();
}
}
});
animator.start(); }

这样的方式实现的效果可能会不佳,只是也不失为一种思路,在使用的时候能够调整ValueAnimator.ofInt()中第二个參数的计算參数

6 .使用 Timer 与TimerTask方式实现

Timer mSignTimer = new Timer();
mSignTimer.schedule(new TimerTask() {
@Override
public void run() {
mCurrentTime += 1000;
getActivity().runOnUiThread(
new Runnable() {
public void run() {
mSignDateTextView.setText(DataUtils
.getDateYear(mCurrentTime) + ""); }
}); }
}, 0, 1000);

这里用schedule方法。第一个參数是TimerTask对象,第二个參数表示開始运行前的延时时间(单位是milliseconds。这里定义了0),第三个參数是表示定时运行时间(单位是milliseconds,这里定义了1000)

这里使用的mCurrentTime是系统当前的时间 ,设置schedule的第三个參数为1000,也就是每1秒运行一下这种方法,那么每次运行加1000毫秒。从而实现了计时功能,当然这里使用的不是倒计时的功能。加一些逻辑推断是能够实现倒计时功能的

这里是直接调用Context对象的runOnUiThread方法。在主线程中进行更新Ui上的显示效果

源代码:

最新文章

  1. 跟我一起数据挖掘(21)——redis
  2. python练手基础
  3. flume坑之channel.transactionCapacity和HdfsSink.batchSize
  4. Spring-Context之二:使用Spring提供的测试框架进行测试
  5. javascript学习随笔(二)原型prototype
  6. iOS的runtime(转)
  7. 转载:JMS-ActiveMQ浅析
  8. InputStream和Reader区别
  9. php中检查文件或目录是否存在的代码小结
  10. lintcode:线段树的修改
  11. Spring AOP基础知识
  12. 第五篇、iOS常用的工具 app icon 、office文件格式互转、在线HTML编辑器、16、10进制互转
  13. Android开发系列----学习伊始
  14. PHP安全编程:防止源代码的暴露(转)
  15. js动态新增组合Input标签
  16. poj3678(two-sat)
  17. 监控mysql主从
  18. [LeetCode] Add One Row to Tree 二叉树中增加一行
  19. 系统调用号、errno
  20. Bootstrap 4正式发布还有意义吗?

热门文章

  1. javascript 原生得到document.Element的方法
  2. open -python操作文件
  3. cloudstack ssvm 管理地址不够造成无法启动修复过程
  4. [2]树的DFS序
  5. 所以学树分块的时候为什么要看vector啊sjb
  6. [Codeforces #201] Tutorial
  7. [HDU5965]扫雷
  8. python开发_pprint()
  9. 九月回顾 这篇文章和ACM毫无关系= =
  10. Level-shifting nixes need for dual power supply