Android进度条有4种风格可以使用。
默认值是progressBarStyle。
设置成progressBarStyleSmall后,图标变小。 
设置成progressBarStyleLarge后,图标变大
设置成progressBarStyleHorizontal后,变成横向长方形。
进度条默认是不确定(indeterminate=true)Android进度条
进度条之所以是那个颜色,是因为你所选择的风格。那是一个固定式样并用了产生UI元素的系统主题。

下面详细介绍ProgressBar

一、说明

  在某些操作的进度中的可视指示器,为用户呈现操作的进度,还它有一个次要的进度条,用来显示中间进度,如在流媒体播放的缓冲区的进度。一个进度条也可不确定其进度。在不确定模式下,进度条显示循环动画。这种模式常用于应用程序使用任务的长度是未知的。

二、XML重要属性

    android:progressBarStyle:默认进度条样式

    android:progressBarStyleHorizontal:水平样式

三、重要方法

    getMax():返回这个进度条的范围的上限

    getProgress():返回进度

    getSecondaryProgress():返回次要进度

    incrementProgressBy(int diff):指定增加的进度

    isIndeterminate():指示进度条是否在不确定模式下

    setIndeterminate(boolean indeterminate):设置不确定模式下

    setVisibility(int v):设置该进度条是否可视

四、重要事件

    onSizeChanged(int w, int h, int oldw, int oldh):当进度值改变时引发此事件

五、实例

1.布局文件

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="wrap_content">    <ProgressBar android:id="@+id/progress_horizontal"        style="?android:attr/progressBarStyleHorizontal"        android:layout_width="200dip"        android:layout_height="wrap_content"        android:max="100"        android:progress="50"        android:secondaryProgress="75" />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="默认进度条" />            <LinearLayout        android:orientation="horizontal"        android:layout_width="match_parent"        android:layout_height="wrap_content">        <Button android:id="@+id/decrease"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="减少" />        <Button android:id="@+id/increase"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="增加" />    </LinearLayout>    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="自定义进度条" />            <LinearLayout        android:orientation="horizontal"        android:layout_width="match_parent"        android:layout_height="wrap_content">        <Button android:id="@+id/decrease_secondary"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="第二减少" />        <Button android:id="@+id/increase_secondary"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="第二增加" />    </LinearLayout></LinearLayout> 


2.Java代码

