实现倒计时的方式真是多种多样,今天作为小白的我就总结一下 ,如有雷同,哈哈哈。
方式1 sendEmptyMessageDelayed
int time = 10 ;
int RepeatCount = 10;
private Handler handler =new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 1://倒计时
time = time - 1;
if (time <= 0) {
time = RepeatCount ;
Log.e("tag" ,"结束 "+ StringFormat.dateFormathmm(new Date()) ) ;
} else {
Log.e("tag" ,"当前时间"+ StringFormat.dateFormathmm(new Date()) +" time " + String.valueOf(time)) ; handler.sendEmptyMessageDelayed(1, 1000);
}
break;
default:
break;
}
};
};
调用
handler.sendEmptyMessage(1);//通知倒计时
###方式二 CountDownTimer
Android 封装好的 ,比较简单
private CountDownTimer countDownTimer = new CountDownTimer(10000, 1000) {//第一个参数表示总时间,第二个参数表示间隔时间。
@Override
public void onTick(long millisUntilFinished) {
Log.e("tag", "当前时间" + StringFormat.dateFormathmm(new Date()) + " timer " + (millisUntilFinished / 1000));
}
@Override
public void onFinish() {
Log.e("tag", "结束");
}
};
调用
countDownTimer.start();
方式三 Timer + TimerTask + Handler
int time = 10 ;
Timer timer;
private Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 1:
if(time>0){
Log.e("tag" ,"当前时间"+ StringFormat.dateFormathmm(new Date()) +" timer " + String.valueOf(time)) ;
}else{
timer.cancel();
Log.e("tag" ,"结束 "+ StringFormat.dateFormathmm(new Date()) ) ;
}
break;
default:
break;
}
};
};
timer=new Timer();
TimerTask task = new TimerTask() {
@Override
public void run(){
time--;
handler.sendEmptyMessage(1);
}
};
timer.schedule(task, 100, 1000);
方式四 ValueAnimator
也是相当容易…
valueAnimator = ValueAnimator.ofInt(10, 0);
valueAnimator.setDuration(10 * 1000);// 注意 单位
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Integer timer = (Integer) animation.getAnimatedValue();
if (value == 0) {
Log.e("tag", "结束");
} else {
Log.e("tag" ,"当前时间"+ StringFormat.dateFormathmm(new Date()) +" timer " + String.valueOf(timer)) ;
}
}
});
valueAnimator.start();
方式五 Observable.timer + repeat
Observable.timer(1, TimeUnit.SECONDS)
.subscribeOn(Schedulers.computation())
.observeOn(AndroidSchedulers.mainThread())
.repeat(count)
.subscribe(aLong ->
{
timer--;//aLong 0
Log.e("tag" ,"当前时间"+ StringFormat.dateFormathmm(new Date()) +" timer " + String.valueOf(timer)) ;
if (timer<=0){
timer = count;
Log.e("tag" ," 结束 " ) ;
}
});
方式六 Observable.interval
Observable.interval(0, 1, TimeUnit.SECONDS)//设置0延迟,每隔一秒发送一条数据
.doOnSubscribe(new Action0() {
@Override
public void call() {
Log.e("tag","开始");
})
.subscribeOn(AndroidSchedulers.mainThread()) // 上面 指定主线程
.map(new Func1<Long, Long>() {
@Override
public Long call(Long aLong) {
return count - aLong; // 将 1,2,3 。。 转成 3,2,1 。。
}
})
.take(count + 1) //设置循环count +1次
.observeOn(AndroidSchedulers.mainThread()) //回调到主线程
.subscribe(new Observer<Long>() {
@Override
public void onCompleted() {
Log.e("tag" ," 结束 " ) ;
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(Long aLong) {
Log.e(TAG, "当前时间"+ StringFormat.dateFormathmm(new Date()) +"onNext:剩余时间 " + aLong);
}
});
注意:
在当前页面销毁时,注意当前的计时器是否还在继续,如有要 手动 取消,或置 空。