【问题标题】:Blue highlight over an ImageView when the user taps it当用户点击它时,ImageView 上的蓝色突出显示
【发布时间】:2013-11-01 11:29:40
【问题描述】:

我有一个带有 ImageView 和 TextView 的 LinearLayout“卡片”。我希望在用户点击卡片时突出显示卡片。示例见http://www.youtube.com/watch?v=Yx1l9Y7GIk8&feature=share&t=15m17s

通过设置android:background="@drawable/blue_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 提供半透明颜色的触摸高亮效果?

【问题讨论】:

  • 我相信你可以把你的LinearLayout放到FrameLayout中并为其设置foreground属性。

标签: android android-imageview touch-feedback


【解决方案1】:

令人惊讶的是,FrameLayout 具有可用于此目的的 foreground 属性。缺点是您必须添加一个额外的 FrameLayout 元素。实现很简单,所以在您有时间实现花哨的图像处理(对图像进行一点去色,然后应用高光)和动态构建状态可绘制对象之前,这似乎已经足够了。

所以在我的情况下,这将是:

<FrameLayout
    android:foreground="@drawable/blue_highlight_image"
    >
  <ImageView ...>
</FrameLayout>

@drawable/blue_highlight_image 使用新的@color/pressed_image 值,该值与@color/pressed 类似,但有一个alpha 通道,例如:#81CFEB -> #AA81CFEB。

感谢@Vang 的建议。

【讨论】:

  • 对于默认的蓝色,你可以使用这个:?android:attr/selectableItemBackground
  • 这很好用,正如@aleb 所说,我使用 alpha 30% 使它看起来像高亮(30% alpha = 4D in hex)。
【解决方案2】:

你是在正确的方式。尝试使用 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>

【讨论】:

  • 谢谢。可能适用于静态图像。如果图像来自数据库或下载,则更难实现。
  • 你是对的。但你没有提到这一点。为了更加动态,您可以对选择器进行编程。 Here你可以找到一个很好的例子(接受的答案)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-07
  • 2016-01-17
  • 2010-10-16
  • 1970-01-01
  • 2017-07-06
  • 2011-05-30
  • 1970-01-01
相关资源
最近更新 更多