美文网首页Android开发Android开发经验谈Android开发
Android tint着色(优化,减小apk体积)

Android tint着色(优化,减小apk体积)

作者: Kandy_JS | 来源:发表于2019-03-10 15:48 被阅读13次

参考

1、Android-使用tint一张图制作selector
2、探索Android Material Design 中的Tint(着色)
3、Android background tint颜色渲染

前言

tint翻译而来就是着色(颜色渲染),就是在一张图的图层上刷颜色而达到不同效果,一个显著的好处就是,一个简单的icon图标使用,不再需要美工做多张不同颜色的,直接用着色器多一张icon着色,就有好多张不同效果的icon图片了,可以显著减少apk体积。

PorterDuff.Mode属性

tint着色的主要属性,在xml中是tintMode,用来在不同场景中使用的


image.png
常量 含义
CLEAR 所绘制不会提交到画布上
SRC 显示上层绘制图片
DST 显示下层绘制图片
SRC_OVER 正常绘制显示,上下层绘制叠盖
DST_OVER 上下层都显示。下层居上显示
SRC_IN 取两层绘制交集。显示上层
DST_IN 取两层绘制交集。显示下层
SRC_OUT 取上层绘制非交集部分
DST_OUT 取下层绘制非交集部分
SRC_ATOP 取下层非交集部分与上层交集部分
DST_ATOP 取上层非交集部分与下层交集部分
XOR 异或:去除两图层交集部分
DARKEN 取两图层全部区域,交集部分颜色加深
LIGHTEN 取两图层全部,点亮交集部分颜色
MULTIPLY 取两图层交集部分叠加后颜色
SCREEN 取两图层全部区域,交集部分变为透明色
ADD
OVERLAY

实例

截图
image.png
代码
  • 1、普通无着色、着红色、着绿色背景橘黄色
    使用tint和android:backgroundTint这两个属性
<ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/dev_printer" />

            <ImageView
                android:layout_marginStart="10dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/dev_printer"
                android:tint="@color/red"/>

            <ImageButton
                android:layout_marginStart="10dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/dev_printer"
                android:tint="@color/green"
                android:background="@color/pink"
                android:backgroundTint="@color/orange"/>
  • 2、变色器,按钮点击触发变色
    src加selector的drawable
<ImageView
                android:layout_marginStart="10dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/bg_tint_press_selector"
                android:clickable="true"/>

drawable/bg_tint_press_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/bg_tint_press_on"/>
    <item android:state_pressed="false" android:drawable="@drawable/dev_printer"/>
</selector>

selector中的drawable/bg_tint_press_on.xml,这里使用tint属性

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/dev_printer"
    android:tint="@color/green"
    android:tintMode="multiply">

</bitmap>

3、Java代码实现tint变色,2种方式,api高于或等于21的直接用setImageTintList进行着色,api低的用着色好的drawable来加载

    private void tint_red(){
        //需要api21
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            LogUtil.d("setImageTintList red >>> ");
            ibt_1.setImageTintList(ColorStateList.valueOf(getResources().getColor(R.color.red)));
        }
        else{
            LogUtil.d("setImageDrawable red >>> ");
            ibt_1.setImageDrawable(tintDrawable(this, R.mipmap.dev_printer, R.color.red));
        }
    }

    public static Drawable tintDrawable(Context context, int resIcon, int resColor){
        //利用ContextCompat工具类获取drawable图片资源
        Drawable drawable = ContextCompat.getDrawable(context, resIcon);
        return tintDrawable(drawable, ContextCompat.getColor(context,resColor));
    }

    public static Drawable tintDrawable(Drawable drawable, int colors) {
        final Drawable wrappedDrawable = DrawableCompat.wrap(drawable).mutate();
        DrawableCompat.setTint(wrappedDrawable, colors);
        return wrappedDrawable;
    }

相关文章

网友评论

    本文标题:Android tint着色(优化,减小apk体积)

    本文链接:https://www.haomeiwen.com/subject/rtnupqtx.html