Android - 自动覆盖触地/突出显示的图像按钮状态
是否可以使用 ImageButton 实现触摸和突出显示状态,而无需在 Android 上添加 2 个图像资源(考虑到 h/m/ldpi,还需要 6 个图像资源)?我基本上正在寻找类似于 iOS 的行为,其中操作系统可以在按钮的触摸状态上放置半 alpha 覆盖。
我尝试在 onTouch 侦听器中使用 setColorFilter(0xFF000000, Mode.MULTIPLY)
,结果非常接近我想要的结果 - 但我不确定状态处理的最佳方式实现这一点:
即
- touchDown 事件 ->更改颜色叠加。
- touchUp 事件 ->删除颜色叠加,并执行按钮操作。
有没有更好的方法...或者有人可以帮助填补空白?
我不想使用单独的图像有几个原因 - 这是一个 iPhone 端口,我还没有合适的资源,并且考虑到我已经有低/中/高的创意,需要更多的设计时间考虑。
谢谢!
Is it possible using an ImageButton to have touch down and highlighted states, without the need for 2 more image resources on Android (6 more, considering h/m/ldpi)? I'm basically looking for behavior similar to iOS where the OS can put an semi-alpha overlay on the touch down state of a button.
I've tried using setColorFilter(0xFF000000, Mode.MULTIPLY)
in the onTouch listener, and the result was pretty close to what I was after - but I'm not sure the best way of the state handling to achieve this:
i.e.
- touchDown event -> Change color overlay.
- touchUp event -> remove color overlay, and perform button action.
Is there a better way... Or can someone help fill in the gaps?
I don't want to use separate images for a couple of reasons - it's an iPhone port for which I don't have the appropriate assets yet, and would require more designer time considering I've got the low/medium/high creatives to consider.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您对 setColorFilter(0xFF000000, Mode.MULTIPLY) 感到满意,那么如何为自己创建一个扩展 ImageButton 的自定义组件呢?
像这样的东西:(
抱歉我没有机会测试这个)
然后是 view.xml
If you are happy with the
setColorFilter(0xFF000000, Mode.MULTIPLY)
then how about making yourself a custom component that extends ImageButton?Something like:
(Sorry I haven't had a chance to test this)
Then the view.xml
不要使用触摸事件来尝试在此类内容上显示适当的状态。你不可避免地会得到部分错误。该框架做了很多工作来检测用户意图(例如:用户触地但随后滑落手指),同时考虑到诸如倾斜等因素。
对于 ImageButton,您可以使用 src 属性来显示图标,然后使用适当的 StateListDrawable 作为背景。
我创建了一个快速项目供您查看 在 Github 上
请注意,触摸状态显示在背景中,不是前景。请参阅 Google+ 上的讨论,了解为什么 Android 框架工程师认为触摸反馈应该放在后面,而不是放在后面正面。如果您仍然希望可绘制对象位于前面,请参阅我的帖子 在我的博客上
Do not use touch events to try to show the appropriate states on something like this. You will inevitably get it partially wrong. The framework does a lot of work to detect user intent (ex: the user touched down but then slid off his finger) taking into account things like slop etc.
For ImageButton, you can use the src attribute to show the icon and then use an appropriate StateListDrawable as the background.
I have created a quick project for you to see on Github
Note that the touch states are shown in the background, not the foreground. See the discussion on Google+ on why Android framework engineers think touch feedback should be behind, not in front. If you still want the drawable to be in front, see my post on my blog