减缓进度条的进展

时间:2014-11-09 06:36:49

标签: android multithreading while-loop android-canvas

我在这里使用此代码来循环和更新自定义进度条的进度。

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

                }
 };

当它越来越接近结束时,我希望它开始减速,这是否可能而不必再进行另一次循环?

2 个答案:

答案 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);

            }


 };