我在这里使用此代码来循环和更新自定义进度条的进度。
class MyThreadRunner implements Runnable {
// @Override
int count = 0;
public void run() {
try {
while (count < 90) {
Thread.sleep(10);
count += 1;
progressBar.setProgress(count);
progressBar.setText(count + "%");
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
当它越来越接近结束时,我希望它开始减速,这是否可能而不必再进行另一次循环?
答案 0 :(得分:2)
为什么不改变超过90的值的睡眠时间?
class MyThreadRunner implements Runnable {
// @Override
int count = 0;
public void run() {
try {
while (count < 100) {
//500 is half a second...
int sleepTime = count < 90 ? 10 : 500;
Thread.sleep(sleepTime);
count += 1;
progressBar.setProgress(count);
progressBar.setText(count + "%");
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
答案 1 :(得分:0)
class MyThreadRunner implements Runnable {
// @Override
int count = 0;
int sleepTime = 0;
public void run() {
while (count < 100) {
sleepTime = count < 90 ? 10 : 100;
Thread.sleep(sleeptime);
}
};