Android进度条
不同的进度条显示结果:
image.png
demo xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:gravity="center_horizontal"
tools:context=".util.ProgressActivity"
android:padding="15dp">
<ProgressBar
android:id="@+id/pb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ProgressBar
android:id="@+id/pb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@android:style/Widget.ProgressBar"
android:layout_marginTop="10dp"/>
<!-- android:max="100"android:progress="10"
总共为100,目前到10 android:secondaryProgress="30"
第二个进度为30-->
<ProgressBar
android:id="@+id/pb3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_marginTop="10dp"
android:max="100"
android:progress="10"
android:secondaryProgress="30"/>
<ProgressBar
android:id="@+id/pb4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@android:style/Widget.Material.ProgressBar.Horizontal"
android:layout_marginTop="10dp"
android:max="100"
android:progress="10"
android:secondaryProgress="30"/>
</LinearLayout>
二.模拟进度条加载过程:
运行展示图:
image.png image.png
xml代码:
<Button
android:id="@+id/btn_start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="模拟进度"
android:layout_marginTop="10dp"/>
Activity代码:
package com.example.lineralayout.util;
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.Button;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.example.lineralayout.R;
public class ProgressActivity extends AppCompatActivity {
private ProgressBar mPb3;
private Button mBtnStart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_progress);
mPb3 = findViewById(R.id.pb3);
mBtnStart = findViewById(R.id.btn_start);
mBtnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
handler.sendEmptyMessage(0);
}
});
}
Handler handler = new Handler() {
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
if(mPb3.getProgress() < 100){
handler.postDelayed(runnable,500);
}else{
ToastUtil.showMsg(ProgressActivity.this,"加载完成");
}
}
};
Runnable runnable = new Runnable() {
@Override
public void run() {
mPb3.setProgress(mPb3.getProgress()+5);
handler.sendEmptyMessage(0);
}
};
}
三.自定义加载图片
image.png
自定义drawable资源:
<?xml version="1.0" encoding="utf-8"?>
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/progress"
android:pivotX="50%"
android:pivotY="50%">
</animated-rotate>
应用:
<ProgressBar
android:id="@+id/pb5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@android:style/Widget.ProgressBar"
android:indeterminateDrawable="@drawable/bg_progress"
android:layout_marginTop="10dp"/>
网友评论