Article source: www.cnblogs.com/wt616/archi…
Study Purpose:
How to build progressBars on Android
2. Master the common properties of ProgressBar
3. Control the UI thread in another thread
Introduction:
Progress bar is a dynamic control on behalf of the running state of the program, generally it is not controlled by the user, its purpose is to tell the user: please wait!
Progress bars are classified into two types:
The first is not able to tell the user the specific progress, but can always be in a scrolling state, suitable for the occasion of unknown end time.
The second one can tell the user the specific progress. When it reaches 100%, all background tasks need to be processed.
You need to understand a few things:
1, the progress bar is only a control to reflect the progress, the real background processing should be carried out in another thread.
2. The design of the progress bar reflecting the actual progress should be as accurate and smooth as possible.
3. When the progress bar reaches 100%, the task must be completed or the user will think they have been cheated.
The sample implements two progress bars, which you’ll learn how to control in another thread. This is what you must do when you download a lot of data and don’t want the interface to freeze:
The first circular, constantly rotating progress bar is only suitable for operations where the end time is unclear, or where you can be sure it will be completed immediately.
The second progress bar has the primary and secondary progress, and you can assign values to them according to actual needs. The TextView on the right tells the user the value of the actual progress, otherwise the difference between 99% and 100% will be hard to detect.
The buttons below verify that we can asynchronously download data while using other controls on the interface.
XML layout:
1 <? The XML version = "1.0" encoding = "utf-8"? > 2 < LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android" 3 android:orientation ="vertical" 4 android:layout_width ="fill_parent" 5 android:layout_height ="fill_parent" 6 > 7 < ProgressBar 8 android:layout_width ="wrap_content" 9 android:layout_height ="wrap_content" 10 ></ ProgressBar > 11 12 < LinearLayout 13 android:orientation ="horizontal" 14 android:layout_width ="fill_parent" 15 android:layout_height ="wrap_content" 16 > 17 < ProgressBar 18 style ="?android:attr/progressBarStyleHorizontal" 19 android:id = "@+id/pb" 20 android:layout_width ="170px" 21 android:layout_height ="wrap_content" 22 ></ ProgressBar > 23 < TextView 24 android:id ="@+id/tvPersent" 25 android:paddingLeft ="10px" 26 android:layout_width ="wrap_content" 27 android:layout_height ="wrap_content" 28 > 29 </ TextView > 30 </ LinearLayout > 31 < Button 32 Android :text =" TextView > 30 </ LinearLayout > 31 < Button 32 Android :text =" 33 Android :layout_width ="wrap_content android:layout_height ="wrap_content" 35 ></ Button > 36 </ LinearLayout >Copy the code
Background code:
1 // ProgressBar 2 ProgressBar pb = null; TextView tvPersent = null; 5 /* * Called when the activity is first created. */ 6 @Override 7 public void onCreate(Bundle savedInstanceState) { 8 super.onCreate(savedInstanceState); 9 setContentView(R.layout.main); 10 11 pb = (ProgressBar) this .findViewById(R.id.pb); 12 tvPersent = (TextView) this .findViewById(R.id.tvPersent); Thread Thread = new Thread(myRun); myRun = new Thread(myRun); 17 Thread.start (); Handler Handler = new Handler(new Callback() {22 23 @override 24 Public Boolean handleMessage(Message arg0) {public Boolean handleMessage(Message arg0) {25 26 // TODO auto-generated method stub 27 pb.setProgress(arg0.arg1); 28 tvPersent.setText(arg0.arg1 + " % " ); 29 pb.setSecondaryProgress(arg0.arg1 + 10 ); 30 return false ; 31} 32}); 35 Runnable myRun = new Runnable() {36 int progressValue = 0; 37 @override 38 public void run() {39 while (true){40 // TODO auto-generated method Stub 41 // Obtain the Message of the main thread Handler 42 Message msg = handler.obtainMessage(); 44 msg.arg1 = progressValue ++; 46 Handler. SendMessage (MSG); 48 if (progressValue > 100){49 progressValue = 0; Thread.sleep(300); thread.sleep (300); thread.sleep (300); 54 } 55 catch (InterruptedException e){ 56 e.printStackTrace(); 57} 58} 59} 60};Copy the code
Handler
Android controls are not thread-safe, and controls that operate on the main thread in other threads can cause accidents, so designers want to leave control operations to the main thread, but in other threads, we want to have a mechanism to tell the main thread to change the control, so Handler was born. This is a bit like delegation in C#, but since Java functions can’t themselves be arguments to other functions, we can only tell the function in the main thread how to manipulate a control by sending a message.
The Handler object also runs on a thread, and the default empty constructor Handler will run on the thread that created it. That is, the Handler in the sample code is an object that runs on the main thread. It has a callback function that accepts the message, and it can handle some logic here because it is running on the main thread, so it can safely manipulate the control. On the other hand, we can intercept the Handler’s Message, and it will send it to a postman, and we will tell him, “Please deliver it!” The letter contains what you want the recipient to do. The postman doesn’t need to know the specific task. When the mailman finally delivers the letter, the recipient on the main route opens the envelope, sees “50” and immediately understands: The sender wants me to change the schedule to 50%.
If the text is too boring, take a look at vivid pictures:
Conclusion:
This article explains how to use the ProgressBar on Android, describes how to manipulate UI thread controls on other threads, which is a must for Android development, and finally analyzes the inner workings of the Handler.