Android倒计时案例展示

Android倒计时案例展示,第1张

概述1.Handler与Message方法实现倒计时功能 关于Handler与Message消息机制的原理可查看:Android--Handler使用应运及消息机制处理原理分析这个设计思路也是最常用的一种设计 例如:当点击一个按钮触发事件,在事件中调用handler的sendMessage的方法,那么在对应的handler 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上的显示效果

源码: 

总结

以上是内存溢出为你收集整理的Android倒计时案例展示全部内容,希望文章能够帮你解决Android倒计时案例展示所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/999575.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存