倒计时 时间校准android,Android时间倒计时

有个CountDownTimer类,重写这个类可以实现自己的时间倒计时,我实现的代码如下:

public class MyCount extends CountDownTimer{ //构造函数,都是毫秒为单位 public MyCount(long millisInFuture, long countDownInterval) { super(millisInFuture, countDownInterval); // TODO Auto-generated constructor stub } @Override public void onFinish() { // TODO Auto-generated method stub //倒计完成 } @Override public void onTick(long millisUntilFinished) { // TODO Auto-generated method stub mTimeLabel.setText("Left: " + millisUntilFinished / 1000); } }

开始和取消调用其已有的方法start和cancel。

这里有别人写的一个类,很不错

import android.os.Handler; import android.os.Message; public abstract class AdvancedCountdownTimer { private final long mCountdownInterval; private long mTotalTime; private long mRemainTime; public AdvancedCountdownTimer(long millisInFuture, long countDownInterval) { mTotalTime = millisInFuture; mCountdownInterval = countDownInterval; mRemainTime = millisInFuture; } public final void seek(int value) { synchronized (AdvancedCountdownTimer.this) { mRemainTime = ((100 - value) * mTotalTime) / 100; } } public final void cancel() { mHandler.removeMessages(MSG_RUN); mHandler.removeMessages(MSG_PAUSE); } public final void resume() { mHandler.removeMessages(MSG_PAUSE); mHandler.sendMessageAtFrontOfQueue(mHandler.obtainMessage(MSG_RUN)); } public final void pause() { mHandler.removeMessages(MSG_RUN); mHandler.sendMessageAtFrontOfQueue(mHandler.obtainMessage(MSG_PAUSE)); } public synchronized final AdvancedCountdownTimer start() { if (mRemainTime <= 0) { onFinish(); return this; } mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_RUN), mCountdownInterval); return this; } public abstract void onTick(long millisUntilFinished, int percent); public abstract void onFinish(); private static final int MSG_RUN = 1; private static final int MSG_PAUSE = 2; private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { synchronized (AdvancedCountdownTimer.this) { if (msg.what == MSG_RUN) { mRemainTime = mRemainTime - mCountdownInterval; if (mRemainTime <= 0) { onFinish(); } else if (mRemainTime < mCountdownInterval) { sendMessageDelayed(obtainMessage(MSG_RUN), mRemainTime); } else { onTick(mRemainTime, new Long(100 * (mTotalTime - mRemainTime) / mTotalTime) .intValue()); sendMessageDelayed(obtainMessage(MSG_RUN), mCountdownInterval); } } else if (msg.what == MSG_PAUSE) { } } } }; }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值