设置进度栏位置

时间:2019-06-30 16:38:51

标签: android timestamp progress-bar

我需要进度条,巫婆的位置取决于毫秒中的两个时间戳值,因此它取决于将来的时间戳,过去的时间戳和当前的时间戳。

例如,我的时间戳记为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

我已经尝试了下一个代码,但是现在我已经了解到,使用CountDownTimer是没有用的,并且不适用于上述任务:

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();
        }

那我该如何在取决于未来时间,过去时间和现在时间的特定位置上创建进度条?

1 个答案:

答案 0 :(得分:0)

您可以尝试以下操作:

long startTimeInMillis = ...;     // 08:46:11 30.06.2019
long stopTimeInMillis = ...;      // 10:46:11 30.06.2019
long currentTimeInMillis = ...;   // 09:46:11 30.06.2019
long totalTimeInMilis = stopTimeInMillis - startTimeInMillis;

// Standart logic
long remainingTimeInMillis = stopTimeInMillis - currentTimeInMillis;
// But this line assures remaining time is not greater than total time
long remainingTimeInMillis = currentTimeInMillis < startTimeInMillis ?
        totalTimeInMilis : stopTimeInMillis - currentTimeInMillis;

// Update every second
long countDownIntervalInMillis = 1000;
// Alternatively you can adjust interval such that it is called only in integer percent changes
int remainingSteps = (int) (((double) remainingTimeInMillis / totalTimeInMilis) * 100);
long countDownIntervalInMillis = remainingTimeInMillis / remainingSteps;

mCountDownTimer = new CountDownTimer(remainingTimeInMillis, countDownIntervalInMillis) {

    @Override
    public void onTick(long millisUntilFinished) {
        int completedPercent = (int) ((1 - (double) millisUntilFinished / totalTimeInMilis) * 100);
        prBar.setProgress(completedPercent);
    }

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