【发布时间】: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