Android ColorFilter and Tint

本文探讨了Android中ColorFilter和Tint的区别与联系,它们都是用于改变图像颜色的效果。Tint在API 21及以上版本引入,主要用于BitmapDrawable和NinePatchDrawable,可以设置颜色或主题属性进行着色。文章通过示例展示了ImageView和Drawable应用Tint的不同方法,并指出对于多状态着色,ImageButton比ImageView表现更好。最后提到,系统原生支持的多状态Tint仅限于特定视图,期待更好的解决方案。
摘要由CSDN通过智能技术生成

概述

关于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之后才添加的功能,可以对BitmapDrawableNinePatchDrawable 应用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 {

    
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值