当应用程序在进行一些耗时操作时(如网络下载,图片资源的加载等),应用程序会处于一个“等待”的状态,在这个时候如果不通知用户程序正在运行中,用户可能认为程序“未响应”而关闭程序,这样的用户体验显然非常糟糕。因此我们可以用一个进度条让用户知道当前程序的进度信息,提高用户体验。
Android提供的进度条控件一般以两种样式显示,一种是环形样式,一种是线性样式,可以通过“style”属性来设置样式。此外进度条有“确定”和“不确定”两种,前者可以精确的表示操作执行的进度;后者只能表示操作正在执行,可以通过“android:indeterminate”属性来设置。“确定”的进度条有两个进度,一个是主进度(可表示操作执行的重进度),一个是第二进度(可用来表示缓存进度,如在线看视频的进度条等)
XML常用属性:
android:indeterminate 是否设置为不精确模式
android:progressDrawable 用来设置外观样式
android:max 设置最大进度值
android:progress 设置默认第一进度值
android:secondaryProgress 设置默认第二进度值
style="?android:attr/progressBarStyleHorizontal" 设置进度条显示样式为线性
常用方法:
getMax():返回这个进度条的范围的上限
getProgress():返回进度
getSecondaryProgress():返回次要进度
incrementProgressBy(int diff):指定增加的进度
isIndeterminate():指示进度条是否在不确定模式下
setIndeterminate(boolean indeterminate):设置不确定模式下
setVisibility(int v):设置该进度条是否可视
onSizeChanged(int w, int h, int oldw, int oldh):当进度值改变时引发此事件
效果预览:

1.布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progressDrawable="@drawable/progress_bar"
android:id="@+id/progressBar" />
<TextView
android:id="@+id/textView"
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/but_add"
android:text="增加"
android:onClick="onClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:id="@+id/but_lost"
android:text="减少"
android:onClick="onClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:id="@+id/but_reset"
android:text="复位"
android:onClick="onClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
其中andrpod:progressDrawable 用来设置进度条外观样式,在这里引用了drawable文件夹下的progress_bar.xml文件,其代码如下:
<?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="5dip" />
<solid android:color="#88000000"/>
</shape>
</item>
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:angle="270"
android:centerColor="#C6B7FF"
android:centerY="0.75"
android:endColor="#C3B2FF"
android:startColor="#B9A4FF" />
</shape>
</clip>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:angle="270"
android:centerColor="#74EBFF"
android:centerY="0.75"
android:endColor="#8EEFFF"
android:startColor="#57E8FF" />
</shape>
</clip>
</item>
</layer-list>
初学者不用关心该xml文件中各个属性的含义,只要知道通过andrpod:progressDrawable来改变进度条外观进行了
2.MainActivity类:
package com.example.lowp.progressbar_test;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private ProgressBar bar;
private TextView textView;
private Button but_add;
private Button but_lost;
private Button but_reset;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bar = (ProgressBar) findViewById(R.id.progressBar);
textView = (TextView) findViewById(R.id.textView);
but_add = (Button) findViewById(R.id.but_add);
but_lost = (Button) findViewById(R.id.but_lost);
but_reset = (Button) findViewById(R.id.but_reset);
bar.setMax(100);
bar.setProgress(20);
bar.setSecondaryProgress(30);
textView.setText("fristProgress : " + (int) ((bar.getProgress() / (float) bar.getMax()) * 100)
+ "% secondProgress : " + (int) ((bar.getSecondaryProgress() / (float) bar.getMax()) * 100)
+ "%");
}
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.but_add:
bar.incrementProgressBy(10);
bar.incrementSecondaryProgressBy(15);
break;
case R.id.but_lost:
bar.incrementProgressBy(-10);
bar.incrementSecondaryProgressBy(-15);
break;
case R.id.but_reset:
bar.setProgress(20);
bar.setSecondaryProgress(30);
break;
}
textView.setText("fristProgress : " + (int) ((bar.getProgress() / (float) bar.getMax()) * 100)
+ "% secondProgress : " + (int) ((bar.getSecondaryProgress() / (float) bar.getMax()) * 100)
+ "%");
}
}