拓展 Android 原生 CountDownTimer 倒计时

2017-01-14 15:50:37来源:http://www.jianshu.com/p/281027bfb40c作者:didikee人点击

第七城市
拓展 Android 原生 CountDownTimer 倒计时

[TOC]


CountDownTimer


在系统的CountDownTimer上进行的修改,主要是拓展了功能,当然也保留了系统默认的模式.


四种模式:


Normal模式: 向上取整(我觉得应该是日常中用的最多的)
Floor模式: 向下取整
System模式: 系统默认的(保留系统原始功能)
SystemFix模式: 系统默认会少一个onTick()回调,这里只是把缺的这个回调加进去


在Activity中的代码如下:


final CountDownTimer timer=new CountDownTimer(10000,1000,CountDownTimer.NORMAL) {
@Override
public void onTick(long millisUntilFinished) {
Log.d("test","millisUntilFinished: "+millisUntilFinished);
long l = millisUntilFinished / 1000;
mTs.setText(""+l);
}
@Override
public void onFinish() {
Log.d("test","onFinish 0");
mTs.setText("0");
}
};
mBt_start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timer.start();
}
});

对应模式的Log如下所示:


Normal 模式
08-10 09:23:53.595 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 10000
08-10 09:23:54.595 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 9000
08-10 09:23:55.605 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 8000
08-10 09:23:56.605 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 7000
08-10 09:23:57.605 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 6000
08-10 09:23:58.605 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 5000
08-10 09:23:59.605 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 4000
08-10 09:24:00.605 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 3000
08-10 09:24:01.605 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 2000
08-10 09:24:02.615 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 1000
08-10 09:24:03.605 27628-27628/didikee.com.demoapk D/test: onFinish 0

Floor 模式
08-10 09:26:54.455 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 9000
08-10 09:26:55.455 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 8000
08-10 09:26:56.455 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 7000
08-10 09:26:57.455 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 6000
08-10 09:26:58.455 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 5000
08-10 09:26:59.455 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 4000
08-10 09:27:00.465 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 3000
08-10 09:27:01.465 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 2000
08-10 09:27:02.465 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 1000
08-10 09:27:03.465 27628-27628/didikee.com.demoapk D/test: onFinish 0

System 模式
08-10 09:29:03.035 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 9999
08-10 09:29:04.035 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 8998
08-10 09:29:05.035 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 7996
08-10 09:29:06.035 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 6995
08-10 09:29:07.045 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 5993
08-10 09:29:08.045 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 4992
08-10 09:29:09.045 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 3990
08-10 09:29:10.045 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 2989
08-10 09:29:11.045 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 1987
08-10 09:29:13.035 27628-27628/didikee.com.demoapk D/test: onFinish 0

SystemFix 模式
08-10 09:29:59.795 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 9999
08-10 09:30:00.795 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 8998
08-10 09:30:01.805 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 7997
08-10 09:30:02.795 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 6997
08-10 09:30:03.795 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 5996
08-10 09:30:04.805 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 4994
08-10 09:30:05.805 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 3992
08-10 09:30:06.805 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 2990
08-10 09:30:07.805 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 1989
08-10 09:30:08.805 27628-27628/didikee.com.demoapk D/test: millisUntilFinished: 988
08-10 09:30:09.795 27628-27628/didikee.com.demoapk D/test: onFinish 0

总结

项目里需要倒计时,用到的时候遇到了这个问题,查的时候别人也发现了,但是没看到有人修改,所以自己做了些改动,需要的小伙伴可以直接拿走.=.=
代码在这里: https://github.com/didikee/Android-Learning-Report


附言

有的小伙伴肯定嫌麻烦,谁会点开那么慢的Github,更不会注册账户Star或者Fork的,如果遇到你可以这样解决"丢一秒"的问题.


private CountDownTimer robLiftTimer = new CountDownTimer(5000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
Message msg = Message.obtain();
msg.what = 2;
float v = millisUntilFinished * 1.0f / 1000f;
int round = Math.round(v);
msg.arg1 = round;
Log.e("live", millisUntilFinished + "--" + round + "==" + v);
mHandler.sendMessage(msg);
if (round == 2) {//第二秒的时候"补"一秒
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
Message msg = Message.obtain();
msg.what = 2;
msg.arg1 = 1;
mHandler.sendMessage(msg);
}
}, 1000);
}
}

也很简单,对吧.=.=




第七城市

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台