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

Android 著色機制解析:ColorFilter 與 Tint 到底差在哪

編輯:關於 Android 程式設計

概述

Android 的 ColorFilter 與 Tint 一直是很多人混淆的一對概念。兩者都是拿來對顯示的圖片做著色或色彩過濾:

ColorFilter:色彩過濾
Tint:著色

從字面意義看似乎很接近,從實際視覺效果看也幾乎一致。而 Android 官方文件對此僅一筆帶過,反而讓人更摸不著頭緒:

With Android 5.0 (API level 21) and above, you can tint bitmaps and nine-patches defined as alpha masks. You can tint them with color resources or theme attributes that resolve to color sources (for example, ?android:attr/colorPrimary). Usually, you create these assets only once and color them automatically to match your theme.

You can apply a tint to BitmapDrawable or NinePatchDrawable objects with the setTint() method. You can also set the tint color and mode in your layouts with the android:tint and android:tintMode attributes.

大意是:Tint 是 API 21 之後才加入的能力,可以對 BitmapDrawable 與 NinePatchDrawable 套用著色。有了它,就不必為同一張圖在不同主題下各做一個版本,省下大量切圖工。文件接著提到兩種使用方式。

1. 呼叫方法

class Drawable {
    ...
    public void setTint(int tint);                    // Added in API 21
    public void setTintMode(PorterDuff.Mode tintMode); // Added in API 21
    ...
}

2. XML 宣告

drawable 檔(res/drawable/tintedrawable.xml):

<bitmap xmlns:android="https://schemas.android.com/apk/res/android"
    android:src="@drawable/rawdrawable"
    android:tint="@color/what_ever_color"
    android:tintMode="multiply" />

layout 檔(res/layout/layout_main.xml):

<View
    android:id="@+id/my_view_id"
    android:layout_width="20dp"
    android:layout_height="10dp"
    android:background="@drawable/tintedrawable" />

ImageView 與 Tint

class ImageView {
    public final void setColorFilter(int color, PorterDuff.Mode mode); // Added in API 1
    public void setImageTintList(ColorStateList tint);                 // Added in API 21
    public void setImageTintMode(PorterDuff.Mode tintMode);            // Added in API 21
}

除了原本就有的 setColorFilter(int, PorterDuff.Mode),API 21 也為 ImageView 補上了 setImageTintList()、setImageTintMode(),以及 XML 屬性 android:tint、android:tintMode。

三種寫法,效果相同

先看一個範例,用三種不同方式為同一張圖著色:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="setColorFilter" />

<ImageView
    android:id="@+id/image1"
    android:layout_width="200dp"
    android:layout_height="50dp"
    android:layout_gravity="center_horizontal"
    android:scaleType="fitXY"
    android:src="@drawable/rawdrawable" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="setImageTintList" />

<ImageView
    android:id="@+id/image2"
    android:layout_width="200dp"
    android:layout_height="50dp"
    android:layout_gravity="center_horizontal"
    android:scaleType="fitXY"
    android:src="@drawable/rawdrawable" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="android:tint" />

<ImageView
    android:id="@+id/image3"
    android:layout_width="200dp"
    android:layout_height="50dp"
    android:layout_gravity="center_horizontal"
    android:scaleType="fitXY"
    android:src="@drawable/rawdrawable"
    android:tint="#673AB7"
    android:tintMode="multiply" />
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.tinting_fragment, null);
    mImage1 = (ImageView) v.findViewById(R.id.image1);
    mImage2 = (ImageView) v.findViewById(R.id.image2);
    updateTint(Color.argb(0xFF, 0x67, 0x3A, 0xB7), PorterDuff.Mode.MULTIPLY);
    return v;
}

public void updateTint(int color, PorterDuff.Mode mode) {
    mImage1.setColorFilter(color, mode);
    mImage2.setImageTintList(ColorStateList.valueOf(color));
    mImage2.setImageTintMode(mode);
}

從上圖可以看到,對 ImageView 而言,不論採用哪一種寫法,最終視覺結果都是一樣的。


ColorStateList 與多狀態的陷阱

ImageView.setImageTintList(ColorStateList) 接受的參數型別是 ColorStateList。前面範例中 android:tint="#673AB7" 給的是單一顏色,那如果換成狀態列表會如何?

1 res/color/custom_tint.xml

<selector xmlns:android="https://schemas.android.com/apk/res/android">
    <!-- Pressed state -->
    <item android:state_pressed="true" android:color="#ff0000" />
    <!-- Default -->
    <item android:color="#4aff48" />
</selector>

2 res/layout/layout_main.xml

<ImageView
    android:id="@+id/image3"
    android:layout_width="200dp"
    android:layout_height="50dp"
    android:layout_gravity="center_horizontal"
    android:clickable="true"
    android:scaleType="fitXY"
    android:src="@drawable/rawdrawable"
    android:tint="@color/custom_tint"
    android:tintMode="multiply" />

實測會發現:點擊這個 ImageView 時,顏色並沒有隨按下狀態改變。把檢視改成 ImageButton 再試一次:

<ImageButton
    android:id="@+id/image3"
    android:layout_width="200dp"
    android:layout_height="50dp"
    android:layout_gravity="center_horizontal"
    android:scaleType="fitXY"
    android:src="@drawable/rawdrawable"
    android:tint="@color/custom_tint"
    android:tintMode="multiply" />

原因在於 ImageView 預設不傳遞可變狀態,換成 ImageButton 後它就具備了 pressed 狀態的傳遞能力,ColorStateList 才會生效。


Drawable 層級的 Tint

文件明確指出,Tint 的設計初衷就是為了讓 Drawable 支援著色,其中 BitmapDrawable 與 NinePatchDrawable 都支援 android:tint 與 android:tintMode。這意味著我們可以把 android:tint 的能力從 ImageView 擴展到任意 View——只要把 View 的 android:background 指向自訂的 drawable 即可。

1 res/drawable/tintedrawable.xml

<bitmap xmlns:android="https://schemas.android.com/apk/res/android"
    android:src="@drawable/background_image"
    android:tint="@color/custom_tint"
    android:tintMode="multiply" />

2 res/layout/layout_main.xml

<View
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:background="@drawable/tintedrawable" />

但這裡會遇到與前面完全相同的問題:用 background 承載時不支援多狀態切換。解法也一樣——把 View 換成 ImageButton,並改用 android:src:

<ImageButton
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:scaleType="centerInside"
    android:src="@drawable/tintedrawable" />

小結一句:Tint 適合「一張圖多主題」的單色著色場景,ColorFilter 則更偏向即時、動態的像素運算;而想用 ColorStateList 做狀態切換時,別忘了檢視本身得具備狀態傳遞能力。系統若能在原生層面直接支援,當然更好。

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