简体   繁体   English

如何在倒数计时器中更改刻度

[英]How to change tick in Countdowntimer

I have the following CountDownTimer that helps me to set a button visible every 490 ms 30 seconds long. 我有以下CountDownTimer ,可帮助我将按钮设置为每490毫秒30秒可见一次。

Is it possible to say that the first 10 seconds the "tick" should be for example 1000ms, the next 10 seconds 700 ms and the last 10 seconds 490ms ? 是否可以说“滴答声”的前10秒应为例如1000ms,接下来的10秒为700ms,最后10秒为490ms?

new CountDownTimer(30000, 490) {

@Override
public void onTick(long millisUntilFinished) {
    for(int i = 0; i< arr.size(); i++){
        Button aga = arr.get(i);
        if(aga.getVisibility() == View.VISIBLE){
            aga.setVisibility(View.GONE);
        }
    }
    int zufall = (int) (Math.random()*23);
    setNextButton(arr.get(zufall));
}
Timer mTimer; // Global 

public void countDownTimerCaller()
{
     static int count = 0;

     int time;

    switch(count)
    {
    case 0:
        time = 1000;
        break;
    case 1:
        time = 700;
        break;
    case 2:
        time = 490;
        break;

        default:
                mTimer.cancel(); // stop timer
            return;
    }

       new CountDownTimer(10000, time) {

        @Override
        public void onTick(long millisUntilFinished) {
            for(int i = 0; i< arr.size(); i++){
                Button aga = arr.get(i);
                if(aga.getVisibility() == View.VISIBLE){
                    aga.setVisibility(View.GONE);
                }
            }
            int zufall = (int) (Math.random()*23);
            setNextButton(arr.get(zufall));

        }

    }

    count++;
}

mTimer = new Timer().schedule(countDownTimerCaller, 10000); // call from where ever you created CountDownTimer instance

除了按这些间隔启动新计时器来满足新要求外,最好的选择就是找到一个共同的除数并将其设置为间隔,然后使用具有模数的开关在所需的时间动作。

Hope this will helpful. 希望这会有所帮助。

public void countDown(long time) {
        if (time == 490) {
            return;
        }

        new CountDownTimer(10000, time) {

            public void onTick(long millisUntilFinished) {
                // Do whatever you want
             }

             public void onFinish() {
                 countDown(nextTime); // nextTime can be 700, 100, ... It's up to you. (Your rule). :">
             }

        }.start();

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM