Android 当用户轻触图像视图时,图像视图上的蓝色高亮显示
我有一张带有图像视图和文本视图的线性布局“卡”。我希望当用户点击卡片时,它会被高亮显示。有关示例,请参见 通过设置Android 当用户轻触图像视图时,图像视图上的蓝色高亮显示,android,android-imageview,touch-feedback,Android,Android Imageview,Touch Feedback,我有一张带有图像视图和文本视图的线性布局“卡”。我希望当用户点击卡片时,它会被高亮显示。有关示例,请参见 通过设置android:background=“@drawable/blue\u highlight”,可以很容易地为TextView完成此操作。以下是res/drawable/blue_highlight.xml: <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.
android:background=“@drawable/blue\u highlight”
,可以很容易地为TextView完成此操作。以下是res/drawable/blue_highlight.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@color/selected"/>
<item android:state_pressed="true" android:drawable="@color/pressed"/>
<item android:drawable="@color/bg_window"/>
</selector>
但这不适用于ImageView,因为图像在前面,背景不可见。如何在ImageView中使用半透明颜色实现触摸高光效果?您的方法是正确的。尝试使用blue_highlight.xml作为可绘制文件,但在其中使用颜色,使用要显示的图像,并定义第二个可绘制文件,在突出显示时为图像添加颜色过滤器
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@drawable/yourdrawable_with_colorfilter"/>
<item android:state_pressed="true" android:drawable="@drawable/yourdrawable_with_colorfilter"/>
<item android:drawable="@drawable/your_drawable"/>
</selector>
令人惊讶的是,FrameLayout具有可用于此目的的属性。缺点是必须添加额外的FrameLayout元素。实现很简单,因此,在您有时间实现奇特的图像处理(稍微对图像进行去彩色处理,然后应用高光)和动态构建状态绘图功能之前,这似乎已经足够好了 因此,在我的情况下,这将是:
<FrameLayout
android:foreground="@drawable/blue_highlight_image"
>
<ImageView ...>
</FrameLayout>
@可绘制/蓝色突出显示图像使用一个新的@color/pressed_图像值,该值与@color/pressed类似,但有一个alpha通道,例如:#81CFEB->#AA81CFEB
感谢@Vang的建议。我相信您可以将线性布局放入FrameLayout并为其设置前景属性。谢谢。可能对静态图像非常有用。如果图像来自数据库或下载,则更难实现。你是对的。但你没有提到这一点。为了更具动态性,您可以对选择器进行编程。您可以找到一个很好的示例(公认的答案)。对于默认的蓝色,您可以使用以下颜色:
?android:attr/selectableItemBackground
。这非常有效,正如@aleb提到的,我使用alpha 30%使其看起来像高亮显示(30%alpha=4D,十六进制)。