以毫秒为单位的带有时间戳的ProgressBar

时间:2019-06-30 14:04:15

标签: android timestamp progress-bar countdowntimer

我需要进度条,其间隔为两个时间戳值(以毫秒为单位)。

例如,我的时间戳记为08:46:11 30.06.2019和10:46:11 30.06.2019。但是当前时间是09:46:11 30.06.2019,所以现在进度条应该填满50%并增加到100,直到10:46:11 30.06.2019。

所以我尝试了下一个代码:

private void progressBar(){
            prBar.setProgress(i);
            mCountDownTimer=new CountDownTimer(ltimestampStop * 1000,ltimestampStart * 1000) {

                @Override
                public void onTick(long millisUntilFinished) {
                    Log.v("Log_tag", "Tick of Progress"+ i+ millisUntilFinished);
                    i++;
                    int Start = ltimestampStart.intValue();
                    int Stop = ltimestampStop.intValue();
                    prBar.setProgress((int)i*100/(Stop/Start));

                }

                @Override
                public void onFinish() {
                    //Do what you want
                    i++;
                    prBar.setProgress(100);
                }
            };
            mCountDownTimer.start();
        }

但是问题是int无法处理我的时间戳(毫秒),而setProgress()不能用于long类型。

我可以尝试完成什么任务?

UPD::根据我的任务,我认为countdowntimer不是我所需要的。

1 个答案:

答案 0 :(得分:1)

像这样初始化进度条和CountDownTimer-

 MyCountDownTimer myCountDownTimer;
`ProgressBar progressBar = (ProgressBar)findViewById(R.id.progressbar);`
 progressBar.setProgress(100);

让您的倒数计时器这样-

public class MyCountDownTimer extends CountDownTimer {

   public MyCountDownTimer(long millisInFuture, long countDownInterval) {
    super(millisInFuture, countDownInterval);
  }

    @Override
    public void onTick(long millisUntilFinished) {
    Log.d("Time in millis", String.valueOf(millisUntilFinished));
    int progress = (int) (millisUntilFinished/100);
    progressBar.setProgress(progress);
   }

    @Override
    public void onFinish() {
    Log.d("Time in millis", "Timer finished");
    progressBar.setProgress(0);
   }
}

像这样调用您的CountDownTimer-

myCountDownTimer = new MyCountDownTimer(50000, 1000);
myCountDownTimer.start();