您的位置:首页 > 移动开发 > Android开发

Android倒计时案例展示

2017-08-07 21:36 435 查看

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上的显示效果

源代码:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: