android - 以编程方式为 ImageView 设置有状态色调

标签 android imageview tint

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

例如,我有以下四个点,它们在 XML 中为红色,而我在代码中将底部的两个点设置为绿色。右侧的两个点被禁用,因此我希望两者都是相同的禁用灰色,但正如您所看到的,以编程方式设置的颜色始终为绿色

/image/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 应设置为所需 ImageViewimageTintList

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

关于android - 以编程方式为 ImageView 设置有状态色调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67799722/

相关文章:

java - Spinner 选择的值未上传到 firebase 数据库

java - ZXing SCAN_RESULT if 语句

android - ImageView 未显示在 ListView 的自定义行内

java - 如何给 ImageIcon 着色

android - 如何让 SwitchCompat 在选中时不改变颜色?

iphone - 如何在 iPhone 中为透明的 PNG 图像着色?

android - Pending Intent 有生命周期吗?

android - 从 Facebook/Instagram 链接时,Angular 7 网站在 android WebView 上崩溃

android - 自定义 ImageView、ImageMatrix.mapPoints() 和 invert() 不准确?

android - 尝试旋转图像时程序停止