CountDownTimer 在 TextView 上显示时跳过一秒 [英] CountDownTimer skipping one second when displaying on TextView

查看:32
本文介绍了CountDownTimer 在 TextView 上显示时跳过一秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 TextView 中显示一个简单的 CountDownTimer,但它似乎立即从 30 下降到 28,好像中间有延迟.我不知道如何着手修复这个小错误.这是我的代码:

I want to display a simple CountDownTimer in a TextView, but it seems to go from 30 down to 28 right away, as if there was a lag in between. I'm not sure how to go about on fixing this small bug. Here is my code:

这是在Button的点击监听器中:

This is in the click listener of the Button:

new CountDownTimer(30000, 1000) {

    @Override
    public void onTick(long millisUntilFinished) {
        coolDownTimer.setText("Cool Down for: " + String.valueOf(millisUntilFinished / 1000));
    }

    @Override
    public void onFinish() {
        animatedPic.setClickable(true);
        // reregister the proximity sensor
        sm.registerListener(sensorListener, proxSensor, SensorManager.SENSOR_DELAY_NORMAL);
        coolDownTimer.setText("GO!");
    }
}.start();

推荐答案

有2个问题:

  • 第一个问题是倒数计时器直到第一个滴答的时间过去后才会执行,在这种情况下,是在 1000 毫秒之后.

  • The first issue is that the countdown timer doesn't execute until time has elapsed for the first tick, in this case, after 1000ms.

第二个是经过的时间只是近似值.millisUntilFinished 不能保证以间隔的增量出现(您可以看到,如果不除以 1000,则第一个刻度略低于 29000).

The second is that the elapsed time is only approximate. millisUntilFinished is not guaranteed to come in increments of the interval (you can see that if you don't divide by 1000, the first tick is slightly under 29000).

要记住的另一件事是,您不能保证收到滴答电话.也就是说,如果设备没有足够的时间来完成一个滴答声,它可能会跳过它(通常只有在较快的间隔时才会注意到).

Another thing to keep in mind is that you're not guaranteed to receive a tick call. That is to say, if the device did not have enough time to complete a tick, it may skip it (generally only noticeable for faster intervals).

要解决问题 1,您可以简单地运行 onTick 代码(或将其重构为自己的方法)并在启动倒数计时器时运行它.

To solve issue 1, you can simply run the onTick code (or refactor it into its own method) and run it as you start the countdown timer.

对于问题 2,您可以简单地将数字四舍五入.

For issue 2, you can simply round the number.

例如:

new CountDownTimer(30000, 1000)
   {
        @Override
        public void onTick(long millisUntilFinished)
        {
            performTick(millisUntilFinished);
        }
        @Override
        public void onFinish()
        {
            animatedPic.setClickable(true);
            // reregister the proximity sensor
            sm.registerListener(sensorListener,proxSensor,SensorManager.SENSOR_DELAY_NORMAL);
            coolDownTimer.setText("GO!");
        }
    }.start();

    performTick(30000);


void performTick(long millisUntilFinished) {
    coolDownTimer.setText("Cool Down for: " + String.valueOf(Math.round(millisUntilFinished * 0.001f)));
}


如果您想确保以最小的延迟更新适当的值,您可能需要考虑减少间隔.


If you want to make sure the appropriate value is updated with minimal latency, you may want to consider reducing the interval.

这篇关于CountDownTimer 在 TextView 上显示时跳过一秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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