1.使用progressbar组件创建进度条
android:max 用于设置进度条最大值
android:progress 用于设置进度条以完成的进度
android:progressDrawable 用于设置进度条轨道的绘制形式
setprogress()方法:用于设置进度完成百分比
incrementprogressby()方法:用于设置进度条的进度增加或减少
?android:attr/progressBarStyleHorizontal 细水平长条进度条
?android:attr/progressBarStyleLarge 大圆形进度条
?android:attr/progressBarStyleSmall 小圆形进度条
@android:style/Widget.ProgressBar.Large 大跳跃、旋转画面的进度条
@android:style/Widget.ProgressBar.Small 小跳跃、旋转画面的进度条
@android:style/Widget.ProgressBar.Horpzontal 粗水平长度进度条
布局代码:
<ProgressBar
android:id="@+id/progressBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleLarge"/>
<ProgressBar
android:id="@+id/progressBar2"
style="?android:attr/progressBarStyleHorizontal"
android:max="100"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ProgressBar
android:id="@+id/progressBar3"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ProgressBar
android:id="@+id/progressBar4"
style="@android:style/Widget.ProgressBar.Large"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ProgressBar
android:id="@+id/progressBar5"
style="@android:style/Widget.ProgressBar.Small"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ProgressBar
android:id="@+id/progressBar6"
style="@android:style/Widget.ProgressBar.Horizontal"
android:max="100"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
实例化Handler类的对象,重写handleMessage()方法:
horizonP=(ProgressBar)findViewById(R.id.progressBar2);
circleP=(ProgressBar)findViewById(R.id.progressBar1);
mHandler=new Handler(){
@Override
public void handleMessage(Message msg) {
if (msg.what==0x111) {
horizonP.setProgress(mProgressStatus);
circleP.setProgress(mProgressStatus);
}else {
Toast.makeText(MainActivity.this, "耗时操作已完成", Toast.LENGTH_SHORT).show();
horizonP.setVisibility(View.GONE);
circleP.setVisibility(View.GONE);
}
}
开启一个线程,用于模拟一个好事操作:
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
mProgressStatus = doWork();
Message m = new Message();
if (mProgressStatus<100) {
m.what=0x111;
mHandler.sendMessage(m);
}else {
m.what=0x110;
mHandler.sendMessage(m);
break;
}
}
}
private int doWork() {
mProgressStatus+=Math.random()*10;
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return mProgressStatus;
}
}).start();