import android.app.Activity;
import android.os.Bundle;
import android.widget.ProgressBar;
//使用Runnable接口
public class MainActivity extends Activity implements Runnable {
private Thread th ; //声明一条线程
private ProgressBar pb ; //声明一个进度条对象
private boolean stateChage; //标识进度值最大最小的状态
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//实例进度条对象
pb = (ProgressBar)findViewById(R.id.porb);
th = new Thread(this);//实例线程对象
th.start();//启动线程
}
@Override
public void run() {//实现Runnable接口抽象函数
while(true){
int current = pb.getProgress();//得到当前进度值
int currentMax = pb.getMax();//得到进度条的最大进度值
int secCurrent = pb.getSecondaryProgress();//得到底层当前进度值
//以下代码实现进度值越来越大,越来越小的一个动态效果
if(stateChage==false){
if(current>=currentMax){
stateChage=true;
}else{
//设置进度值
pb.setProgress(current+1);
//设置底层进度值
pb.setSecondaryProgress(current+1);
}
}else{
if(current<=0){
stateChage=false;
}else{
pb.setProgress(current-1);
pb.setSecondaryProgress(current-1);
}
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
<?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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="默认进度条:" />
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="progress1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="小圆形进度条:" />
<ProgressBar
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="progress3" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="大圆形进度条:" />
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="progress3" />
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="条形进度条:" />
<ProgressBar
android:id="@+id/porb"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="50"
android:secondaryProgress="70"
android:text="progress2" />
</LinearLayout>