Reproduced in the source, Lee http://blog.csdn.net/hnulwt/article/details/44462177
Today, I saw a discussion about the slow progress of the bar. The actual problem is this: the background execution of the program is very fast, but the interface to feel the progress bar is slow to go. To do this, a small experiment, the code experiment in 1 seconds in two ways to increase the progress bar to 100% (every 10ms increase of 1% and every 250ms increase of 25%), to verify which way to feel fast, but after the demo has been made, found a new problem, I would like to discuss with you.
Problem
According to the code, I expected the result is that the two progress bar increases the same speed, but after my test, click the same time Add button, jump to increase the progress bar will always be faster than the incremental progress bar.
The interface is as follows (source and layout files are in the back):
Analysis:
Through the handler mechanism to update the progress, after clicking the "Add simultaneously" button, post two Runnable objects to the message queue respectively, and then execute the runnable inside the Run method, after the progress value increases, send a message to the queue, Last Handlemessage, update the progress bar, and then post the Runnable object separately ... So loop until the progress bar is full.
The present conclusion
By testing the "incremental increase" "Jump increase" and "increase" the traceview of the three execution process, the results do not see any difference. Read the relevant source code, think in the process of time-consuming operation is this lock it. Since every 10ms increase of 1% sends the message too frequently, the interface updates also frequently, may cause the gradual increase the speed to be slow. I do not know how the great God to see this problem, I hope that everyone's generous enlighten.
Before the source code to say a word, online to find some of the use of handler update progress bar codes are problematic. He removecallbacks before the post of runnable, resulting in remove the last runnable after adding a new runnable, so that infinite cycle down, occupy resources.
Demo
Mainactivity.java
Packagecom.example.progress;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.os.Message;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.ProgressBar; Public class mainactivity extends Activity implements Onclicklistener { Private Static Final intPercent_total = -; ProgressBar mprogressfluency, Mprogressjumpy;intPercentfluency =0;intPercentjumpy =0;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); Mprogressfluency = (ProgressBar) Findviewbyid (R.ID.PROGRESSBAR1); Mprogressjumpy = (ProgressBar) Findviewbyid (R.ID.PROGRESSBAR2); (Button) Findviewbyid (r.id.button_fluency)). Setonclicklistener ( This); (Button) Findviewbyid (R.id.button_jumpy)). Setonclicklistener ( This); (Button) Findviewbyid (R.id.button_reset)). Setonclicklistener ( This); (Button) Findviewbyid (r.id.button_syn_increase)). Setonclicklistener ( This); }FinalHandler Mhandler =NewHandler () { Public void Handlemessage(Message msg) {Super. Handlemessage (msg);Switch(msg.what) { CaseWhat.FLUENCY:mProgressFluency.setProgress (MSG.ARG1); Mhandler.post (mupdateprogressfluency); Break; CaseWhat.JUMPY:mProgressJumpy.setProgress (MSG.ARG1); Mhandler.post (Mupdateprogressjumpy); Break;default: Break; } } };StaticClass What {Final Static intFluency =1;Final Static intJumpy =2; }FinalRunnable mupdateprogressfluency =NewRunnable () {Final intEVERYTIMEADDF =1;Final intDelay = +/ ( -/EVERYTIMEADDF);@Override Public void Run() {percentfluency + = EVERYTIMEADDF;if(Percentfluency > Percent_total) {mhandler.removecallbacks (mupdateprogressfluency); }Else{Message msg = Mhandler.obtainmessage (); MSG.ARG1 = percentfluency; Msg.what = what.fluency; Mhandler.sendmessagedelayed (msg, delay); } } };Final intEverytimeadd = -;Final intDelay = +/ ( -/Everytimeadd);FinalRunnable Mupdateprogressjumpy =NewRunnable () {@Override Public void Run() {percentjumpy + = Everytimeadd;if(Percentjumpy > Percent_total) {mhandler.removecallbacks (mupdateprogressjumpy); }Else{Message msg = Mhandler.obtainmessage (); MSG.ARG1 = Percentjumpy; Msg.what = What.jumpy; Mhandler.sendmessagedelayed (msg, delay); } } };@Override Public void OnClick(View v) {Switch(V.getid ()) { CaseR.id.button_fluency:percentfluency =0; Mprogressfluency.setvisibility (view.visible); Mhandler.post (mupdateprogressfluency); Break; CaseR.id.button_jumpy:percentjumpy =0; Mprogressjumpy.setvisibility (view.visible); Mhandler.post (Mupdateprogressjumpy); Break; CaseR.id.button_reset:mhandler.removecallbacks (mupdateprogressfluency); Mprogressfluency.setprogress (0); Mhandler.removecallbacks (Mupdateprogressjumpy); Mprogressjumpy.setprogress (0); Break; CaseR.id.button_syn_increase:percentfluency =0; Mprogressfluency.setvisibility (view.visible); Mhandler.post (mupdateprogressfluency); Percentjumpy =0; Mprogressjumpy.setvisibility (view.visible); Mhandler.postdelayed (mupdateprogressjumpy, delay); Break;default: Break; } }}
Layout file
<LinearLayout xmlns:android="Http://schemas.android.com/apk/res/android" Xmlns:tools="Http://schemas.android.com/tools" Android:id="@+id/linearlayout1" Android:layout_width="Match_parent" Android:layout_height="Match_parent" android:orientation="Vertical" Android:paddingbottom="@dimen/activity_vertical_margin" Android:paddingleft="@dimen/activity_horizontal_margin" Android:paddingright="@dimen/activity_horizontal_margin" Android:paddingtop="@dimen/activity_vertical_margin" Tools:context="Cn.wps.progress.MainActivity"> <linearlayoutandroid:layout_width="Match_parent"android:layout_height ="Wrap_content"android:orientation="Horizontal" > <progressbar android:id< /span>= "@+id/progressbar1" style = android:layout_wi DTH = "match_parent" android:layout_height= "wrap_content" android:layout_weight =< Span class= "Hljs-value" > "1" /> <buttonandroid:id= "@+id/button_fluency"android:layout_width=" Match_parent "android:layout_height=" Wrap_content "android:layout_weight=" 3 " Android:text="incremental increase" /> </linearlayout> <linearlayout android: Layout_width = "match_parent" android:layout_ Height = "wrap_content" android:orientation
= "horizontal" ; <progressbar android:id< /span>= "@+id/progressbar2" style = android:layout_wi DTH = "match_parent" android:layout_weight= "1" android:layout_height = "wrap_content" /> <buttonandroid:id= "@+id/button_jumpy"android:layout_width=" Match_parent "android:layout_height=" Wrap_content "android:layout_weight=" 3 " Android:text="Jump increase" /> </linearlayout> <button android:id = "@+id/button_syn_increase" android:layout_ Width = "wrap_content" android:layout_height< /span>= "wrap_content" android:text =/> <buttonandroid:id="@+id/button_reset"android:layout_width="Wrap _content "android:layout_height=" Wrap_content "android:text=" reset " /> </linearlayout>
Android Progress progress bar code, progress bar Refresh problem Discussion