編輯:關於 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 theandroid:tintandandroid:tintModeattributes.
大意是:Tint 是 API 21 之後才加入的能力,可以對 BitmapDrawable 與 NinePatchDrawable 套用著色。有了它,就不必為同一張圖在不同主題下各做一個版本,省下大量切圖工。文件接著提到兩種使用方式。
class Drawable {
...
public void setTint(int tint); // Added in API 21
public void setTintMode(PorterDuff.Mode tintMode); // Added in API 21
...
}
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" />
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 而言,不論採用哪一種寫法,最終視覺結果都是一樣的。
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 才會生效。
文件明確指出,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 做狀態切換時,別忘了檢視本身得具備狀態傳遞能力。系統若能在原生層面直接支援,當然更好。
Android 視頻錄制並保存到本地的完整實現
視頻錄制是不少 App 的常見需求。這裡貼出主要代碼並做詳細講解,幫助你把「調用相機預覽 → 錄制視頻 → 保存到本地」這條鏈路走通。 關鍵角色與整體思路 整個錄制流程
Android 讓 ImageView 顯示網路圖片的完整做法
並非所有素材都適合打包進 APK,體積、更新頻率、授權都是考量。這時候把圖片放在網路上,讓 ImageView 在執行期動態載入,反而是更靈活的解法。 先看一下實作後的效
Android 學習:LogCat 日誌與 Log 類的使用
Android 應用運行後,System.out.println() 的輸出並不會出現在 IDE 的控制台裡。這是很多從 Java SE 轉過來的同學第一個會撞到的牆。A
Android Studio 中 Gradle 的配置與使用大全
加速篇 Gradle 的構建過程往往漫長得讓人抓狂——一個 10M 左右的中等項目,一次完整構建大約要 5 分鐘。好在調試階段可以用 Android Studio 2.0