Kotlin动态设置View前景色扩展函数

66 阅读1分钟
package com.example.testcompose

import android.annotation.SuppressLint
import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import android.os.Build
import android.view.View
import android.widget.ImageView
import androidx.annotation.ColorInt
import androidx.annotation.ColorRes
import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.DrawableCompat
import androidx.core.graphics.drawable.TintAwareDrawable
import androidx.core.view.TintableBackgroundView
import androidx.core.widget.TintableImageSourceView

/*
  * ================================================
  * 描述:Kotlin动态设置View前景色扩展函数
  * 作者:zhuangzeqin
  * 时间: 2025/5/16 11:47
  * 邮箱:zzq@eeepay.cn
  * 备注:用法(设置Tint为例):view.setBackgroundTintColor(0xff006241.toInt())
  *
  * ----------------------------------------------------------------
  * You never know what you can do until you try !
  *      _              _           _     _   ____  _             _ _
  *     / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
  *    / _ \ | '_ \ / _` | '__/ _ | |/ _` | ___ | __| | | |/ _` | |/ _ \
  *   / ___ | | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
  *  /_/   __| |_|__,_|_|  ___/|_|__,_| |____/ __|__,_|__,_|_|___/
  *
  * 签名:最痛苦的事不是我失败了,而是我本可以.--zzq
  * ----------------------------------------------------------------
  * ================================================
  */
class ViewResSetter {
    fun View.setBackgroundTintRes(@ColorRes colorRes: Int) {
//        Color.parseColor("#ff006241")
        val colorStateList = ContextCompat.getColorStateList(this.context, colorRes)
        setBackgroundTintColor(colorStateList)
    }

    fun View.setBackgroundTintColor(@ColorInt colorInt: Int) {
        val colorStateList = ColorStateList.valueOf(colorInt)
        setBackgroundTintColor(colorStateList)
    }

    fun View.setBackgroundTintColor(colorStateList: ColorStateList?) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            this.backgroundTintList = colorStateList
        } else if (this is TintableBackgroundView) {
            val tintView = this as TintableBackgroundView
            tintView.supportBackgroundTintList = colorStateList
        } else {
            var background: Drawable = this.background ?: return
            if (background !is TintAwareDrawable) {
                background = DrawableCompat.wrap(background)
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    this.background = background
                } else {
                    this.setBackgroundDrawable(background)
                }
            }
            DrawableCompat.setTintList(background, colorStateList)
        }
    }

    fun ImageView.setSrcTintRes(@ColorRes colorRes: Int) {
        val colorStateList = ContextCompat.getColorStateList(this.context, colorRes)
        setSrcTint(colorStateList)
    }

    @SuppressLint("RestrictedApi")
    fun ImageView.setSrcTint(colorStateList: ColorStateList?) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            this.imageTintList = colorStateList
        } else if (this is TintableImageSourceView) {
            val tintView = this as TintableImageSourceView
            tintView.supportImageTintList = colorStateList
        } else {
            var drawable: Drawable = this.drawable ?: return
            if (drawable !is TintAwareDrawable) {
                drawable = DrawableCompat.wrap(drawable)
                this.setImageDrawable(drawable)
            }
            DrawableCompat.setTintList(drawable, colorStateList)
        }
    }

    /**
     * 创建一个扩展函数来动态改变View的背景颜色
     */
    fun View.setBackgroundColorRes(@ColorRes colorRes: Int) {
        this.setBackgroundColor(ContextCompat.getColor(context, colorRes))
    }

    fun View.setBackgroundColorRes(colorRes: String) {
//        view.setBackgroundColor(Color.parseColor("#FF0000")) // 设置为红色
        this.setBackgroundColor(Color.parseColor(colorRes))
    }

    /**
     * 设置渐变背景
     */
    fun View.setGradientBackground(startColor: Int, endColor: Int, orientation: GradientDrawable.Orientation = GradientDrawable.Orientation.TOP_BOTTOM) {
        val gradientDrawable = GradientDrawable(
            orientation, // 可以是其他方向,如LEFT_RIGHT, TL_BR等
            intArrayOf(startColor, endColor)
        )
        background = gradientDrawable
//        invalidate()
    }
//在Kotlin代码中,获取对应的bottomNavigationView实例,并使用setItemIconTintList()方法设置图标的颜色。
//val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottomNavigationView)
//bottomNavigationView.itemIconTintList = ColorStateList.valueOf(ContextCompat.getColor(this, R.color.your_color))

}