天天看點

Android(java)學習筆記72:ProgressBar的使用

1. ProgressBar使用

首先我們看例程如下:

(1) main.xml檔案如下:

1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical"
 4     android:layout_width="fill_parent"
 5     android:layout_height="fill_parent"
 6     >
 7          <TextView
 8               android:layout_width="wrap_content" 
 9            android:layout_height="wrap_content" 
10            android:text="預設進度條:"
11            />
12       <ProgressBar  
13            android:layout_width="wrap_content" 
14            android:layout_height="wrap_content" 
15            android:text="progress1" 
16                 //沒有設定style,預設樣式,中度旋轉的圓形進度條
17          />
18        <TextView
19               android:layout_width="wrap_content" 
20            android:layout_height="wrap_content" 
21            android:text="小圓形進度條:"
22            />
23       <ProgressBar  
24           android:layout_width="wrap_content" 
25           android:layout_height="wrap_content" 
26           android:text="progress3"
27           style="?android:attr/progressBarStyleSmall" 
28           />
29       <TextView
30               android:layout_width="wrap_content" 
31            android:layout_height="wrap_content" 
32            android:text="大圓形進度條:"
33            android:layout_gravity="center_vertical"
34            />
35       <ProgressBar  
36           android:layout_width="wrap_content" 
37           android:layout_height="wrap_content" 
38           android:text="progress3"
39           style="?android:attr/progressBarStyleLarge" 
40           />
41       <TextView
42               android:layout_width="wrap_content" 
43            android:layout_height="wrap_content" 
44            android:text="條形進度條:"
45            android:id="@+id/tv"
46            />
47       <ProgressBar  
48            android:layout_width="fill_parent" 
49          android:layout_height="wrap_content" 
50          android:text="progress2"
51          android:id="@+id/porb"
52           style="?android:attr/progressBarStyleHorizontal"
53           android:max="100"
54           android:progress="50"
55           android:secondaryProgress="70"
56           /> 
57 </LinearLayout>      

(2)接下來是MainActivity.java檔案:

1 package com.progressbar;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.widget.ProgressBar;
 6 //使用Runnable接口
 7 public class MainActivity extends Activity implements Runnable {
 8     private Thread th ;            //聲明一條線程
 9     private ProgressBar pb ;     //聲明一個進度條對象
10     private boolean stateChage; //辨別進度值最大最小的狀态
11     @Override
12     public void onCreate(Bundle savedInstanceState) {
13         super.onCreate(savedInstanceState);
14         setContentView(R.layout.main); 
15         //執行個體進度條對象
16         pb = (ProgressBar)findViewById(R.id.porb); 
17         th = new Thread(this);//執行個體線程對象,目前MainActivity實作了Runnable接口,這樣MainActivity本身可以作為參數執行個體化Thread()對象
18         th.start();//啟動線程
19     }
20 
21     @Override
22     public void run() {//實作Runnable接口抽象函數
23         while(true){ 
24             int current = pb.getProgress();//得到目前進度值
25             int currentMax = pb.getMax();//得到進度條的最大進度值
26             int secCurrent = pb.getSecondaryProgress();//得到底層目前進度值
27             //以下代碼實作進度值越來越大,越來越小的一個動态效果
28             if(stateChage==false){ 
29                 if(current>=currentMax){ 
30                     stateChage=true; 
31                 }else{
32                     //設定進度值
33                     pb.setProgress(current+1);
34                     //設定底層進度值
35                     pb.setSecondaryProgress(current+1);
36                 }
37             }else{
38                 if(current<=0){ 
39                     stateChage=false; 
40                 }else{
41                     pb.setProgress(current-1);
42                     pb.setSecondaryProgress(current-1);
43                 }
44             }
45 // 設定長方形進度條進度變大和變小的速率快慢
46             try {
47                 Thread.sleep(50);
48             } catch (InterruptedException e) {
49                 // TODO Auto-generated catch block
50                 e.printStackTrace();
51             }
52         }
53     }
54 }      
style="?android:attr/progressBarStyleHorizontal"  長形進度條

style="?android:attr/progressBarStyleLarge"   超大号圓形ProgressBar
      
style="?android:attr/progressBarStyleLargeInverse"  超大号圓形ProgressBar,Inverse反向轉向的      
style="?android:attr/progressBarStyleSmall" 小号圓形ProgressBar

style="?android:attr/progressBarStyleSmallTitle" 标題型圓形ProgressBar

等等還有很多

(2)setProgress和setSecondaryProgress差別:
這麼說你肯定明白:網絡視訊加載的時候有個播放進度和緩沖進度,播放進度一般可以用setProgerss來顯示,而setSecondaryProgress就是用來顯示緩沖進度的,
同樣的360的雷神引擎也是一樣,setProgress是檔案寫入本地進度,setSecondaryProgress就是檔案加載進度。還不明就自己寫一個Demo測試一遍。寫一個把複制檔案的程式,setProgress表示往流裡面讀資料,setSecondaryProgerss表示從流裡面輸出資料。