Blue highlight over an ImageView when the user taps it

ⅰ亾dé卋堺 提交于 2019-12-05 09:51:56

Amazingly, FrameLayout has the foreground attribute which can be used for this. The disadvantage is that you have to add an extra FrameLayout element. The implementation is simple, so until you have time to implement fancy image processing (de-colorize the image a bit, then apply the highlight) and building state drawables dynamically, this seems good enough.

So in my case this would be:

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

@drawable/blue_highlight_image uses a new @color/pressed_image value which is similar with @color/pressed but has an alpha channel, for example: #81CFEB -> #AA81CFEB.

Thanks @Vang for the suggestion.

You are on the right way. Try to use the blue_highlight.xml as your drawable but instead using colors in there use the image you want to show and define a second drawable that adds a color filter to the image when highlighted.

<?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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!