how make Progress Bar for 15 sec loading?

牧云@^-^@ 提交于 2021-02-11 12:14:54

问题


codes

progressBar = (ProgressBar) findViewById(R.id.progressBar);
            textView = (TextView) findViewById(R.id.secdelay);
            // Start long running operation in a background thread
            new Thread(new Runnable() {
                public void run() {
                    while (progressStatus < 15) {
                        progressStatus += 1;
                        // Update the progress bar and display the

                        //current value in the text view
                        handler.post(new Runnable() {
                            public void run() {
                                progressBar.setProgress(progressStatus);
                                textView.setText(progressStatus+"/"+progressBar.getMax());
                            }
                        });
                        try {
                            // Sleep for 200 milliseconds.

                            //Just to display the progress slowly
                            Thread.sleep(1500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }).start();

now its loading fine , I want to progress bar for 15ec how to do it ? hope you guys understand what meant . a 15 sec loading progress bar and I saw its jumping the animation, any way to make it smooth . thanks in advance noob question sorry just a learner


回答1:


You can set the max of the progress bar to 15secs*5 = 75 to enable updating smaller 'bites' for a smoother animation.

Then update progress by 1 each 200ms.

progressBar.setMax(75)
while (progressStatus < 75) {
    progressStatus += 1;
    // Update the progress bar and display the

    //current value in the text view
    handler.post(new Runnable() {
        public void run() {
            progressBar.setProgress(progressStatus);
            textView.setText(progressStatus+"/"+progressBar.getMax());
        }
    });
    try {
        // Sleep for 200 milliseconds.

        //Just to display the progress slowly
        Thread.sleep(200);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

Could also sleep only 100ms using a max of 150.



来源:https://stackoverflow.com/questions/23181117/how-make-progress-bar-for-15-sec-loading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!