android进度条如何平稳运行5秒然后更改活动

时间:2013-07-25 07:58:42

标签: android android-widget android-progressbar

我的应用程序以包含进度条的活动开始。此活动会在5秒后自动转换为另一个活动。我想使用平滑的进度条5秒。 我试过这段代码:

 progressBar = (ProgressBar) findViewById(R.id.prog_bar);   
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            progressBar.setProgress(progressBarStatus);
            progressBarStatus += 10;        
        }
    }, 500);

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            progressBar.setProgress(progressBarStatus);
            progressBarStatus += 10;        
        }
    }, 1000);

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            progressBar.setProgress(progressBarStatus);
            progressBarStatus += 10;        
        }
    }, 1500);
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            progressBar.setProgress(progressBarStatus);
            progressBarStatus += 10;        
        }
    }, 2000);
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            progressBar.setProgress(progressBarStatus);
            progressBarStatus += 10;        
        }
    }, 2500);
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            progressBar.setProgress(progressBarStatus);
            progressBarStatus += 10;        
        }
    }, 3000);
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            progressBar.setProgress(progressBarStatus);
            progressBarStatus += 10;        
        }
    }, 3500);
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            progressBar.setProgress(progressBarStatus);
            progressBarStatus += 10;        
        }
    }, 4000);
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            progressBar.setProgress(progressBarStatus);
            progressBarStatus += 10;        
        }
    }, 4500);
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            progressBar.setProgress(progressBarStatus);
            progressBarStatus += 10;        
        }
    }, 5000);
    progressBar = (ProgressBar) findViewById(R.id.prog_bar);    
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            progressBar.setProgress(progressBarStatus);
            progressBarStatus += 10;        
        }
    }, 500);

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            progressBar.setProgress(progressBarStatus);
            progressBarStatus += 10;        
        }
    }, 1000);

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            progressBar.setProgress(progressBarStatus);
            progressBarStatus += 10;        
        }
    }, 1500);
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            progressBar.setProgress(progressBarStatus);
            progressBarStatus += 10;        
        }
    }, 2000);
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            progressBar.setProgress(progressBarStatus);
            progressBarStatus += 10;        
        }
    }, 2500);
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            progressBar.setProgress(progressBarStatus);
            progressBarStatus += 10;        
        }
    }, 3000);
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            progressBar.setProgress(progressBarStatus);
            progressBarStatus += 10;        
        }
    }, 3500);
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            progressBar.setProgress(progressBarStatus);
            progressBarStatus += 10;        
        }
    }, 4000);
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            progressBar.setProgress(progressBarStatus);
            progressBarStatus += 10;        
        }
    }, 4500);
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            progressBar.setProgress(progressBarStatus);
            progressBarStatus += 10;        
        }
    }, 5000);

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            final Intent mainIntent = new Intent(MainPage.this, DashboardActivity.class);
            MainPage.this.startActivity(mainIntent);
            MainPage.this.finish();

        }
    }, 4500);

}

}

我没有其他方法可以这样做。我需要更有效的方法和平稳的进度吧....

2 个答案:

答案 0 :(得分:0)

创建一个Runnable。 r并在其run()方法中调用handler.postDelayed(r,500),确保它是有条件调用的,因此它不会永远循环

答案 1 :(得分:0)

试试这个

 new AsyncTask<Void, Integer, Void>(){

                @Override
                protected Void doInBackground(Void... params) {
                    int progressStatus = 0;
                    while (progressStatus < 5000) {
                        progressStatus++;
                        publishProgress(progressStatus);
                        try {
                            Thread.sleep(1);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    return null;  
                }

                @Override
                protected void onProgressUpdate(Integer... values) {
                    progressBar.setProgress(values[0]);

                }
            }.execute();