如何在选择器的可绘制对象中应用着色?

9

我有两张图片(normalpressed),我想在按钮的选择器中设置它们。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" 
        android:drawable="@drawable/recent_pressed" />
    <item
         android:drawable="@drawable/recent" />
</selector>

现在我想在@drawable/recent_pressed图像中应用色彩渲染。

有没有解决方案。

我不想为ImageView创建自定义类,因为这个选择器用作菜单。

我搜索了一下,但是这个链接对我没用。

2个回答

19
创建一个选择器tint_tem.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/white" android:state_pressed="true" />
<item android:color="@color/white" android:state_activated="true" />
<item android:color="@color/green" />

然后在你的xml中,你可以给ImageView添加tint属性:

<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:tint="@color/tint_item"
android:src="@drawable/ic_menu_home" />

你也可以使用textColor属性在TextView上使用这个选择器:

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/tint_item" />

8
您可以创建一个位图并对其应用着色。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <bitmap android:src="@drawable/your_pressed_drawable" android:tint="@color/colorPrimary"/>
    </item>
    <item android:drawable="@drawable/your_second_drawable" />
</selector>

20
只有当可绘制的是位图而不是其他类型的可绘制对象(如矢量可绘制对象)时,才可以这样做。 - Lukas1
非常好的答案,特别是使用了“位图”标签! - bebosh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接