ProgressBar 不会在启动屏幕上逐渐显示进度

发布于 2024-11-30 22:57:33 字数 2030 浏览 1 评论 0原文

我有一个持续 5 秒的闪屏,我想使用 ProgressBar 来表示进度。

public class SplashActivity extends Activity {

    private static final long SPLASHTIME = 5000;
    private ProgressBar progressBar;

    @Override
    public void onCreate(Bundle icicle) {

        super.onCreate(icicle);
        setContentView(R.layout.splash);

        SplashHandler handlerSplash = new SplashHandler();
        progressBar = (ProgressBar) findViewById(R.id.progression);
        progressBar.setMax((int) ((SPLASHTIME) / 1000));
        progressBar.setProgress(0);
        Message msg = new Message();
        msg.what = 0;
        handlerSplash.sendMessageDelayed(msg, SPLASHTIME);


        ThreadProgressBar threadProgress = new ThreadProgressBar();
        threadProgress.start();


    }

    private class ThreadProgressBar extends Thread {
        ProgressBarHandler handlerProgress = new ProgressBarHandler();
        public void run() {
            try {

                while (progressBar.getProgress() <= progressBar.getMax()) {
                    Thread.sleep(1000);

                    handlerProgress.sendMessage(handlerProgress.obtainMessage());
                }
            } catch (java.lang.InterruptedException e) {

            }
        }
    }

        private class ProgressBarHandler extends Handler {

            public void handleMessage(Message msg) {
                progressBar
                        .incrementProgressBy((int) (SPLASHTIME / SPLASHTIME));
            }
        }

        private class SplashHandler extends Handler {

            public void handleMessage(Message msg) {
                switch (msg.what) {
                default:
                case 0:
                    super.handleMessage(msg);
                    // new ProgressBarIncrease().execute();
                    Intent intent = new Intent();
                    intent.setClass(SplashActivity.this, RdvTab.class);
                    startActivity(intent);

                }
            }
        }

I have a splashscreen which lasts 5 seconds, and I want to represent the progress using a ProgressBar.

public class SplashActivity extends Activity {

    private static final long SPLASHTIME = 5000;
    private ProgressBar progressBar;

    @Override
    public void onCreate(Bundle icicle) {

        super.onCreate(icicle);
        setContentView(R.layout.splash);

        SplashHandler handlerSplash = new SplashHandler();
        progressBar = (ProgressBar) findViewById(R.id.progression);
        progressBar.setMax((int) ((SPLASHTIME) / 1000));
        progressBar.setProgress(0);
        Message msg = new Message();
        msg.what = 0;
        handlerSplash.sendMessageDelayed(msg, SPLASHTIME);


        ThreadProgressBar threadProgress = new ThreadProgressBar();
        threadProgress.start();


    }

    private class ThreadProgressBar extends Thread {
        ProgressBarHandler handlerProgress = new ProgressBarHandler();
        public void run() {
            try {

                while (progressBar.getProgress() <= progressBar.getMax()) {
                    Thread.sleep(1000);

                    handlerProgress.sendMessage(handlerProgress.obtainMessage());
                }
            } catch (java.lang.InterruptedException e) {

            }
        }
    }

        private class ProgressBarHandler extends Handler {

            public void handleMessage(Message msg) {
                progressBar
                        .incrementProgressBy((int) (SPLASHTIME / SPLASHTIME));
            }
        }

        private class SplashHandler extends Handler {

            public void handleMessage(Message msg) {
                switch (msg.what) {
                default:
                case 0:
                    super.handleMessage(msg);
                    // new ProgressBarIncrease().execute();
                    Intent intent = new Intent();
                    intent.setClass(SplashActivity.this, RdvTab.class);
                    startActivity(intent);

                }
            }
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

辞取 2024-12-07 22:57:33

我想既然 SplashActivity 本身就是一个 Activity,那么阻止 UI 就可以了,对吗? AsyncTask有时运行不流畅,如果想让更新流畅显示,可以改成worker ThreadHandlder

  • 在 SplashActivity 中创建 Handler inside SplashActivity
  • 在主线程中,显示​​进度条或启动栏或任何你喜欢的内容,
  • 同时让另一个线程运行倒计时,每秒一次 sendEmptyMessage()处理程序。 5 秒后,向 Handler 发送消息以关闭对话框并启动,然后工作线程 Thread 结束。

更新:抱歉反馈缓慢。这个怎么样?我还没有测试过它,但这不是最好的实现,你可以使用这个逻辑

public class SplashActivity extends Activity {

