需要为图像视图设置色调…我使用它的方式如下:
imageView.setColorFilter(R.color.blue,android.graphics.PorterDuff.Mode.MULTIPLY);
但这并没有改变……
需要为图像视图设置色调…我使用它的方式如下:
imageView.setColorFilter(R.color.blue,android.graphics.PorterDuff.Mode.MULTIPLY);
但这并没有改变……
当前回答
大多数答案指的是使用setColorFilter,这不是最初的问题。
用户@Tad的答案是正确的,但它只适用于API 21+。
要设置所有Android版本的色调,使用ImageViewCompat:
ImageViewCompat.setImageTintList(imageView, ColorStateList.valueOf(yourTint));
注意,在这种情况下,yourTint必须是一个“颜色int”。如果你有一个像r。color这样的颜色资源。蓝色,你需要先加载颜色int:
ContextCompat.getColor(context, R.color.blue);
其他回答
不要使用波特达夫。模式, 使用setColorFilter()它适用于所有情况。
ImageView imageView = (ImageView) listItem.findViewById(R.id.imageView);
imageView.setColorFilter(getContext().getResources().getColor(R.color.msg_read));
如果你想将选择器设置为你的色调:
ImageViewCompat.setImageTintList(iv, getResources().getColorStateList(R.color.app_icon_click_color));
大多数答案指的是使用setColorFilter,这不是最初的问题。
用户@Tad的答案是正确的,但它只适用于API 21+。
要设置所有Android版本的色调,使用ImageViewCompat:
ImageViewCompat.setImageTintList(imageView, ColorStateList.valueOf(yourTint));
注意,在这种情况下,yourTint必须是一个“颜色int”。如果你有一个像r。color这样的颜色资源。蓝色,你需要先加载颜色int:
ContextCompat.getColor(context, R.color.blue);
我在派对上迟到了,但我没有看到我的解决方案。我们能够通过setImageResource()设置色调颜色,太(我的minSdkVersion是24)。
因此,首先,您需要创建一个选择器,并将其保存在/drawable资产文件夹(我称之为ic_color_white_green_search.xml)
<!-- Focused and not pressed -->
<item android:state_focused="true"
android:state_pressed="false">
<bitmap android:src="@drawable/ic_search"
android:tint="@color/branding_green"/>
</item>
<!-- Focused and pressed -->
<item android:state_focused="true"
android:state_pressed="true">
<bitmap android:src="@drawable/ic_search"
android:tint="@color/branding_green"/>
</item>
<!-- Default -->
<item android:drawable="@drawable/ic_search"/>
然后像这样在代码中设置它:
val icon = itemView.findViewById(R.id.icon) as ImageButton
icon.setImageResource(R.drawable.ic_color_white_green_search)
我发现我们可以为tint attr使用颜色选择器:
mImageView.setEnabled(true);
activity_main.xml:
<ImageView
android:id="@+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_arrowup"
android:tint="@color/section_arrowup_color" />
section_arrowup_color.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@android:color/white" android:state_enabled="true"/>
<item android:color="@android:color/black" android:state_enabled="false"/>
<item android:color="@android:color/white"/>
</selector>