The progress bar component is one of the important components of Android. When a program executes in the background, the foreground progress bar dynamically shows the percentage of program execution progress. It is a long-time program that allows users to feel under their control and improve the program's friendliness.
Android supports several styles of progress bars, which can be styled using the Style property, which is as follows:
@android: Style/widget.progressbar.horizontal Horizontal progress bar
@android: Style/widget.progressbar.inverse General circular progress bar
@android: Style/widget.progressbar.large Large Loop progress bar
@android: Style/widget.progressbar.large.inverse Large Loop progress bar
@android: Style/widget.progressbar.small Small Loop progress bar
@android: Style/widget.progressbar.small.inverse Small Loop progress bar
Also, the progress bar will have the following properties in the XML file:
Android:max setting the maximum value of the progress bar
ANDROID:RPOGRESS Specifies the progress value that the progress bar has completed
android:progressdrawable specifies the drawing form of a progress bar track
The progress bar component has two most common methods: Setprogress () sets the progress that the progress bar has completed, Incrementprogressby () sets the progress bar to increase or decrease, the parameter is positive, and the negative number decreases;
Example operation: Below we implement the video progress bar and the Loop progress bar
1. Create a new project, add two progress bars in the layout, one horizontal progress bar for the ring
<ProgressBarAndroid:id= "@+id/progressbar1"style= "? Android:attr/progressbarstylehorizontal"android:layout_gravity= "Center_horizontal"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:max= "+"Android:layout_weight= "1" /> <ProgressBarAndroid: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 the progress bar in the main activity, define a progress variable and message processing handler class object
HP =(ProgressBar) Findviewbyid (R.ID.PROGRESSBAR1); CP=(ProgressBar) Findviewbyid (R.ID.PROGRESSBAR2); H=NewHandler () {@Override Public voidhandlemessage (Message msg) {if(Msg.what = = 0x11) {hp.setprogress (progress); }Else{toast.maketext (mainactivity). This, "time-consuming completion", Toast.length_short). Show (); } hp.setvisibility (View.gone); Cp.setvisibility (View.gone); } };
3. Process the handler class object by instantiating the message through an anonymous inner class, overriding the Handlermessage () method. Update progress when time is not complete, prompt after time-consuming completion
New Handler () { publicvoid handlemessage (Message msg) { if( Msg.what = = 0x11) { hp.setprogress (progress); } Else { toast.maketext (mainactivity). This, "time-consuming completion", Toast.length_short). Show (); } Hp.setvisibility (view.gone); Cp.setvisibility (View.gone); } }
4. Start thread time, sendMessage () Send processing message
NewThread (NewRunnable () {@Override Public voidrun () { while(true) {Progress=doWork (); Message m=NewMessage (); if(Progress <= 100) {M.what= 0x11; H.sendmessage (m); }Else{m.what= 0x10; H.sendmessage (m); Break; } } } Private intDoWork () {Progress+ = Math.random () *10; Try{Thread.Sleep (2000); } Catch(interruptedexception e) {e.printstacktrace (); } returnprogress; }}). Start ();
Run the code and you can see the Loop progress bar and the horizontal progress bar update.
Android Advanced Components-----progress bar