scene
Progress bar effect
Note:
Blog:
https://blog.csdn.net/badao_liumang_qizhi
Pay attention to the public address
Domineering procedural ape
Get programming related ebooks, tutorials and free downloads.
Realization
Change the layout to relative, then add a ProgressBar and add the id attribute.
Then pass
android:max="100"
Set progress bar maximum
adopt
android:layout_alignParentBottom="true" android:layout_marginBottom="50dp"
Set its position at the bottom and set the outer margin
For the style of the progress bar, refer to the following
The progress bar style used here is
style="?android:attr/progressBarStyleHorizontal"
Complete xml sample code
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".ProgressBarActivity"> <ProgressBar android:layout_width="match_parent" android:layout_height="wrap_content" style="?android:attr/progressBarStyleHorizontal" android:max="100" android:layout_alignParentBottom="true" android:layout_marginBottom="50dp" android:id="@+id/progressBar" /> </RelativeLayout>
Then come to Activity
The simple process to implement the progress bar is as follows
First, declare the progress bar object, progress value and Handler object in the Activity.
About the Hnadler object, because Android does not support updating UI controls in the main thread, it provides a Handler object.
First, get the progress bar object through id, and then instantiate the Handler. You need to override its handleMessage method
In the handleMessage method, the identification of msg is used to determine whether the loading of progress bar is completed.
Then a new thread needs to be opened to add progress value to the progress bar randomly in the thread, and then judge whether it reaches 100, and then report different progress and identification variables.
Full sample code
package com.badao.relativelayouttest; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.widget.ProgressBar; import android.widget.Toast; import java.util.prefs.PreferenceChangeEvent; public class ProgressBarActivity extends AppCompatActivity { private ProgressBar progressBar; private int mProgress = 0; private Handler mHandler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_progress_bar); //Get progress bar control progressBar = (ProgressBar) findViewById(R.id.progressBar); //Android Update in main thread is not supported UI Component so provides Handler object mHandler = new Handler(){ //Via message code msg Differentiate whether loading is complete @Override public void handleMessage(@NonNull Message msg) { if(msg.what ==0x111) { //Assign value to progress bar progressBar.setProgress(mProgress); }else { Toast.makeText(ProgressBarActivity.this,"Load complete",Toast.LENGTH_SHORT).show(); progressBar.setVisibility(View.GONE); } } }; //New thread new Thread(new Runnable() { @Override public void run() { while (true) { //Progress of specific implementation method, return to progress mProgress = doWork(); //Define an ID to identify whether the progress bar has been loaded Message message = new Message(); if(mProgress <100) { //Indicates that the progress has not been loaded to 100, report the progress to the progress bar message.what = 0x111; mHandler.sendMessage(message); }else { //Indicates that the loading is completed, and the Handler Send message and jump out of loop message.what =0x110; mHandler.sendMessage(message); break; } } } //How to execute and return progress private int doWork() { //Progress bar random plus progress mProgress +=Math.random()*10; try { //Process sleep 0.5 second Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } return mProgress; } }).start(); //Startup process } }