Android tint著色(優(yōu)化,減小apk體積)

參考

1、Android-使用tint一張圖制作selector
2、探索Android Material Design 中的Tint(著色)
3、Android background tint顏色渲染

前言

tint翻譯而來(lái)就是著色(顏色渲染),就是在一張圖的圖層上刷顏色而達(dá)到不同效果,一個(gè)顯著的好處就是,一個(gè)簡(jiǎn)單的icon圖標(biāo)使用,不再需要美工做多張不同顏色的,直接用著色器多一張icon著色,就有好多張不同效果的icon圖片了,可以顯著減少apk體積。

PorterDuff.Mode屬性

tint著色的主要屬性,在xml中是tintMode,用來(lái)在不同場(chǎng)景中使用的


image.png
常量 含義
CLEAR 所繪制不會(huì)提交到畫(huà)布上
SRC 顯示上層繪制圖片
DST 顯示下層繪制圖片
SRC_OVER 正常繪制顯示,上下層繪制疊蓋
DST_OVER 上下層都顯示。下層居上顯示
SRC_IN 取兩層繪制交集。顯示上層
DST_IN 取兩層繪制交集。顯示下層
SRC_OUT 取上層繪制非交集部分
DST_OUT 取下層繪制非交集部分
SRC_ATOP 取下層非交集部分與上層交集部分
DST_ATOP 取上層非交集部分與下層交集部分
XOR 異或:去除兩圖層交集部分
DARKEN 取兩圖層全部區(qū)域,交集部分顏色加深
LIGHTEN 取兩圖層全部,點(diǎn)亮交集部分顏色
MULTIPLY 取兩圖層交集部分疊加后顏色
SCREEN 取兩圖層全部區(qū)域,交集部分變?yōu)橥该魃?/td>
ADD
OVERLAY

實(shí)例

截圖
image.png
代碼
  • 1、普通無(wú)著色、著紅色、著綠色背景橘黃色
    使用tint和android:backgroundTint這兩個(gè)屬性
<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、變色器,按鈕點(diǎn)擊觸發(fā)變色
    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代碼實(shí)現(xiàn)tint變色,2種方式,api高于或等于21的直接用setImageTintList進(jìn)行著色,api低的用著色好的drawable來(lái)加載

    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工具類(lèi)獲取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;
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 很早看過(guò)這篇文章,并做了筆記,后來(lái)看到群里的小伙伴有問(wèn)相關(guān)Drawable的問(wèn)題,就把這篇翻譯過(guò)來(lái)的文章給放出來(lái)了...
    Kotyo閱讀 1,687評(píng)論 0 5
  • 概述 今天我們來(lái)探究一下android的樣式。其實(shí),幾乎所有的控件都可以使用 background屬性去引用自定義...
    CokeNello閱讀 5,118評(píng)論 1 19
  • ¥開(kāi)啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開(kāi)一個(gè)線程,因...
    小菜c閱讀 7,362評(píng)論 0 17
  • Android有很多種drawable類(lèi)型,除了前幾篇詳細(xì)講解的shape、selector、layer-list...
    騎著老鼠追貓咪閱讀 931評(píng)論 0 2
  • 轉(zhuǎn)載自Keegan小鋼并標(biāo)明原文鏈接:http://keeganlee.me/post/android/20150...
    堅(jiān)持編程_lyz閱讀 1,267評(píng)論 0 1

友情鏈接更多精彩內(nèi)容