ProgressBar及其子類

ProgressBar也是一組重要的的組件,ProgressBar自己表明了進度條組件,它還派生了兩個經常使用的組件:SeekBar和RatingBar。ProgressBar及其子類在用法上十分類似,只是顯示界面有必定的區別。html

ProgressBar及其子類的繼承關係如圖所示android

 

  • 進度條(ProgressBar)的功能與用法

Android支持多種風格的進度條,經過style屬性能夠爲ProgressBar指定風格。該屬性可支持以下幾個屬性值。數組

  1. @android:style/Widget.ProgressBar.Horizontal:水平進度條。
  2. @android:style/Widget.ProgressBar.Inverse:普通大小的環形進度條。
  3. @android:style/Widget.ProgressBar.Large:大環形進度條。
  4. @android:style/Widget.ProgressBar.Laege.Inverse:大環形進度條。
  5. @android:style/Widget.ProgressBar.Laege.Small:小環形進度條。
  6. @android:style/Widget.ProgressBar.Laege.Small,Inverse:小環形進度條。

ProgressBar提供了以下方法來操做進度。dom

  1. setProgress(int):設置進度的完成百分比。
  2. incrementProgressBar(int):設置進度條的進度增長或減小。當參數爲正數時進度增長;當參數爲負數時進度減小。

下面的程序簡單示範了進度條的用法。該程序的界面佈局文件只是定義了幾個簡單的進度條,並指定style屬性爲@android:style/Widget.ProgressBar.Horizontal,即水平進度條。界面佈局文件以下。ide

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <!--定義一個大環行進度條-->
        <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@android:style/Widget.ProgressBar.Large"/>
        <!--定義一箇中等大小的環行進度條-->
        <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <!--定義一個小環行進度條-->
        <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@android:style/Widget.ProgressBar.Small"/>
    </LinearLayout>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="任務完成的進度"/>
    <!--定義一個水平進度條-->
    <ProgressBar
        android:id="@+id/bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100"
        style="@android:style/Widget.ProgressBar.Horizontal"/>
    <!--定義一個水平進度條,並改變軌道外觀-->
    <ProgressBar
        android:id="@+id/bar2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progressDrawable="@drawable/my_bar"
        style="@android:style/Widget.ProgressBar.Horizontal"/>
</LinearLayout>

 

上面的佈局文件中先定義了三個環形進度條,這種進度條沒法顯示進度,它只是顯示一個不斷旋轉的圖片,佈局文件的後面定義的兩個進度條的最大值爲100,第一個進度條的樣式爲水平進度條;第二個進度條的外觀被定義爲@drawable/my_bar,所以還須要在drawable中定義以下。佈局

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
   <!-- 定義軌道的背景 -->
   <item
      android:id="@android:id/background"
      android:drawable="@drawable/no" />
   <!-- 定義軌道上已完成部分的樣式 -->
   <item
      android:id="@android:id/progress"
      android:drawable="@drawable/ok" />
</layer-list>

 

下面的主程序用一個填充數組的任務模擬了耗時操做,並以進度條來標識任務的完成百分比。this

public class MainActivity extends AppCompatActivity {
    
    //該程序模擬填充長度爲100的數組
    private int[] data = new int[100];
    private int hasData = 0;
    //記錄ProgressBar的完成進度
    int status = 0;
    private ProgressBar bar;
    private ProgressBar bar2;
    
    static class MyHandler extends Handler {
        private WeakReference<MainActivity> activity;

        MyHandler(WeakReference<MainActivity> activity) {
            this.activity = activity;
        }

        @Override
        public void handleMessage(Message msg) {
            //表面消息是由該程序發送的
            if (msg.what == 0x1111) {
                activity.get().bar.setProgress(activity.get().status);
                activity.get().bar2.setProgress(activity.get().status);
            }
        }
    }
    
    //建立一個負責更新的進度的Handler
    MyHandler myHandler = new MyHandler(new WeakReference<>(this));

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bar = findViewById(R.id.bar);
        bar2 = findViewById(R.id.bar2);
        //啓動線程來執行任務
        new Thread(){
            @Override
            public void run() {
                while (status < 100){
                    //獲取耗時操做的完成百分比
                    status = doWork();
                    //發送消息
                    myHandler.sendEmptyMessage(0x111);
                }
            }
        }.start();
    }
    
    //模擬一個耗時的操做
    public int doWork() {
        //爲數組元素賦值
        data[hasData++] = (int)(Math.random()*100);
        try {
            Thread.sleep(100);
        }catch (InterruptedException e) {
            e.printStackTrace();
        }
        return hasData;
    }
}

 

 

上面程序中的紅色代碼用於修改進度條的完成進度。運行結果如圖:spa

 

 子類後續更新!線程

子類SeekBar用法:https://www.cnblogs.com/de1021/p/11784410.htmlcode

相關文章
相關標籤/搜索