自定义计时器

x443137254 2018-03-09 08:51:57
烦了2天这个计时器,自己切图码字母,思路按照网上那些人的说法,使用延迟刷新或者延迟发消息通知刷新的办法,最后跟手机自带秒表一对比,居然慢这么多,自己这个跑了20秒,秒表跑了30秒了。有谁有好点的办法更新计时器时间的没
...全文
526 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
jklwan 2018-03-15
  • 打赏
  • 举报
回复
引用 2 楼 x443137254 的回复:
校准的思路很不错噢,就是要是机子太卡的话,校准的时候误差已经超过预设值,是不是又有问题了
只有在onTick的时候自己的操作会有耗时,那些计算基本不耗时,所有尽量保证onTick中不要有太多耗时操作
x443137254 2018-03-14
  • 打赏
  • 举报
回复
引用 1 楼 jklwan 的回复:
系统的有CountDownTimer不过也有bug,如果有耗时,倒计时的间隔可能不准,我自己写了个,和CountDownTimer用法相同

/**
 * 自定义倒计时<br/>
 * Created by cds on 2016/8/29.
 */
public abstract class CountDownTimerUtil {
    private boolean mCancelled = false;
    private long mStopTimeInFuture;
    private final long countTime;
    private final long intervalTime;
    private CountDownHandler mHandler;
    private static final int MSG = 1;

    public CountDownTimerUtil(long countTime, long intervalTime) {
        this.countTime = countTime;
        this.intervalTime = intervalTime;
        mHandler = new CountDownHandler(this);
    }

    private static class CountDownHandler extends Handler{
        private WeakReference<CountDownTimerUtil> weakReference;
        public CountDownHandler(CountDownTimerUtil util){
            weakReference = new WeakReference<>(util);
        }
        @Override
        public void handleMessage(Message msg) {
            if (weakReference != null && weakReference.get() != null) {
                CountDownTimerUtil timerUtil = weakReference.get();
                if (timerUtil.mCancelled) {
                    return;
                }
                final long millisLeft = timerUtil.mStopTimeInFuture - SystemClock.elapsedRealtime();
                //LogUtil.d("millisLeft:" + millisLeft);
                if (millisLeft <= 0) {
                    timerUtil.onFinish();
                } else {
                    // 校准时间
                    long wait = (millisLeft % timerUtil.intervalTime == 0 ? timerUtil.intervalTime : millisLeft % timerUtil.intervalTime);
                    sendMessageDelayed(obtainMessage(MSG), wait);
                    timerUtil.onTick(millisLeft);
                }
            }
        }
    }

    public synchronized final void cancel() {
        mCancelled = true;
        mHandler.removeMessages(MSG);
        onFinish();
    }

    /**
     * 开始
     */
    public synchronized final CountDownTimerUtil start() {
        mCancelled = false;
        if (countTime <= 0) {
            onFinish();
            return this;
        }
        mStopTimeInFuture = SystemClock.elapsedRealtime() + countTime;
        mHandler.sendMessage(mHandler.obtainMessage(MSG));
        return this;
    }

    /**
     * Callback fired on regular interval.
     *
     * @param millisUntilFinished The amount of time until finished.
     */
    public abstract void onTick(long millisUntilFinished);

    /**
     * Callback fired when the time is up.
     */
    public abstract void onFinish();
}

校准的思路很不错噢,就是要是机子太卡的话,校准的时候误差已经超过预设值,是不是又有问题了
jklwan 2018-03-12
  • 打赏
  • 举报
回复
系统的有CountDownTimer不过也有bug,如果有耗时,倒计时的间隔可能不准,我自己写了个,和CountDownTimer用法相同

/**
 * 自定义倒计时<br/>
 * Created by cds on 2016/8/29.
 */
public abstract class CountDownTimerUtil {
    private boolean mCancelled = false;
    private long mStopTimeInFuture;
    private final long countTime;
    private final long intervalTime;
    private CountDownHandler mHandler;
    private static final int MSG = 1;

    public CountDownTimerUtil(long countTime, long intervalTime) {
        this.countTime = countTime;
        this.intervalTime = intervalTime;
        mHandler = new CountDownHandler(this);
    }

    private static class CountDownHandler extends Handler{
        private WeakReference<CountDownTimerUtil> weakReference;
        public CountDownHandler(CountDownTimerUtil util){
            weakReference = new WeakReference<>(util);
        }
        @Override
        public void handleMessage(Message msg) {
            if (weakReference != null && weakReference.get() != null) {
                CountDownTimerUtil timerUtil = weakReference.get();
                if (timerUtil.mCancelled) {
                    return;
                }
                final long millisLeft = timerUtil.mStopTimeInFuture - SystemClock.elapsedRealtime();
                //LogUtil.d("millisLeft:" + millisLeft);
                if (millisLeft <= 0) {
                    timerUtil.onFinish();
                } else {
                    // 校准时间
                    long wait = (millisLeft % timerUtil.intervalTime == 0 ? timerUtil.intervalTime : millisLeft % timerUtil.intervalTime);
                    sendMessageDelayed(obtainMessage(MSG), wait);
                    timerUtil.onTick(millisLeft);
                }
            }
        }
    }

    public synchronized final void cancel() {
        mCancelled = true;
        mHandler.removeMessages(MSG);
        onFinish();
    }

    /**
     * 开始
     */
    public synchronized final CountDownTimerUtil start() {
        mCancelled = false;
        if (countTime <= 0) {
            onFinish();
            return this;
        }
        mStopTimeInFuture = SystemClock.elapsedRealtime() + countTime;
        mHandler.sendMessage(mHandler.obtainMessage(MSG));
        return this;
    }

    /**
     * Callback fired on regular interval.
     *
     * @param millisUntilFinished The amount of time until finished.
     */
    public abstract void onTick(long millisUntilFinished);

    /**
     * Callback fired when the time is up.
     */
    public abstract void onFinish();
}

80,471

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