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

作者 : 开心源码 本文共2828个字,预计阅读时间需要8分钟 发布时间: 2022-05-12 共213人阅读

参考

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;    }

说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » Android tint着色(优化,减小apk体积)

发表回复