Android Tint着色器

Android Tint着色器

概述

tint 译为着色。tint一般与tintMode配合使用。 同时还有backgroundTint和backgroundTintMode属性(很明显backgroundTint是针对背景色着色,通过实践也证实了backgroundTint只有在为控件设置了background属性才会生效。)

tintMode和backgroundTintMode有六种模式:

  • src_in
  • add
  • multiply
  • screen
  • src_atop
  • src_over

使用

XML中使用

在这里插入图片描述

<ImageView
    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">

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_margin="10dp"
        android:src="@drawable/a_normal"
        app:tint="@color/red" />

    <ImageView
        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" />

</LinearLayout>

代码中使用

//修改图片颜色
val drawable: Drawable? = ContextCompat.getDrawable(this, R.drawable.a_normal)
drawable?.let {
    val tintDrawable: Drawable = DrawableCompat.wrap(it).mutate()
    DrawableCompat.setTint(tintDrawable, Color.RED)
    imageView1.setImageDrawable(tintDrawable)
}
//修改背景色
val tintDrawable: Drawable = DrawableCompat.wrap(ColorDrawable(Color.WHITE)).mutate()
DrawableCompat.setTint(tintDrawable, Color.RED)
imageView2.setImageResource(R.drawable.a_normal)
imageView2.setBackground(tintDrawable)

选择器使用

按下状态和抬起状态颜色变化:

res/color目录下定义颜色选择器selector

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/red" android:state_pressed="true" />
    <item android:color="@color/black" />
</selector>
val drawable3 = ContextCompat.getDrawable(this, R.drawable.a_normal)
drawable3?.let {
    val tintDrawable = DrawableCompat.wrap(it).mutate()
    DrawableCompat.setTintList(tintDrawable,
        ContextCompat.getColorStateList(this, R.color.selector))
    imageView3.setImageDrawable(tintDrawable)
}

tintMode详解

在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="原始图片" />

        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_margin="10dp"
            android:src="@drawable/a_normal" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="默认值 src_in" />

        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_margin="10dp"
            android:src="@drawable/a_normal"
            app:tint="@color/red" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="add" />

        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_margin="10dp"
            android:src="@drawable/a_normal"
            android:tintMode="add"
            app:tint="@color/red" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="multiply" />

        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_margin="10dp"
            android:src="@drawable/a_normal"
            android:tintMode="multiply"
            app:tint="@color/red" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="screen" />

        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_margin="10dp"
            android:src="@drawable/a_normal"
            android:tintMode="screen"
            app:tint="@color/red" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="src_atop" />

        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_margin="10dp"
            android:src="@drawable/a_normal"
            android:tintMode="src_atop"
            app:tint="@color/red" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="src_over" />

        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_margin="10dp"
            android:src="@drawable/a_normal"
            android:tintMode="src_over"
            app:tint="@color/red" />
    </LinearLayout>
</LinearLayout>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值