Android倒计时案例展示




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

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

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

例如: 当点击一个按钮触发事件,在事件中调用 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
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

早起的年轻人

创作源于分享

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值