通过程序设置ImageView的状态色调



我正在尝试用程序更新ImageView的色调,我使用的颜色是一个选择器,在启用或禁用视图时具有不同的颜色。当我尝试使用其他StackOverflow帖子中建议的方法时,它设置了启用的颜色,但不会更新禁用的视图

例如,我有以下四个点,它们在XML中是红色的,在代码中我将底部的两个设置为绿色。右边的两个点被禁用了,所以我希望两者都是相同的禁用灰色,但正如你所看到的,程序设置的颜色总是绿色

https://i.stack.imgur.com/tFpgp.png

如何通过程序更新ImageView的色调,使其正确遵循选择器中的状态?

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<!-- Also tried making these AppCompatImageViews -->
<ImageView
android:id="@+id/imgDot1"
android:layout_width="40dp"
android:layout_height="40dp"
android:tint="@color/color_red_stateful"
android:src="@drawable/ic_dot" />
<ImageView
android:id="@+id/imgDot2"
android:layout_width="40dp"
android:layout_height="40dp"
android:tint="@color/color_red_stateful"
android:src="@drawable/ic_dot" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imgDot3"
android:layout_width="40dp"
android:layout_height="40dp"
android:tint="@color/color_red_stateful"
android:src="@drawable/ic_dot" />
<ImageView
android:id="@+id/imgDot4"
android:layout_width="40dp"
android:layout_height="40dp"
android:tint="@color/color_red_stateful"
android:enabled="false"
android:src="@drawable/ic_dot" />
</LinearLayout>
imgDot2.isEnabled = false
imgDot4.isEnabled = false
// Tried each of these in turn
ImageViewCompat.setImageTintList(imgDot3, ColorStateList.valueOf(resources.getColor(R.color.color_green_stateful)))
ImageViewCompat.setImageTintList(imgDot4, ColorStateList.valueOf(resources.getColor(R.color.color_green_stateful)))
imgDot3.setColorFilter(ContextCompat.getColor(this, R.color.color_green_stateful))
imgDot4.setColorFilter(ContextCompat.getColor(this, R.color.color_green_stateful))
imgDot3.imageTintList = ColorStateList.valueOf(resources.getColor(R.color.color_green_stateful))
imgDot4.imageTintList = ColorStateList.valueOf(resources.getColor(R.color.color_green_stateful))
imgDot3.imageTintList = ColorStateList.valueOf(ContextCompat.getColor(this, R.color.color_green_stateful))
imgDot4.imageTintList = ColorStateList.valueOf(ContextCompat.getColor(this, R.color.color_green_stateful))

color_red_stateful.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:color="#999"/>
<item android:color="#F00"/>
</selector>

color_green_stateful.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:color="#999"/>
<item android:color="#0F0"/>
</selector>

ic_dot.xml

<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#000"
android:pathData="M12,12m-10,0a10,10 0,1 1,20 0a10,10 0,1 1,-20 0"/>
</vector>

您在每个尝试的行中都使用getColor方法,但这不正确,这不是颜色,这是selector(color_red_statefulcolor_green_stateful文件(。使用Resources或优选ContextCompat类及其有用方法getColorStateList

ColorStateList colorStateList = ContextCompat.getColorStateList(this, R.color.your_color_selector);

colorStateList应设置为所需ImageViews 的imageTintList

顺便说一句。在上面的设置之后,你可以使用view.invalidate()方法来强制立即应用更改(强制重绘(,但在大多数情况下,也可能是你的,这将不需要

我正在尝试以编程方式更新ImageView的色调,我使用的颜色是一个选择器,当视图启用或禁用时,它具有不同的颜色

选项1:

正如我所看到的,您已经为XML文件中的每个视图设置了android:tint

因此,要以编程方式启用禁用,您需要在启用或禁用后更新视图。

首先将isEnabled设置为true或false:

imgDot2.isEnabled = false
imgDot4.isEnabled = false

无效视图:

imgDot2.invalidate()
imgDot4.invalidate()

然后它将根据您的输入更新视图。

无需在"活动"中再次设置setImageTintListsetColorFilter

选项2:

想要通过程序设置颜色然后

ImageViewCompat.setImageTintList(imgDot2, ContextCompat.getColorStateList(this, R.color.color_green_stateful))
ImageViewCompat.setImageTintList(imgDot4, ContextCompat.getColorStateList(this, R.color.color_green_stateful))

无效视图:

imgDot2.invalidate()
imgDot4.invalidate()

注意:在XML中必须使用app:tint而不是android:tint

最新更新