Android:使用 Tint 为图标 Icon 动态着色

1. 背景

在 App 当中,会有很多 形状相同、颜色不同 的 Icon。

例如上图这个场景,是筛选项的 icon,对应的代码可能为:

  • Xml 中初始化颜色为黑色
xml 复制代码
<ImageView
    ...
    android:src="@drawable/icon_filter_black" />
  • 代码中根据筛选状态,切换不同颜色的 icon
kotlin 复制代码
// XXXFragment.kt

imageView.setImageDrawable(
    if (isSelect) {
        // 筛选后为橙色
        resources.getDrawable(R.drawable.icon_filter_orange);
    } else {
        // 未筛选为黑色
        resources.getDrawable(R.drawable.icon_filter_black);
    }
)

如果为每种颜色的 Icon 都保存一张图片,会增加包体积。同时,会增加设计的工作负担。有没有什么办法可以做到只需要一张图片,然后动态的着色呢?

2. 使用 Tint 动态着色

2.1 使用方式

有以下几种方式:

  1. xml 中设置
xml 复制代码
<ImageView
    ...
    android:src="@drawable/icon_filter" 
    app:tint="@color/black"
    />
  1. 代码中为 ImageView 设置
kotlin 复制代码
imageView.imageTintList = ColorStateList(...)
  1. 代码中直接为 Drawable 设置
kotlin 复制代码
val drawable = resources.getDrawable(R.drawable.icon_filter)
drawable.setTint = Color.BLACK

imageView.setImageDrawable(drawable)

设置 tint 后,绘制时就会用 tint 的颜色渲染 icon。这样,就算只有一张图片,也可以渲染为多种颜色。很大程度的节省了包体积。

2.2 原理分析

  • ImageView 会将 Tint 传给 Drawable
java 复制代码
 // ImageView.java
public void setImageTintList(@Nullable ColorStateList tint) {
    mDrawableTintList = tint;
    mHasDrawableTint = true;
    
    applyImageTint();
}

private void applyImageTint() {
    if (mDrawable != null && (mHasDrawableTint || mHasDrawableBlendMode)) {
        mDrawable = mDrawable.mutate();
        
        if (mHasDrawableTint) {
            mDrawable.setTintList(mDrawableTintList); // 将 tint 传递给 Drawable!
        }
        // ...
    }
}
  • Drawable 在 收到 Tint 时会创建 ColorFilter,在 draw 时会用它做颜色过滤处理。这里拿 VectorDrawable 举例:
java 复制代码
public class VectorDrawable extends Drawable {
    private BlendModeColorFilter mBlendModeColorFilter ;
    
    @Override
    public void setTintList(ColorStateList tint) {
        // 使用 tint 创建 ColorFilter
        updateColorFilters(... , tint) ;
    }
    
    private void updateColorFilters(@Nullable BlendMode blendMode , ColorStateList tint) {
        // ...
    mBlendModeColorFilter = updateBlendModeFilter(mBlendModeColorFilter , tint , blendMode) ;
    }
    
    @Override
    public void draw(Canvas canvas) {
        // 绘制时,如果外部没有设置 mColorFilter,则使用 mBlendModeColorFilter
        final ColorFilter colorFilter = (mColorFilter == null ? mBlendModeColorFilter : mColorFilter) ;
        // ...
    }
}

public abstract class Drawable {
    @Nullable 
    BlendModeColorFilter updateBlendModeFilter(@Nullable ColorStateList tint , ... ) {
        // ... 
        final int color = tint.getColorForState(getState() , Color.TRANSPARENT) ;
    
        if (blendFilter == null || blendFilter.getColor() != color
                || blendFilter.getMode() != blendMode) {
            return new BlendModeColorFilter(color , blendMode) ;
        }
        
        return blendFilter ;
    }
}

3. 注意事项

3.1 同时设置 ImageView 与 Drawable,Drawable 的设置会失效

  • 例如,我们在 xml 中设置 tint 为黑色
xml 复制代码
<ImageView
    ...
    android:src="@drawable/icon_filter"
    app:tint="@color/black"
    />
  • 在代码中,为 Drawable 设置为黄色
kotlin 复制代码
val drawable = resources.getDrawable(R.drawable.icon_filter)
drawable.setTint = Color.YELLOW

imageView.setImageDrawable(drawable)
  • 此时,对 Drawable 的设置是不生效的,因为此时 ImageView.setImageDrawable 会覆盖掉 Drawable 自身的 tint
kotlin 复制代码
// ImageView.kt

public void setImageDrawable(@Nullable Drawable drawable) {
    updateDrawable(drawable) ;
}

private void updateDrawable(Drawable d) {
    applyImageTint() ;
}

private void applyImageTint() {
    if (mHasDrawableTint) {
        mDrawable.setTintList(mDrawableTintList) ;
    }
}
  • 所以如果同时设置 ImageView 与 Drawable 的 tint,要关注覆盖问题。
相关推荐
张风捷特烈31 分钟前
Flutter UI 解耦 - 天下大势,合久必分, 分久必合
android·前端·flutter
蓝速科技40 分钟前
蓝速科技丨甲级写字楼会议预约屏尺寸选型与空间适配指南
android·科技
zhangjin11204 小时前
解决Android Studio gradle下载超时和缓慢问题(二)
android·ide·android studio
xingxiliang11 小时前
深入浅出 Android CTS 视频测试:从环境搭建、用例设计到底层通信原理
android·音视频
我命由我1234511 小时前
Android 开发问题:为 PDFView 设置一个带有黑色边框的背景 drawable,但边框没有生效
android·java·java-ee·android studio·android jetpack·android-studio·android runtime
雨白13 小时前
深入理解 Kotlin 协程 (八):拾遗补阙,探秘官方框架的调度细节与取消闭环
android·kotlin
海天鹰15 小时前
PHP上传文件
android·开发语言·php
2501_9159184116 小时前
详解iOS App上架至App Store的全流程步骤与注意事项
android·macos·ios·小程序·uni-app·cocoa·iphone
码农coding17 小时前
android12 SystemUI之StatusBar(二)
android
Lesile19 小时前
Interview#1 历史演进:MVC · MVP · MVVM · MVI架构详解
android·android jetpack