• Android Tint着色器


    1.tint

    Tint着色器可以使图片变色,使用tint可以显示不同颜色的图片。

    Tint着色器效果是将非透明的像素点渲染成指定的颜色。比如给定一个白色图标图片,如果要显示不同的颜色,可以直接在ImageView中设置android:tint或app:tint属性,设置一个颜色值即可将该图片显示为指定颜色的图片。这样同一张图片可以显示多种不同颜色的效果,从而减少APK打包的图片数量,也就减少了APK安装包的大小。

     

    2.基本用法

    Tint基本用法就是在ImageView组件中添加app:tint属性,为其设置一个颜色值属性值即可。

        android:layout_width="50dp"

        android:layout_height="50dp"

        android:layout_margin="10dp"

        android:src="@drawable/a_normal" />

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal">

       

            android:layout_width="50dp"

            android:layout_height="50dp"

            android:layout_margin="10dp"

            android:src="@drawable/a_normal"

            app:tint="@color/red" />

       

            android:layout_width="50dp"

            android:layout_height="50dp"

            android:layout_margin="10dp"

            android:background="@color/white"

            android:backgroundTint="@color/red"

            android:src="@drawable/a_normal" />

    运行效果展示 : 第一张图片是图片本身颜色,后面两张图片分别设置了Tint颜色值。

    446af08196754b11900bcff7ce862050.png

     

    3.tint属性

    tint一般与tintMode配合使用。 同时还有backgroundTint和backgroundTintMode属性(backgroundTint是针对背景色着色,backgroundTint只有在为控件设置了background属性后才会生效。)

    ①android:tint,android:tintMode

    作用于ImageView,对ImageView内的mDrawable(内容图片)着色,以及着色模式设定。

    对应设定方法:

    void setImageTintList(ColorStateList tint)

    void setImageTintMode(PorterDuff.Mode tintMode);

    ②android:backgroundTint,android:backgroundTintMode

    作用于View,对View内的mBackground(背景图)着色,以及着色模式设定。

    对应设定方法:

    void setBackgroundTintList(ColorStateList tint)

    void setBackgroundTintMode(PorterDuff.Mode tintMode)

    ③android:foregroundTint,android:foregroundTintMode

    作用于View,对View内的mForegroundInfo.mDrawable(前景图)着色,以及着色模式设定。

    对应设定方法:

    void setForegroundTintList(ColorStateList tint)

    void setForegroundTintMode(PorterDuff.Mode tintMode)

    ④android:drawableTint,android:drawableTintMode

    作用于TextVIew,对TextVIew内的mDrawables(TextView上下左右图)着色,以及着色模式设定。

    对应设定方法:

    void setCompoundDrawableTintList(ColorStateList tint)

    void setCompoundDrawableTintMode(PorterDuff.Mode tintMode)

    注:Tint在较低系统版本无法支持,需要使用相应的Compact类。

    tintMode有六种模式:src_in、add、multiply、screen、src_atop、src_over

    60a5384211b34eafb9de62101a0cf154.png

     

  • 相关阅读:
    18--Django-项目实战-博客开发-个人站点板块
    智慧图书馆中的自助借还系统
    React 使用合成事件(SyntheticEvent)
    Spring Cloud--Nacos+@RefreshScope实现配置的动态更新
    捷报频传!苏州箱讯荣获2023年江苏省物流产业服务贡献奖
    韩国突发:将批准比特币ETF
    电脑一键重装系统后如何打开事件查看器
    如何实现云上 Lakehouse 高性能
    ai 问答时刻
    常用的BI工具有哪些?口碑怎样?
  • 原文地址:https://blog.csdn.net/zenmela2011/article/details/127457879