最近做倒计时领取红包功能,用到倒计时功能,由于不需要太精确,所以选择了系统提供的CountDownTimer实现倒计时功能。
1 CountDownTimer
CountDownTimer是Google提供的一个倒计时工具类,利用这个工具可以很方便的实现倒计时功能。
Schedule a countdown until a time in the future, with regular notifications on intervals along the way.
构造函数:
CountDownTimer(long millisInFuture, long countDownInterval)
参数说明:
- millisInFuture :总倒计时时长
- countDownInterval:每次时间间隔
两个重要回调函数:
//countDownInterval间隔指定时间的回调,millisUntilFinished:剩余的时间
public abstract void onTick(long millisUntilFinished);
//倒计时结束时回调
public abstract void onFinish();
实现原理:CountDownTimer的源码很简单,内部利用Handler实现。
官方例子:
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
System.out.println("===============seconds remaining: " + millisUntilFinished);
}
public void onFinish() {
System.out.println("==============done!");
}
}.start();
**Result:
===============seconds remaining: 29969
===============seconds remaining: 28968
===============seconds remaining: 27967
===============seconds remaining: 26966
===============seconds remaining: 25965
===============seconds remaining: 24964
===============seconds remaining: 23962
===============seconds remaining: 22961
===============seconds remaining: 21959
===============seconds remaining: 20958
===============seconds remaining: 19957
===============seconds remaining: 18955
===============seconds remaining: 17954
===============seconds remaining: 16952
===============seconds remaining: 15951
===============seconds remaining: 14949
===============seconds remaining: 13948
===============seconds remaining: 12946
===============seconds remaining: 11945
===============seconds remaining: 10943
===============seconds remaining: 9942
===============seconds remaining: 8940
===============seconds remaining: 7939
===============seconds remaining: 6938
===============seconds remaining: 5937
===============seconds remaining: 4936
===============seconds remaining: 3935
===============seconds remaining: 2934
===============seconds remaining: 1934
==============done!
注意
**
可以看到每次onTrack中的时间都不是整秒数的值(不是30000,29000,28000,27000),而且误差越来越大。
存在问题:
- 内部使用了Handler,同时倒计时一般和View绑定,所以当Activity或者Fragment销毁时一定要cancle掉
- 每次 onTick() 返回的值都不是a*1000的整数,会有几毫秒的误差,由于最终我们会转成秒使用,就会造成倒计时时数值的跳跃。网上的解决方案是在总时间上添加一定误差时间达到准确的目的(10秒大概相差4-5毫秒)。
- 最后执行onFinish明显感觉时间较长,一般接近两秒,且没有出现0值。
2 简单封装
public class CountDownTextView extends android.support.v7.widget.AppCompatTextView {
private CountDownTimer mCountDownTimer;
private long mCurrentTime = 0;
private boolean isPause = false;
private long mMaxTime = 60000;
//当前倒计时时间
private CurrentTimeListener mCurrentTimeListener;
//倒计时完成之后的文案
private String mFinishText = "完成";
public String getmFinishText() {
return mFinishText;
}
public void setmFinishText(String mFinishText) {
this.mFinishText = mFinishText;
}
public CountDownTextView(Context context) {
this(context, null, 0);
}
public CountDownTextView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CountDownTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public void setCurrentTimeListener( CurrentTimeListener tCurrentTimeListener){
this.mCurrentTimeListener = tCurrentTimeListener;
}
public void setMaxTime(long time) {
mMaxTime = time + 400;
cancelCountDown();
setText(getCountTimeByLong(mMaxTime));
initCountDownTimer(mMaxTime);
}
private void init() {
}
public void initCountDownTimer(long millisInFuture) {
mCountDownTimer = new CountDownTimer(millisInFuture, 1000) {
@Override
public void onTick(long millisUntilFinished) {
mCurrentTime = millisUntilFinished;
if (mCurrentTimeListener != null){
mCurrentTimeListener.currentTime(mCurrentTime);
}
setText(getCountTimeByLong(millisUntilFinished));
isPause = false;
}
public void onFinish() {
setText(mFinishText);
}
};
}
public void startCountDown() {
if (mCountDownTimer != null && mMaxTime > 1000) {
isPause = false;
mCountDownTimer.start();
}
}
public void cancelCountDown() {
if (mCountDownTimer != null) {
isPause = false;
mCountDownTimer.cancel();
}
}
public void resumeCountDown() {
if (mCurrentTime != 0 && isPause) {
initCountDownTimer(mCurrentTime);
mCountDownTimer.start();
isPause = false;
}
}
public void pauseCountDown() {
if (!isPause) {
isPause = true;
mCountDownTimer.cancel();
}
}
public long getmCurrentTime() {
return mCurrentTime;
}
public interface CurrentTimeListener{
public void currentTime(long curTime);
}
public static String getCountTimeByLong(long finishTime) {
int totalTime = (int) Math.round((double) finishTime / 1000);
int hour = 0, minute = 0, second = 0;
if (3600 <= totalTime) {
hour = totalTime / 3600;
totalTime = totalTime - 3600 * hour;
}
if (60 <= totalTime) {
minute = totalTime / 60;
totalTime = totalTime - 60 * minute;
}
if (0 <= totalTime) {
second = totalTime;
}
StringBuilder sb = new StringBuilder();
if (hour < 10) {
sb.append("0").append(hour).append(":");
} else {
sb.append(hour).append(":");
}
if (minute < 10) {
sb.append("0").append(minute).append(":");
} else {
sb.append(minute).append(":");
}
if (second < 10) {
sb.append("0").append(second);
} else {
sb.append(second);
}
return sb.toString();
}
}
使用方式:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity174">
<FrameLayout
android:layout_width="70dp"
android:layout_height="wrap_content">
<ImageView
android:layout_width="70dp"
android:layout_height="50dp"
android:scaleType="fitCenter"
android:src="@mipmap/ic_launcher"/>
<com.ldx.canvasdrawdemo.CountDownTextView
android:id="@+id/countdown_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/dialog_user_protocol_shape"
android:layout_gravity="bottom|center_horizontal"
android:gravity="center"
android:paddingTop="1dp"
android:paddingBottom="1dp"
android:paddingLeft="2dp"
android:paddingRight="2dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
</FrameLayout>
</android.support.constraint.ConstraintLayout>
public class MainActivity174 extends AppCompatActivity {
private CountDownTextView mCountDownTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main174);
SignedDialogFragment signedDialogFragment = SignedDialogFragment.newInstance(new SignedInfoResult(),true);
signedDialogFragment.setCancelable(false);
signedDialogFragment.show( getSupportFragmentManager(), SignedDialogFragment.TAG );
mCountDownTextView = findViewById(R.id.countdown_text_view);
mCountDownTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 9);
mCountDownTextView.setMaxTime(300000);
mCountDownTextView.setCurrentTimeListener(new CountDownTextView.CurrentTimeListener() {
@Override
public void currentTime(long curTime) {
//存储剩余时间
}
});
mCountDownTextView.startCountDown();
}
//每次可见调用网络查看红包状态
public void getRedPackState(){
// 每次可见请求网络,如果失败不显示,如果成功进行下面判断
// 1 如果状态显示已经完成则不显示,
// 2如果没有完成,对比本地记录时间戳和返回时间戳,如果时间戳相同(没有完成),则本地记录的倒计时不变,开始倒计时,倒计时完成时更新状态
// 如果时间戳不同(任务没有完成),则重新记录时间戳,然后初始化本地记录的时间,开始倒计时。
}
public void startCountDownText(long time){
mCountDownTextView.setMaxTime(180000);
mCountDownTextView.startCountDown();
}
}