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

Android ProgressBar 客製化:旋轉動畫與自訂水平進度條

編輯:關於 Android 程式設計

ProgressBar 是少數「預設樣式很醜、但客製化很簡單」的元件。本文示範兩種常見需求:換掉那個轉圈圈的小動畫,以及做出一條自訂配色的水平進度條。

一、自訂旋轉類型的 ProgressBar

步驟 1:在 drawable 資料夾中建立一個旋轉動畫描述檔(例如 progressbg.xml):

<?xml version="1.0" encoding="utf-8"?>
<animated-rotate xmlns:android="https://schemas.android.com/apk/res/android"
    android:drawable="@drawable/loading1"
    android:pivotX="50%"
    android:pivotY="50%" />

其中 loading1 就是你想使用的那張圖片。pivotX 與 pivotY 設為 50% 代表以圖片中心為旋轉軸心——這個設定很關鍵,若填錯會變成偏心旋轉,畫起來像陀螺而非載入動畫。

步驟 2:在版面配置中套用:

<ProgressBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:indeterminateDrawable="@drawable/progressbg"
    android:indeterminateOnly="true"
    android:indeterminateBehavior="repeat" />

這三個 indeterminate 相關屬性的意思是:「使用我指定的圖片」、「只在不確定進度時顯示」、「播放完畢後重複」。不確定進度模式適合用於不知道總量的等待情境,例如登月中。

二、自訂水平的 ProgressBar

水平進度條的做法是用 layer-list 把「背景軌道」與「進度填充」疊起來。

步驟 1:建立 drawable/progressbarcolor.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="https://schemas.android.com/apk/res/android">
    <item
        android:id="@android:id/progress"
        android:drawable="@drawable/common_button_red" />
</layer-list>

這裡的 android:id="@android:id/progress" 是系統約定不可改的識別碼,ProgressBar 就是靠它找到「進度部分」該畫在哪。少了這行,進度永遠不會顯示。

步驟 2:建立軌道背景 drawable/progressshape.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <corners android:radius="10dp" />
    <solid android:color="#ffffff" />
</shape>

用 shape 定義一個白色圓角矩形當底。corners 的 radius 決定圓角大小,通常設為高度的一半能得到膠囊狀外觀。

步驟 3:版面配置:

<ProgressBar
    android:id="@+id/pb"
    android:background="@drawable/progressshape"
    android:layout_width="wrap_content"
    android:layout_height="10dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_weight="1"
    android:indeterminateOnly="false"
    android:max="100"
    android:progressDrawable="@drawable/progressbarcolor" />

幾個屬性值得說明:android:max="100" 設定進度上限;android:indeterminateOnly="false" 表示這是有確定進度的模式;progressDrawable 指向剛才的 layer-list,background 則是軌道。程式碼中只要呼叫 progressBar.setProgress(n) 就能更新。

更完整的做法:用 shape 同時定義背景與進度

上面的寫法把填充色寫死在 drawable 裡,若需要動態換色會很麻煩。實務上更靈活的做法是在 layer-list 中同時定義 @android:id/background、@android:id/secondaryProgress 與 @android:id/progress 三層:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="https://schemas.android.com/apk/res/android">
    <!-- 軌道 -->
    <item android:id="@android:id/background">
        <shape>
            <corners android:radius="5dp" />
            <solid android:color="#E0E0E0" />
        </shape>
    </item>
    <!-- 次要進度,可用於顯示緩衝進度 -->
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <corners android:radius="5dp" />
                <solid android:color="#B0BEC5" />
            </shape>
        </clip>
    </item>
    <!-- 主要進度 -->
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="5dp" />
                <solid android:color="#3F51B5" />
            </shape>
        </clip>
    </item>
</layer-list>

<clip> 這個標籤是重點:它會依目前進度裁切子項目的顯示寬度。少了 clip,進度層會永遠全寬顯示,滑桿動都不動。這也是最多人卡住的地方。

補充:想要漸層或更花俏的效果

把 <solid> 換成 <gradient> 就能做漸層:

<gradient
    android:startColor="#FF6F00"
    android:endColor="#FFCA28"
    android:angle="0" />

angle 必須是 45 的倍數,0 代表由左至右。若想做出「進度條上有流動光澤」的效果,則需要自訂 Drawable 或用 ObjectAnimator 搭配 LinearGradient 的 setTileMode 來移動。

最後提醒一個實務細節:ProgressBar 預設在 4.0 以下沒有動畫過渡,進度會一格一格跳。若想要平滑的視覺效果,可以用 ValueAnimator 對進度值做插值動畫,再逐幀呼叫 setProgress。另外,若進度條只是裝飾用途(例如不確定實際進度),優先考慮不確定模式的旋轉圖示,因為它不需要持續更新數值,效能負擔更小。

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