标签:
1、activity_progressbar.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="两种进度条演示:" />
<ProgressBar
android:id="@+id/progressBar_horizontal"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:visibility="gone" />
<ProgressBar
android:id="@+id/progressBar_large"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:max="100"
android:progress="50"
android:secondaryProgress="70"
android:visibility="gone" />
<Button
android:id="@+id/btn_pb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始" />
</LinearLayout>
2、ProgressbarActivity.java
public class ProgressbarActivity extends Activity {
// 声明progressbar对象
private ProgressBar progressBar_horizontalBar;
private ProgressBar progressBar_largeBar;
private Button btn_pb;
protected static final int GUI_STOP_NOTIFIER = 0X108;
protected static final int GUI_THREADING_NOTIFIER = 0X109;
public int counter = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设置窗口模式,因为需要显示进度条在标题栏
requestWindowFeature(Window.FEATURE_PROGRESS);
setProgressBarVisibility(true);
setContentView(R.layout.activity_progressbar);
progressBar_horizontalBar = (ProgressBar) findViewById(R.id.progressBar_horizontal);
progressBar_largeBar = (ProgressBar) findViewById(R.id.progressBar_large);
btn_pb = (Button) findViewById(R.id.btn_pb);
/*
* 貌视不明确就是滚动条的当前值自动在最小到最大值之间来回移动,形成这样一个动画效果,这个只是告诉别人“我正在工作”,但不能提示工作进度到哪个阶段
* 。主要是在进行一些无法确定操作时间的任务时作为提示。而“明确”就是根据你的进度可以设置现在的进度值。
*/
progressBar_horizontalBar.setIndeterminate(false);
progressBar_largeBar.setIndeterminate(false);
btn_pb.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
// 设置progressBar为可见状态
progressBar_horizontalBar.setVisibility(View.VISIBLE);
progressBar_largeBar.setVisibility(View.VISIBLE);
// 设置progressBar的最大值
progressBar_horizontalBar.setMax(100);
// 设置ProgressBar的当前值
progressBar_horizontalBar.setProgress(0);
progressBar_largeBar.setProgress(0);
// 通过线程改变ProgressBar的值
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
counter = (i + 1) * 20;
Thread.sleep(1000);
if (i==4) {
Message m=new Message();
m.what=ProgressbarActivity.GUI_STOP_NOTIFIER;
ProgressbarActivity.this.handler.sendMessage(m);
break;
}else {
Message m=new Message();
m.what=ProgressbarActivity.GUI_THREADING_NOTIFIER;
ProgressbarActivity.this.handler.sendMessage(m);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}).start();
}
});
}
Handler handler=new Handler(){
public void handleMessage(Message msg) {
switch (msg.what) {
case ProgressbarActivity.GUI_STOP_NOTIFIER:
progressBar_horizontalBar.setVisibility(View.GONE);
progressBar_largeBar.setVisibility(View.GONE);
//到达最大值终止线程运行
Thread.currentThread().interrupt();
break;
case ProgressbarActivity.GUI_THREADING_NOTIFIER:
if (!Thread.currentThread().isInterrupted()) {
//改变进度条的值
progressBar_horizontalBar.setProgress(counter);
progressBar_largeBar.setProgress(counter);
//设置标题栏中前景的一个进度条的进度值
setProgress(counter*100);
//设置标题栏中后面的一个进度条进度值
setSecondaryProgress(counter*100);
}
break;
}
super.handleMessage(msg);
}
};
}
标签:
原文地址:http://www.cnblogs.com/Defry/p/4412918.html