【问题标题】:clear tint on a vector drawable programatically?以编程方式在可绘制的矢量上清除色调?
【发布时间】:2022-11-28 15:46:34
【问题描述】:

我正在尝试切换源为矢量可绘制对象的 ImageView 的色调:

    <ImageView
    android:id="@+id/iv_lightbulb"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:srcCompat="@drawable/ic_lightbulb" />

看看这个网站上的其他解决方案,我在我的 recyclerview 适配器的图像视图上尝试了这个:

lightbulbIV.setOnClickListener {
                // read tint of vector drawable, unfilled by default or yellow
                val currentColor =
                    ImageViewCompat.getImageTintList(lightbulbIV)?.defaultColor
                if (currentColor == null) {
                    // make vector drawable yellow
                    ImageViewCompat.setImageTintList(
                        lightbulbIV,
                        ColorStateList.valueOf(
                            ContextCompat.getColor(parent.context, R.color.yellow)
                        )
                    )
                } else
                    // unfill the vector drawable 
                    ImageViewCompat.setImageTintList(lightBulbIV, null)

setImageTintList with null 只是完全删除图像,这不是我想要的。 lightBulbIV.clearColorFilter() 也不会产生预期的效果。我需要做什么来清除色调,以便下次单击图像时将其设置为色调?

【问题讨论】:

    标签: android


    【解决方案1】:

    这个对我有用。你能试试这个代码吗?

        logo.setOnClickListener {
            // read tint of vector drawable, unfilled by default or yellow
            val currentColor =
                ImageViewCompat.getImageTintList(logo)?.defaultColor
            if (currentColor == null) {
                // make vector drawable yellow
                ImageViewCompat.setImageTintList(
                    logo,
                    ColorStateList.valueOf(
                        ContextCompat.getColor(applicationContext, R.color.purple_200)
                    )
                )
            } else
            // unfill the vector drawable
                ImageViewCompat.setImageTintList(logo, null)
        }
    

    【讨论】:

      猜你喜欢
      • 2019-08-14
      • 2018-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-07
      • 1970-01-01
      • 1970-01-01
      • 2018-08-10
      相关资源
      最近更新 更多