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

Android Drawable 完整梳理:從 ColorDrawable 到 StateListDrawable

編輯:關於 Android 程式設計

在 Android 裡,凡是能畫到 Canvas 上的東西幾乎都脫離不了 Drawable。它常被直譯成「圖像」,但這個說法其實不太精準——Drawable 更像是「一組可被繪製的規則」,背後可能是一張點陣圖,也可能只是一段漸層或一個純色。理解這一點,後面那些五花八門的子類就不會讓人眼花。

Drawable 是抽象類,所有具體型別都繼承自它,例如 ShapeDrawable、BitmapDrawable。多數情況下我們用 XML 定義它,當然也可以純程式碼建立。

有兩個方法值得先記住:getIntrinsicHeight() 與 getIntrinsicWidth(),用來取得圖源的原始寬高。但要注意,並非每個 Drawable 都有「內部尺寸」——例如單純由顏色構成的 ColorDrawable 就沒有,這也是為什麼取回的值可能是 -1。

ColorDrawable:最樸素的一塊色

它包裝單一顏色,繪製時會把這抹顏色填進 Paint,畫出一塊純色區域。XML 裡以 <color> 為根節點,只有一個 android:color 屬性:

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

程式碼寫法同樣直接,習慣上使用十六進位表示顏色值:

ColorDrawable drawable = new ColorDrawable(0xffff0000);

GradientDrawable:漸層與形狀的多面手

GradientDrawable 負責各種漸層區域,線性、放射、平鋪都支援,是做出「華麗但不用切圖」介面的主力。XML 中對應的關鍵字是 <shape>,底下可以掛多個子節點:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android">
    <size />      <!-- 定義區域大小 -->
    <gradient />  <!-- 漸層效果 -->
    <solid />     <!-- 純色背景,會覆蓋 gradient -->
    <stroke />    <!-- 邊框 -->
    <padding />   <!-- 內距 -->
</shape>

實務上最常踩到的細節是:<solid> 一旦設定,<gradient> 就會失效,兩者互斥。

BitmapDrawable:替點陣圖加一層外掛

顧名思義,它是对 Bitmap 的包裝,可以額外指定抗鋸齒、抖動、平鋪模式等行為:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="https://schemas.android.com/apk/res/android"
    android:antialias="true"
    android:dither="true"
    android:src="@drawable/png_icon_416"
    android:tilemode="mirror" />

用程式碼也能達到同樣效果:

Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.png_icon_416);
BitmapDrawable mBitmapDrawable = new BitmapDrawable(mBitmap);
mBitmapDrawable.setTileModeXY(TileMode.MIRROR, TileMode.MIRROR);
mBitmapDrawable.setAntiAlias(true);
mBitmapDrawable.setDither(true);
mDrawable = mBitmapDrawable;

NinePatchDrawable:點九圖為何不會變形

.9 圖的價值在於「一張切圖適配多種尺寸」。它把一張 PNG 切成九宮格:四角不拉伸,所以圓角永遠清晰;上下兩條邊只做垂直拉伸、左右兩條邊只做水平拉伸,所以邊框不會被拉粗;真正被拉伸的只有中央區域。

黑線的定義是:左邊與上邊的黑線圍出的矩形是「拉伸區」,右邊與下邊的黑線圍出的矩形是「內容區」。黑線可以中斷,但連續畫通常效果最好。Android SDK 自帶 draw9patch.bat 工具,能把一般 PNG 直接轉成點九圖。

XML 用法很簡單:

<?xml version="1.0" encoding="utf-8"?>
<nine-patch xmlns:android="https://schemas.android.com/apk/res/android"
    android:dither="true"
    android:src="@drawable/droid_logo" />

不過實務上幾乎沒人用程式碼建立 NinePatchDrawable,原因是 SDK 會在編譯階段把點九圖處理成特殊格式;用程式碼建立時只能吃「已編譯過」的資源,未編譯的會被當成普通 BitmapDrawable 對待。另外要記得:點九圖只解決「拉伸」問題,遇到壓縮場景並不適用,需要適配大量解析度時反而該把圖做小一點。

ClipDrawable:用 level 控制裁切

系統的進度條就是 ClipDrawable 的經典應用。它依 level 屬性決定裁掉多少,數值範圍是 0 到 10000:

<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="https://schemas.android.com/apk/res/android"
    android:cliporientation="horizontal"
    android:drawable="@drawable/bitmap_android"
    android:gravity="left" />

如果不寫 android:drawable,就必須給一個子節點當作圖源:

<clip xmlns:android="https://schemas.android.com/apk/res/android"
    android:cliporientation="horizontal"
    android:gravity="left">
    <bitmap android:gravity="center"
        android:src="@drawable/android_text" />
</clip>

AnimationDrawable:逐幀播放

以 <animation-list> 為根節點,每個 <item> 指定一幀的圖源與持續時間(毫秒):

<animation-list xmlns:android="https://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/level1" android:duration="300" />
    <item android:drawable="@drawable/level2" android:duration="300" />
    <item android:drawable="@drawable/level3" android:duration="300" />
    <item android:drawable="@drawable/level4" android:duration="300" />
    <item android:drawable="@drawable/level5" android:duration="300" />
</animation-list>

定義好之後要記得手動 start()。這裡有個經典陷阱:在 onCreate() 裡呼叫 start() 不會有任何反應,因為 View 尚未完成初始化。正確作法是延後到下一輪訊息迴圈:

mHandler.postDelayed(new Runnable() {
    @Override
    public void run() {
        ((AnimationDrawable) mDrawable).start();
    }
}, 1000);

LayerDrawable:多層疊放

它管理一組 Drawable,每個佔一個層級,繪製時依序全部畫上去。即使有重疊,因為分屬不同層,視覺上不會互相干擾。

<layer-list xmlns:android="https://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/layer1" />
    <item android:drawable="@drawable/layer2" />
    <item android:drawable="@drawable/layer3" />
</layer-list>

LevelListDrawable:依 level 選一個來畫

與 LayerDrawable 全部畫上去不同,LevelListDrawable 是「依 level 值挑一個」來繪製,每個 item 對應一段 level 區間:

<level-list xmlns:android="https://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/level1" android:maxLevel="2000" />
    <item android:drawable="@drawable/level2" android:maxLevel="4000" />
    <item android:drawable="@drawable/level3" android:maxLevel="6000" />
</level-list>

StateListDrawable:最常用的一種

它同樣管理一組 Drawable,但每個都綁定一組狀態,比對邏輯近似 Java 的 switch-case:由上而下逐一比對,命中即回傳。因此排序至關重要,最精確的條件要放在最前面。

<selector xmlns:android="https://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/gradient_normal"
        android:state_focused="false"
        android:state_pressed="false" />
    <item android:drawable="@drawable/gradient_pressed"
        android:state_pressed="true" />
    <item android:drawable="@drawable/gradient_focused"
        android:state_focused="true" />
</selector>

日常開發寫背景選擇器時,大概只會用到 pressed 與 focused 兩種狀態,但它能表達的狀態其實多得多,幾乎可以拼出各種複雜的視覺回饋。

Drawable 家族還有 transition、inset、scale 等成員,這裡不逐一展開。掌握「它是可被繪製的規則,而非單純的圖片」這個核心觀念,再回頭看任何一個子類,都能很快推測出它的行為。

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