Progress bar component is one of the most important components of Android. When a background program executes, the foreground progress bar dynamically shows the percentage of the program execution progress. It is a long time-consuming program that makes users feel in control of themselves and improves the program's friendliness.
Android supports several styles of progress bars. It can set the style of progress bars through the style attribute, which is as follows:
@ android:style/Widget.ProgressBar.Horizontal horizontal progress bar
@ android:style/Widget.ProgressBar.Inverse
@ android:style/Widget.ProgressBar.Large
@ android:style/Widget.ProgressBar.Large.Inverse
@ android:style/Widget.ProgressBar.Small
@ android:style/Widget.ProgressBar.Small.Inverse
At the same time, the progress bar has the following attributes in the xml file:
(android:max) Set the maximum of the progress bar
android:rpogress specifies the progress value that the progress bar has completed
Android: Progress Drawable
There are two most commonly used methods for the progress bar component: setProgress() to set the progress bar completed; increment ProgressBy () to set the progress bar increased or decreased; and when the parameter is positive, it increases and decreases the negative number.
Example operation: Next, we implement the video progress bar and the circular progress bar.
1. New project, add two progress bars in the layout, one is circular and one is horizontal.
<ProgressBar android:id="@+id/progressBar1" style="?android:attr/progressBarStyleHorizontal" android:layout_gravity="center_horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:max="100" android:layout_weight="1" /> <ProgressBar android:id="@+id/progressBar2" style="?android:attr/progressBarStyleSmall" android:layout_gravity="center_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" />
2. Get progress bars in the main activity, define a progress variable and message handling Handler class object
hp = (ProgressBar)findViewById(R.id.progressBar1); cp = (ProgressBar)findViewById(R.id.progressBar2); h = new Handler(){ @Override public void handleMessage(Message msg) { if(msg.what == 0x11){ hp.setProgress(progress); }else { Toast.makeText(MainActivity.this, "Time consuming", Toast.LENGTH_SHORT).show(); } hp.setVisibility(View.GONE); cp.setVisibility(View.GONE); } };
3. Handler class objects are instantiated by anonymous internal classes, and handler Message () methods are overridden. Update progress when time-consuming is not complete, prompt appears after time-consuming is completed
h = new Handler(){ public void handleMessage(Message msg) { if(msg.what == 0x11){ hp.setProgress(progress); }else { Toast.makeText(MainActivity.this, "Time consuming", Toast.LENGTH_SHORT).show(); } hp.setVisibility(View.GONE); cp.setVisibility(View.GONE); } }
4. Opening threads takes time, sendMessage() sends processing messages
new Thread(new Runnable() { @Override public void run() { while(true){ progress = doWork(); Message m = new Message(); if(progress <= 100){ m.what = 0x11; h.sendMessage(m); }else { m.what = 0x10; h.sendMessage(m); break; } } } private int doWork(){ progress += Math.random()*10; try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } return progress; } }).start();
Running the code, you can see the updates of the circular progress bar and the horizontal progress bar.