ProgressBar是Android中的一个UI控件,可以用来展示进度条。通常用于表示应用程序中某个任务的进度,例如下载文件、上传数据等。
ProgressBar有多种样式可供选择,包括水平进度条、圆形进度条、不确定进度条等。可以通过设置属性来调整进度条的颜色、进度等。
以下是一个水平进度条的示例代码:
```
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progress="50"
android:max="100"
android:progressDrawable="@drawable/progress_bar_style"
android:indeterminate="false"
android:indeterminateTint="#FF4081"
android:indeterminateTintMode="src_in"
/>
```
其中,android:progress属性表示当前进度,android:max属性表示最大进度值。android:progressDrawable属性用于设置进度条的样式,可以通过自定义drawable来实现。android:indeterminate属性表示为不确定进度条,如果设置为true,则进度条会不停地循环动画,表示任务仍在进行中,无法确定进度。如果设置为false,则进度条会显示当前进度。最后两个属性android:indeterminateTint和android:indeterminateTintMode用于设置不确定进度条的颜色。
在代码中,可以通过findViewById方法获取ProgressBar对象,并调用其方法来控制进度条的进度,例如:
```
ProgressBar progressBar = findViewById(R.id.progress_bar);
progressBar.setProgress(75);
```
这将把进度条的进度设置为75%。
另外,如果需要在后台线程中更新进度条的进度,可以使用Handler和Runnable来实现。例如:
```
private ProgressBar progressBar;
private Handler handler;
// 在onCreate方法中初始化控件和Handler
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = findViewById(R.id.progress_bar);
handler = new Handler();
}
// 在后台线程中更新进度条
private void updateProgressBar() {
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i <= 100; i++) {
final int progress = i;
handler.post(new Runnable() {
@Override
public void run() {
progressBar.setProgress(progress);
}
});
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
```
这个例子中,updateProgressBar方法是在后台线程中运行的,通过循环更新进度条的进度,并使用handler.post方法在UI线程中更新ProgressBar的进度。
自定义ProgressBar代码如下
/**
* 自定义水平进度条,带显示文字进度
*/
public class MyHorizontalProgressBar extends ProgressBar {
String text;
Paint mPaint;
public MyHorizontalProgressBar(Context context) {
super(context);
}
public MyHorizontalProgressBar(Context context, AttributeSet attrs) {
super(context, attrs);
initText();
}
public MyHorizontalProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initText();
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public MyHorizontalProgressBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
initText();
}
@Override
public synchronized void setProgress(int progress) {
setProgressText(progress);
super.setProgress(progress);
}
@Override
protected synchronized void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (!TextUtils.isEmpty(text)) {
Rect rect = new Rect();
this.mPaint.getTextBounds(this.text, 0, this.text.length(), rect);
int x = (getWidth() / 2) - rect.centerX();
int y = (getHeight() / 2) - rect.centerY();
canvas.drawText(this.text, x, y, this.mPaint);
}
}
//初始化
private void initText() {
this.mPaint = new Paint();
this.mPaint.setTextSize(23);
this.mPaint.setColor(Color.parseColor("#FFFFFF"));
}
//设置文字
private void setProgressText() {
setProgressText(this.getProgress());
}
//设置文字内容
private void setProgressText(int progress) {
int i = (progress * 100) / this.getMax();
this.text = i + "%";
}
}
资源文件
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 进度条背景色 -->
<item android:id="@android:id/background">
<shape>
<corners android:radius="6dp" />
<gradient
android:angle="0"
android:centerColor="#FFA8A8"
android:centerX="0.75"
android:endColor="#FFA8A8"
android:startColor="#FFA8A8" />
</shape>
</item>
<!-- 第二进度条 -->
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="6dp" />
<gradient
android:angle="0"
android:centerColor="#ff5252"
android:centerX="0.75"
android:endColor="#ff5252"
android:startColor="#ff5252" />
</shape>
</clip>
</item>
<!-- 第二进度条-->
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="6dp" />
<gradient
android:angle="0"
android:centerColor="#ff5252"
android:centerX="0.75"
android:endColor="#ff5252"
android:startColor="#ff5252" />
</shape>
</clip>
</item>
</layer-list>
使用方式
<?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:gravity="center"
tools:context=".activity.TestActivity">
<com.shangjiafabu.app.view.MyHorizontalProgressBar
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="120dp"
android:layout_height="12dp"
android:max="100"
android:progress="50"
android:progressDrawable="@drawable/progress_bar" />
</LinearLayout>