我的progressbar与我的倒计时计时器不同步

z0qdvdin  于 2021-07-06  发布在  Java
关注(0)|答案(2)|浏览(392)

在这里,我创建了启动计时器的方法,下面的方法更新计时器:

private void startTimer()
{
    mCountDownTimer = new CountDownTimer(mTimeLeftInMillis, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            mTimeLeftInMillis = millisUntilFinished;
            updateCountDownText();
            progress++;
            pb.setProgress((int)progress*100/((int)millisUntilFinished/1000));
        }

        @Override
        public void onFinish()
        {
            progress++;
            pb.setProgress(100);
            //Vibration
            if (Build.VERSION.SDK_INT >= 26)
            {
                ((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(VibrationEffect.createOneShot(150, VibrationEffect.DEFAULT_AMPLITUDE));
                Toast.makeText(MainActivity2.this,"Done",Toast.LENGTH_SHORT).show();
            }
            else
            {
                ((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(VibrationEffect.createWaveform(new long[]{150}, new int[]{VibrationEffect.EFFECT_CLICK},-1));
            }
        }
    }.start();
}

 private void updateCountDownText()
{

    //time in minutes and seconds
    int minutes = (int)(mTimeLeftInMillis/1000)/60;
    int seconds = (int)(mTimeLeftInMillis/1000)%60;
    //formating the above to appear as String
    String timeLeftFormatted = String.format("%02d:%02d",minutes,seconds);
    timer.setText(timeLeftFormatted);
}

“pb”是我的progressbar的名称。它总是比倒计时提前2分钟完成,我不知道如何同步它们。此外,在完成时,由于某种原因不会触发振动,即使它以前触发过。”“进度”作为全局变量初始化为零。

evrscar2

evrscar21#

但我看不出你什么时候初始化progressbar。如果将pb.max(或java中的setmax)=100,并在每次单击时调用一个方法:

private changePb(long millisUntilFinished){
    pb.progress = (millisUntilFinished / millisStartValue) * 100
}

其中millisstartvalue是您在新倒计时计时器(mtimeleftinmillis)中输入的第一个值
在onfinish中,您不必为progressbar添加新值。

1cklez4t

1cklez4t2#

以下是解决方案:

long millisInFuture;

private void startTimer() {
    millisInFuture = mTimeLeftInMillis;
    mCountDownTimer = new CountDownTimer(mTimeLeftInMillis, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            mTimeLeftInMillis = millisUntilFinished;
            updateCountDownText();
            long millisPassed = millisInFuture - mTimeLeftInMillis;
            progress = (int) (millisPassed * 100 / millisInFuture);
            pb.setProgress(progress);
        }

        @Override
        public void onFinish() {
            pb.setProgress(100);
            Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                VibrationEffect effect =
                        VibrationEffect.createOneShot(150, VibrationEffect.DEFAULT_AMPLITUDE);
                vibrator.vibrate(effect);
                Toast.makeText(MainActivity2.this, "Done", Toast.LENGTH_SHORT).show();
            } else {
                vibrator.vibrate(150);
            }
        }
    }.start();
}

相关问题