概述
关于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:tint
andandroid:tintMode
attributes.
这段文字讲述的大意是:Tint 是 API 21之后才添加的功能,可以对BitmapDrawable和 NinePatchDrawable 应用Tint 效果。使用tint效果可以省去我们为不同theme创建同一张图片的多个版本的麻烦。紧接着第二段讲道如何使用tint, 有两种方法:
1 调用函数:
class Drawable {
...
public void setTint (int tint); //Added in API 21
public void setTintMode (PorterDuff.Mode tintMode); //Added in API 21
...
}
2 XML Layout:
drawable file location: res/drawable/tintedrawable.xml
<nine-path xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/rawdrawable"
android:tint="@color/what_ever_color"
android:tintMode="multiply" />
layout file location: 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 and Tint
class ImageView {