Progress bar component

introduce

As can be seen from figure 5.19, the progressbar component inherits from View, while the SeekBar and RatingBar components indirectly inherit from the progressbar component. Therefore, the properties of ProgressBar also apply to SeekBar and RatingBar components.

When an application is executed in the background, the foreground interface will not have any information. At this time, the user does not know whether the program is executing and the execution progress, so it is necessary to use the progress bar to prompt the progress of program execution. In Android, there are two kinds of progress bars, one is the horizontal progress bar, and the other is the circular progress bar. For example, the progress bar in the startup page of Kaixin Xiaole (as shown in Figure 5.20) is a horizontal progress bar, while the progress bar in the garbage cleaning interface of one click cleaning master (as shown in Figure 5.21) is a circular progress bar.

Basic syntax:

<ProgressBar
 Attribute list
 >
</ProgressBar>

XML attributes

XML attributesdescribe
android:maxUsed to set the maximum value of the progress bar
android:progressUsed to specify the progress value of the progress bar completed
android:progressDrawableUsed to set the drawing form of progress bar track

The progress bar component also provides the following two common methods for operating progress:

  1. setProgress(int progress) method: used to set the percentage of progress completion
  2. getProgress() method: get the value of the current progress bar
  3. incrementProgressBy(int diff) method: used to set the progress increase or decrease of the progress bar. When the parameter value is positive, it indicates that the progress increases; When the parameter value is negative, it indicates that the progress is reduced
  4. getVisibility() method: get the visual state of the current view
  5. setVisibility() method: sets the visual state of the current view

Optional value for the style property of the ProgressBar

XML attributesdescribe
?android:attr/progressBarStyleHorizontalThin horizontal bar progress bar
?android:attr/progressBarStyleLarge Large circular progress bar
?android:attr/progressBarStyleSmallSmall round progress bar
@android:style/Widget.ProgressBar.LargeProgress bar of large jump and rotation picture
@android:style/Widget.ProgressBar.SmallProgress bar of small jump and rotation screen
@android:style/Widget.ProgressBar.HorizontalThick horizontal bar progress bar

example

Simulation game progress bar

Writing layout files

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Horizontal progress bar -->
    <ProgressBar
        android:id="@+id/progressBar1"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:layout_width="match_parent"
        android:layout_height="25dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="60dp"
        android:max="100" />

</RelativeLayout>

Write ProgressBarActivity2

public class ProgressBarActivity2 extends AppCompatActivity {

    private ProgressBar horizonP; //Horizontal progress bar
    private int mProgressStatus = 0; //Completion schedule
    private Handler mHandler; //Declare an object of the Handler class used to process messages

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_progress_bar2);
        horizonP = (ProgressBar) findViewById(R.id.progressBar1); //Get horizontal progress bar
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == 0x111) {
                    horizonP.setProgress(mProgressStatus); //Update progress
                } else {
                    Toast.makeText(ProgressBarActivity2.this,
                            "Time consuming operation completed", Toast.LENGTH_SHORT).show();
                    horizonP.setVisibility(View.GONE);//Set that the progress bar is not displayed and does not occupy space
                }
            }
        };
        new Thread(new Runnable() {
            public void run() {
                while (true) { //Loop gets the percentage of time-consuming operations completed until the end of the time-consuming operation
                    mProgressStatus = doWork(); //Gets the percentage of time-consuming operations completed
                    Message m = new Message(); //Create and instantiate a message object
                    if (mProgressStatus < 100) { //When the completion progress is less than 100, it indicates that the time-consuming task has not been completed
                        m.what = 0x111; //Set the message code representing the incomplete time-consuming operation
                        mHandler.sendMessage(m); //Send message
                    } else { //When the completion progress reaches 100, it indicates that the time-consuming operation is completed
                        m.what = 0x110; //Set the message code that represents the completion of the time-consuming operation
                        mHandler.sendMessage(m); //send message
                        break; //Exit loop
                    }
                }
            }
            //Simulate a time-consuming operation
            private int doWork() {
                //Math.random() is a pseudo-random double value that makes the system randomly select greater than or equal to 0.0 and less than 1.0
                mProgressStatus += Math.random() * 10;//Change completion schedule
                try {
                    Thread.sleep(200); //Thread sleep 200 ms
                } catch (InterruptedException e) {
                    e.printStackTrace(); //Output exception information
                }
                return mProgressStatus; //Return to new progress
            }
        }).start(); //Start a thread
    }
}

Added by countcet on Wed, 22 Dec 2021 14:25:37 +0200