package wjq.WidgetDemo;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.Window;import android.widget.Button;import android.widget.ProgressBar;public class ProgressBarDemo extends Activity { /* (non-Javadoc)  * @see android.app.Activity#onCreate(android.os.Bundle)  */ @Override protected void onCreate(Bundle savedInstanceState) {  // TODO Auto-generated method stub  super.onCreate(savedInstanceState);     requestWindowFeature(Window.FEATURE_PROGRESS);         setContentView(R.layout.probarpage);         setProgressBarVisibility(true);                  final ProgressBar progressHorizontal = (ProgressBar) findViewById(R.id.progress_horizontal);         setProgress(progressHorizontal.getProgress() * 100);         setSecondaryProgress(progressHorizontal.getSecondaryProgress() * 100);                  Button button = (Button) findViewById(R.id.increase);         button.setOnClickListener(new Button.OnClickListener() {             public void onClick(View v) {                 progressHorizontal.incrementProgressBy(1);                 // Title progress is in range 0..10000                 setProgress(100 * progressHorizontal.getProgress());             }         });         button = (Button) findViewById(R.id.decrease);         button.setOnClickListener(new Button.OnClickListener() {             public void onClick(View v) {                 progressHorizontal.incrementProgressBy(-1);                 // Title progress is in range 0..10000                 setProgress(100 * progressHorizontal.getProgress());             }         });         button = (Button) findViewById(R.id.increase_secondary);         button.setOnClickListener(new Button.OnClickListener() {             public void onClick(View v) {                 progressHorizontal.incrementSecondaryProgressBy(1);                 // Title progress is in range 0..10000                 setSecondaryProgress(100 * progressHorizontal.getSecondaryProgress());             }         });         button = (Button) findViewById(R.id.decrease_secondary);         button.setOnClickListener(new Button.OnClickListener() {             public void onClick(View v) {                 progressHorizontal.incrementSecondaryProgressBy(-1);                 // Title progress is in range 0..10000                 setSecondaryProgress(100 * progressHorizontal.getSecondaryProgress());             }         });          }}

源码下载


自定义进度条

首先在onCreateDialog方法里创建一个ProgressDialog,如下:

//this表示该对话框是针对当前Activity的
progressDialog = new ProgressDialog(this);
//设置最大值为100
progressDialog.setMax(100);
//设置进度条风格STYLE_HORIZONTAL
progressDialog.setProgressStyle(
ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setTitle("进度条对话框");

接下来就是进度条的进度更新,因为对话框对象一直被当前Dialog保存着,所以onCreateDialog在下次调用对话框时不会在被调,所以
进度条更新操作不能在onCreateDialog里面,而是在onPrepareDialog里面进行。
首先要将进度条置为0

progressDialog.incrementProgressBy(-progressDialog.getProgress());

increamentProgressBy源码中是调用了ProgressBar的setProgress(mProgress + diff); mProgress是当前进度值,这里的diff就是-

progressDialog.getProgress(),最后结果为0,所以也可以这么用,效果和调用incrementProgressBy一样的,如下:

progressDialog.onStart();
progressDialog.setProgress(0);

值得一提的是,不能直接只用setProgress(0)来设置,这样的话,第一次弹出对话框效果可以和上面的一样,但是之后再弹出对话框的进度一直都是100。

为什么这里不能直接用setProgress(0),查看源码可以知道,

if (mHasStarted) {
mProgress.setProgress(value);
onProgressChanged();
} else {
mProgressVal = value;
}

mHasStarted第一次调用的时候是true,当进度条达到100%时会调用onStop方法(此方法为protected,无法直接调用),将mHasStarted设置为false,也就是

无法setProgress(value)了,只能通过onStart来将mHasStarted设置为true。
当点击“显示进度条对话框”按钮,这时候需要很耗时然后才显示对话框,而往往都会导致假死,如果超过5秒未响应将强制关闭,所以这里需要添加个多

线程来做进一步处理。而进度条的增加效果是很快的,所以这这里需要让大家看到效果,得停几毫秒后再继续。

为了使主线程不受影响(处理当前Activity的线程),在点击按钮且耗时操作完后需要发送消息给主线程,主线程再做相应处理。线程之间的消息传递和异

步处理是通过Handler来做处理的,相关代码如下:

new Thread() {
public void run() {
for(int i=0; i<=100; i++) {
handler.sendEmptyMessage(INCREASE);
if(progressDialog.getProgress() >= 100) {
break;
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();

Handler的处理如下:

handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch(msg.what) {
case INCREASE:
progressDialog.incrementProgressBy(1);
if(progressDialog.getProgress() >= 100) {
// progressDialog.dismiss();
}
break;
}
super.handleMessage(msg);
}
};

更多相关文章

  1. Android热插拔事件处理流程--Vold
  2. android UI事件
  3. Android的用户界面
  4. Android(安卓)调用系统摄像头
  5. Android(安卓)EditText输入框被键盘遮挡问题解决
  6. android 调用前摄像头进行拍照的方法及完整例子
  7. Android+JNI调用–文件操作
  8. 动态设置android:drawableLeft|Right|Top|Bottom
  9. Android(安卓)Studio NDK及so文件开发 以及常见错误

随机推荐

  1. r.java文件没有生成
  2. android Gallery的学习心得
  3. iOS对Android产生的负面影响
  4. Android签名教程
  5. ABI报告:iPad已经将平板接力棒“传”给了A
  6. Android开发进阶之路,你需要掌握这些关键
  7. Android用户界面详解
  8. android按需加载你的界面
  9. iOS、Android、java服务端 DES+RSA安全传
  10. SAP企业移动平台开发探索系列6 – 使用SU