前言:今天有时间复习一下进度条,顺便总结一下
一:为什么使用ProgressBar进度条的主要作用简言之就是避免应用在执行某个耗时操作时,让用户感觉程序失去了响应,从而更好的提高用户界面的友好性。
二:Android提供的ProgressBar种类及样式根据下面的xml文件所对应的顺序,Android支持的进度条style属性有
1.@android:style/Widget.ProgressBar 普通大小的环形进度条
2.@android:style/Widget.ProgressBar.Horizontal 水平进度条
3.@android:style/Widget.ProgressBar.Inverse 普通大小的环形进度条
4.@android:style/Widget.ProgressBar.Large 大环形进度条
5.@android:style/Widget.ProgressBar.Large.Inverse 大环形进度条
6.@android:style/Widget.ProgressBar.Small 小环形进度条
7.@android:style/Widget.ProgressBar.Small.Inverse 小环形进度条
代码如下:
<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:gravity="center_horizontal"
android:orientation="vertical"
tools:context="com.example.androidbuttondemo.MainActivity" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ProgressBar
style="@android:style/Widget.ProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" />
<ProgressBar
style="@android:style/Widget.ProgressBar.Horizontal"
android:backgroundTint="#ff0000"
android:background="@drawable/ic_launcher"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:progress="50" />
<ProgressBar
style="@android:style/Widget.ProgressBar.Inverse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" />
<ProgressBar
style="@android:style/Widget.ProgressBar.Large"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:progress="100" />
<ProgressBar
style="@android:style/Widget.ProgressBar.Large.Inverse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" />
<ProgressBar
style="@android:style/Widget.ProgressBar.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" />
<ProgressBar
style="@android:style/Widget.ProgressBar.Small.Inverse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" />
</LinearLayout>
</ScrollView>
</LinearLayout>
显示的样式为:
注意:ProgressBar有一个属性 android:progress="" ,在引号内可以设置默认显示的进度百分比,是一个0-100的值,水平进度条设置有效 android:progress="50" ,显示效果如下:
而对环形进度条是无法显示进度的,它只是显示一个不停旋转的图片。
三:动态的改变进度条进度
在这里使用线程模拟了一个下载任务,使用进度条显示下载进度
public class ProgressBarMainActivity extends Activity {
ProgressBar shuipingPB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_progress_bar_main);
initView();
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 1; i <= 100; i++) {
Message msg = new Message();
msg.what = 1100;
msg.obj = i;
handler.sendMessage(msg);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
// Handler用于线程间通信
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 1100) {
shuipingPB.setProgress(((int) msg.obj));
}
}
};
private void initView() {
shuipingPB = (ProgressBar) findViewById(R.id.shuipingPB);
}
}
四:自定义ProgressBar
ProgressBar有两个进度,一个是android:progress,另一个是android:secondaryProgress。后者主要是为缓存需要所涉及的,比如在看网络视频时候都会有一个缓存的进度条以及还要一个播放的进度,在这里缓存的进度就可以是android:secondaryProgress,而播放进度就是android:progress,有了secondProgress,可以很方便定制ProgressBar。