Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

《Android 從入門到精通》第 17 篇:ProgressBar 進度條完全解析

編輯:關於 Android 程式設計

凡是需要「讓使用者等一等」的場景,幾乎都離不開進度條。本文延續《Android 從入門到精通》的系列進度,把 ProgressBar 的類別結構、常用方法與一個可直接跑起來的水平進度條範例一次講清楚。

一、走進 ProgressBar 的家族

ProgressBar 並不是一個全新的控件,它的血統非常單純:

java.lang.Object
   └── android.view.View
          └── android.widget.ProgressBar

也就是說,凡是 View 具備的繪製、測量、事件處理能力,它樣樣都有;在此之上,它只多做了一件事——把「完成度」畫成視覺化的長條或轉圈。

二、必須掌握的方法

類別中方法不少,但日常開發真正高頻使用的其實只有下面幾個:

其中 setMax() 決定「總量」,setProgress() 決定「已完成量」,兩者搭配就構成進度條的全部語義;setSecondaryProgress() 則是第二軌道,常見於緩存與實際下載進度的雙層呈現。

三、動手做一個水平進度條

這個範例做得很直觀:一個水平進度條,加上「前進/後退」以及「第二進度前進/後退」四顆按鈕,同時把數值同步到標題欄上,方便觀察變化。

1. MainActivity.java

public class MainActivity extends Activity {

    private ProgressBar progressHorizontal;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        progressHorizontal = (ProgressBar) findViewById(R.id.progress_horizontal);

        // 標題欄進度的範圍是 0 ~ 10000
        setProgress(progressHorizontal.getProgress() * 100);
        setSecondaryProgress(progressHorizontal.getSecondaryProgress() * 100);

        findViewById(R.id.increase).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                progressHorizontal.incrementProgressBy(1);
                setProgress(progressHorizontal.getProgress() * 100);
            }
        });

        findViewById(R.id.decrease).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                progressHorizontal.incrementProgressBy(-1);
                setProgress(progressHorizontal.getProgress() * 100);
            }
        });

        findViewById(R.id.increase_secondary).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                progressHorizontal.incrementSecondaryProgressBy(1);
                setSecondaryProgress(progressHorizontal.getSecondaryProgress() * 100);
            }
        });

        findViewById(R.id.decrease_secondary).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                progressHorizontal.incrementSecondaryProgressBy(-1);
                setSecondaryProgress(progressHorizontal.getSecondaryProgress() * 100);
            }
        });
    }
}

這裡的關鍵是 incrementProgressBy():它接受的正負值就是「增量」,傳 -1 即可回退,比自己讀值再寫回要乾淨得多。

2. activity_main.xml

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="30dp">

    <ProgressBar
        android:id="@+id/progress_horizontal"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="200dip"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:max="100"
        android:progress="50"
        android:secondaryProgress="75" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:text="@string/normal" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:orientation="horizontal">

        <Button
            android:id="@+id/decrease"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/decrease" />

        <Button
            android:id="@+id/increase"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/increase" />
    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:text="@string/custom" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:orientation="horizontal">

        <Button
            android:id="@+id/decrease_secondary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/decrease" />

        <Button
            android:id="@+id/increase_secondary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/increase" />
    </LinearLayout>
</LinearLayout>

這裡最容易忽略的是 style="?android:attr/progressBarStyleHorizontal"。不寫它,ProgressBar 預設會是小小的圓圈轉輪;寫上之後才會變成一條可以設定 max / progress 的水平長條。

3. strings.xml

<resources>
    <string name="app_name">ProgressBar</string>
    <string name="normal">預設進度條</string>
    <string name="custom">自訂進度條</string>
    <string name="decrease">減少</string>
    <string name="increase">增加</string>
</resources>

4. AndroidManifest.xml

<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.sweetlover.progressbar"
    android:versionCode="1"
    android:versionName="1.0">

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">

        <activity android:name="com.sweetlover.activity.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

四、小結

進度條本身沒有太多學問,真正的重點在於「誰來推進它」。範例中用按鈕手動驅動,只是為了看清每一支 API 的效果;實務上更常見的是把 ProgressBar 綁到非同步任務的 onProgressUpdate,或在 ProgressBar 的子類 SeekBar 上做使用者互動。下一節我們再往自訂樣式與動畫前進。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved