[英]Java Android, Countdown Timer and ProgressBar (read first)
我有倒數計時器,它由ProgressBar成功顯示。 但是不幸的是存在問題。
如果我之前記得倒數計時器的功能
public void onFinish() { progresBar.setProgress(100); }
顯示屏會混亂一會兒。
我真的很想念帶有Time.deltaTime
Unity Engine;
這是我的代碼:
countDownTimer2 = new CountDownTimer(5000,1000) {
int i = 0;
@Override
public void onTick(long milliSecondUntilFinished) {
i++;
progresBar.setProgress((int) i * 100 / (5000/1000));
}
@Override
public void onFinish() {
progresBar.setProgress(100);
}
}.start();
在設置進度之前,您要遞增i
變量。 首次調用onTick()時, i
值為1
。 因此,您設置的進度是20
,等於1/5
。 用這個 -
@Override
public void onTick(long milliSecondUntilFinished) {
progresBar.setProgress((int) i * 100 / (5000/1000));
i++;
}
另外,您最好使用milliSecondUntilFinished
來查找進度,而不要不必要地引入i
變量。
對於顯示問題,我非常懷疑您在調用新計時器之前沒有取消已經運行的計時器。 如果是,則這兩個計時器將一直運行,直到較舊的計時器結束為止(到此為止,進度欄將表現異常)。
您可以將前一個計時器取消為-
countDownTimer2.cancel();
在開始新的操作之前,請先執行此操作。
使用:
countDownTimer2 = new CountDownTimer(1000,5000) {
int i = 0;
@Override
public void onTick(long milliSecondUntilFinished) {
i++;
progresBar.setProgress((int) i * 100 / (5000/1000));
}
@Override
public void onFinish() {
progresBar.setProgress(100);
}
}.start();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.