android 是否通过编程方式清除可绘制矢量的色调?

iyfjxgzm  于 2022-12-02  发布在  Android
关注(0)|答案(1)|浏览(119)

我正在尝试切换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适配器中的imageview上尝试了这个:

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和null只是完全删除图像,这不是我想要的lightBulbIV.clearColorFilter()也没有产生想要的效果。我需要做什么来清除色调,以便下次单击图像时将其设置为色调?

dced5bon

dced5bon1#

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

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)
    }

相关问题