图1.UC浏览网页圆形进度条效果。
图2.Google Market应用下载长形进度条效果.
下面我们看一下两都皆有之的Android自带的浏览器的效果图如下:
为了让大家更容易理解我将简单写一个Demo,大家可以按照我的步骤一步一步来。
第一步:新建一个Android工程命名为ProgressBarDemo.
第二步:修改main.xml代码如下(圆形进度条和长形进度条这里样式不同用系统自带的):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Welcome to Mr Wei's blog" />
<ProgressBar
android:id="@+id/rectangleProgressBar"
style="?android:attr/progressBarStyleHorizontal" mce_style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone"
/>
<ProgressBar
android:id="@+id/circleProgressBar"
style="?android:attr/progressBarStyleLarge" mce_style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
/>
<Button android:id="@+id/button"
android:text="Show ProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
第三步:修改ProgressBarDemo.java代码如下(如果这里有对Handler和Message不明白的地方可以去Android高 手进阶教程(九) 看下这个教程):
[c-sharp] view plaincopy
package com.tutor.progress;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
public class ProgressBarDemo extends Activity {
private ProgressBar rectangleProgressBar,circleProgressBar;
private Button mButton;
protected static final int STOP = 0x10000;
protected static final int NEXT = 0x10001;
private int iCount = 0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//findView By id
rectangleProgressBar = (ProgressBar)findViewById(R.id.rectangleProgressBar);
circleProgressBar = (ProgressBar)findViewById(R.id.circleProgressBar);
mButton = (Button)findViewById(R.id.button);
rectangleProgressBar.setIndeterminate(false);
circleProgressBar.setIndeterminate(false);
mButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
rectangleProgressBar.setVisibility(View.VISIBLE);
circleProgressBar.setVisibility(View.VISIBLE);
rectangleProgressBar.setMax(100);
rectangleProgressBar.setProgress(0);
circleProgressBar.setProgress(0);
//创建一个线程,每秒步长为5增加,到100%时停止
Thread mThread = new Thread(new Runnable() {
public void run() {
for(int i=0 ; i < 20; i++){
try{
iCount = (i + 1) * 5;
Thread.sleep(1000);
if(i == 19){
Message msg = new Message();
msg.what = STOP;
mHandler.sendMessage(msg);
break;
}else{
Message msg = new Message();
msg.what = NEXT;
mHandler.sendMessage(msg);
}
}catch (Exception e) {
e.printStackTrace();
}
}
}
});
mThread.start();
}
});
}
//定义一个Handler
private Handler mHandler = new Handler(){
public void handleMessage(Message msg){
switch (msg.what) {
case STOP:
rectangleProgressBar.setVisibility(View.GONE);
circleProgressBar.setVisibility(View.GONE);
Thread.currentThread().interrupt();
break;
case NEXT:
if(!Thread.currentThread().isInterrupted()){
rectangleProgressBar.setProgress(iCount);
circleProgressBar.setProgress(iCount);
}
break;
}
}
};
}
第四步:运行上述工程,效果如下图:
没有评论:
发表评论