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

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

问题描述

我有一个Countdowntimer对象.基本上,计时器应该从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

推荐答案

似乎the虫并没有按预期的那样精确地间隔出现.对于您而言,连续的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秒(或9000ms),它有9964ms.因此,从您在CountDownTimer上调用start()的时间到此第一个滴答回调的时间仅过去了36ms.克里斯对他的答案的评论是第一次回调发生在1000毫秒后",这似乎是不正确的(我会对他的答案发表评论,但我没有代表!).由于经典的整数除法问题,您在文本视图和log语句中看到的是9.您看不到额外的0.964秒,因为除法的结果是long类型,它没有小数部分.尝试在log语句中使用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天全站免登陆