    private static final int SPLASHTIME = 5000;
    // u can change the value here for smoother effect
    private static final long UPDATEINTERVAL = 1000;
    private ProgressBar progressBar;

    @Override
    public void onCreate(Bundle icicle) {

        super.onCreate(icicle);
        setContentView(R.layout.splash);

        progressBar = (ProgressBar) findViewById(R.id.progression);

        new Thread(new Runnable(){
            @Override
            public void run(){
                 int progress = 0;
                 while(true){
                     // send current progress to handler
                     mHandler.sendEmptyMessage(progress);
                     // break from the loop after SPLASHSCREEN millis
                     if(progress > SPLASHSCREEN)
                         break;
                     // increase the progress
                     progress = (int) (progress + UPDATEINTERVAL);
                     // sleep the worker thread
                     Thread.sleep(UPDATEINTERVAL);
                 }
            }
        }).start();
    }

    private Handler mHandler = new Handler(){
         @Override
         public void handleMessage(Message message){
             // get the current progress
             int progress = message.what;
             if(progress <= SPLASHTIME){
                 progressBar.setProgress(progress);
             }
             else{
                 // finish splashActivity here & do what u want do to after splashscreen, for example
                 finish();
                 Intent intent = new Intent(SpashActivity.this, MenuActivity.class);
                 startActivity(intent);
             }
         }
     }
 }

I guess since SplashActivity itself is an activity, it is ok to block UI right? AsyncTask sometimes run not gradually, if you want the update to be displayed smoothly, how about changing to worker Thread and Handlder.

  • Create Handler inside SplashActivity
  • In the main Thread, display progressBar or splash or whatever you like
  • While make another Thread to run the countdown, once in every second sendEmptyMessage() to Handler. After 5 seconds, send a message to Handler to dismiss dialog and splash then the worker Thread ends.

UPDATED: Sorry for the slow feedback. How about this? I haven't tested it yet and it's not the best implementation though, you can use this logic

public class SplashActivity extends Activity {

    private static final int SPLASHTIME = 5000;
    // u can change the value here for smoother effect
    private static final long UPDATEINTERVAL = 1000;
    private ProgressBar progressBar;

    @Override
    public void onCreate(Bundle icicle) {

        super.onCreate(icicle);
        setContentView(R.layout.splash);

        progressBar = (ProgressBar) findViewById(R.id.progression);

        new Thread(new Runnable(){
            @Override
            public void run(){
                 int progress = 0;
                 while(true){
                     // send current progress to handler
                     mHandler.sendEmptyMessage(progress);
                     // break from the loop after SPLASHSCREEN millis
                     if(progress > SPLASHSCREEN)
                         break;
                     // increase the progress
                     progress = (int) (progress + UPDATEINTERVAL);
                     // sleep the worker thread
                     Thread.sleep(UPDATEINTERVAL);
                 }
            }
        }).start();
    }

    private Handler mHandler = new Handler(){
         @Override
         public void handleMessage(Message message){
             // get the current progress
             int progress = message.what;
             if(progress <= SPLASHTIME){
                 progressBar.setProgress(progress);
             }
             else{
                 // finish splashActivity here & do what u want do to after splashscreen, for example
                 finish();
                 Intent intent = new Intent(SpashActivity.this, MenuActivity.class);
                 startActivity(intent);
             }
         }
     }
 }
公布 2024-12-07 22:57:33

您应该将启动屏幕时间分成等量,并按这些时间间隔发送进度更新。

例如:假设总时间被分为 50 个相等的间隔,如下

protected void onPreExecute() {
         progressbarStep = progressbarWidth/50;
         timeStep = 5000/50;
         progress = 0;

    }

    @Override
    protected Void doInBackground(Void... params) {
        try {
            while(progress < progressbarWidth){
                progress += progressbarStep;
                publishProgress(progress);
                SystemClock.sleep(timeStep);
               }
               return null;
        } catch (Exception e) {

        }
        return null;
    }

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

You should divide the splash screen time into equal amounts and send the progress update at those intervals.

For example:Lets say the total time is divided into 50 equal intervals as follows

protected void onPreExecute() {
         progressbarStep = progressbarWidth/50;
         timeStep = 5000/50;
         progress = 0;

    }

    @Override
    protected Void doInBackground(Void... params) {
        try {
            while(progress < progressbarWidth){
                progress += progressbarStep;
                publishProgress(progress);
                SystemClock.sleep(timeStep);
               }
               return null;
        } catch (Exception e) {

        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
         progressBar.setProgress(values[0]);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文