倒数计时器从应该的一秒开始 [英] Countdown timer starts from a second after it is supposed to

查看:23
本文介绍了倒数计时器从应该的一秒开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个倒计时对象.基本上,计时器应该从 10 倒计时到 1.它所做的是从 9 开始计时器,并以 1 结束,但它会在 1 上停留大约 2 秒......为什么会发生这种情况.我已经确保我的计时器没有重叠/连续被调用两次,因为这可能是原因.这是我的计时器对象的一部分.

I have a Countdowntimer object. Basically the timer is supposed to countdown from 10 to 1. What it does is starts the timer from 9 and it ends on 1 but it stays on 1 for about 2 seconds... Why does this happen. I've made sure that my timer is not overlapping/getting called twice in a row as that could be the cause. This is part of my timer object.

new CountDownTimer(10000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            Log.d("timer","Actual: " + Long.toString(millisUntilFinished)+".... "+Long.toString(millisUntilFinished/1000));
            time.setText(Long.toString(millisUntilFinished / 1000));
        }

这是我从 Log.d 输出中得到的:

This is what I am getting from the Log.d outputs:

03-03 21:23:15.438 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 9964.... 9
03-03 21:23:16.453 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 8949.... 8
03-03 21:23:17.463 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 7939.... 7
03-03 21:23:18.473 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 6929.... 6
03-03 21:23:19.482 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 5919.... 5
03-03 21:23:20.493 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 4909.... 4
03-03 21:23:21.503 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 3899.... 3
03-03 21:23:22.513 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 2889.... 2
03-03 21:23:23.523 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 1879.... 1
03-03 21:23:25.436 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 9978.... 9
03-03 21:23:26.453 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 8961.... 8
03-03 21:23:27.463 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 7951.... 7
03-03 21:23:28.473 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 6941.... 6
03-03 21:23:29.483 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 5931.... 5
03-03 21:23:30.493 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 4921.... 4
03-03 21:23:31.503 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 3911.... 3
03-03 21:23:32.514 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 2900.... 2
03-03 21:23:33.522 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 1891.... 1
03-03 21:23:35.431 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 9994.... 9
03-03 21:23:36.443 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 8982.... 8
03-03 21:23:37.454 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 7971.... 7
03-03 21:23:38.463 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 6961.... 6
03-03 21:23:39.474 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 5951.... 5
03-03 21:23:40.493 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 4932.... 4
03-03 21:23:41.503 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 3922.... 3
03-03 21:23:42.513 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 2912.... 2
03-03 21:23:43.523 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 1902.... 1
03-03 21:23:45.456 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 9978.... 9

推荐答案

似乎滴答声并没有像预期的那样以完全精确的间隔出现.在您的情况下,连续 onTick 回调中 millisUntilFinished 的值不能保证为 10000、9000、...、0.

It seems that the ticks don't come in at perfectly precise intervals as maybe expected. In your case, the value of millisUntilFinished in consecutive onTick callbacks isn't guaranteed to be 10000, 9000, ..., 0.

第一个刻度没有剩余 9 秒(或 9000 毫秒),它有 9964 毫秒.因此,从您在 CountDownTimer 上调用 start() 到第一个刻度回调的时间只过去了 36 毫秒.克里斯对他的回答的评论第一次回调发生在 1000 毫秒之后"似乎是不正确的(我会对他的回答发表评论,但我没有代表!).由于经典的整数除法问题,您在文本视图和日志语句中看到 9.您没有看到额外的 0.964 秒,因为除法的结果是 long 类型,它没有小数部分.尝试在日志语句中使用 1000.0 作为除数并删除 Long.toString() 并查看显示的值.

The first tick doesn't have 9 seconds (or 9000ms) remaining, it has 9964ms. So, only 36ms has passed from the time you called start() on the CountDownTimer to the time of this first tick callback. Chris' comment on his answer that the "first callback happens after 1000ms" seems to be incorrect (I'd make a comment on his answer, but I don't have the rep!). You're seeing 9 in your text view and log statement because of the classic integer division problem. You're not seeing the additional 0.964 seconds because the result of the division is of type long, which does not have a fractional part. Try using 1000.0 as your divisor in the log statement and remove the Long.toString() and see what value is shown.

关于倒计时结束的悬挂,请参见以下内容:https://stackoverflow.com/a/12283400/1244019

In regards to the hanging towards the end of the countdown, see the following: https://stackoverflow.com/a/12283400/1244019

根据您希望计时器的精确度,您的问题的解决方案可能是在您的 CountDownTimer 中保留一个计数器,更新您的 TextView 并在每次 onTick被调用.请参阅以下内容:

Depending on how precise you want your timer, perhaps a solution to your problem would be to keep a counter within your CountDownTimer, updating your TextView and decrementing the counter each time onTick is called. See the following:

new CountDownTimer(11500, 1000) {
    int i = 10;
    @Override
    public void onTick(long millisUntilFinished) {
        Log.d("timer","Time left: " + i);
        // Update TextView
        i--;
    }

    @Override
    public void onFinish() { }
}.start();

这篇关于倒数计时器从应该的一秒开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