Android : 多点触控和 TYPE_SYSTEM_OVERLAY [英] Android : Multi touch and TYPE_SYSTEM_OVERLAY
问题描述
我试图在系统叠加视图上获得多个触摸事件,但我只收到 MotionEvent.ACTION_OUTSIDE
事件.
I am trying to get multiple touch events on a system overlay view, but am only receiving the MotionEvent.ACTION_OUTSIDE
event.
有没有办法在系统叠加层上获取多个触摸事件?
Is there any possible way of getting multiple touch events on a system overlay?
任何示例或链接都会非常有帮助.
Any examples or links would be very helpful.
推荐答案
要创建叠加视图,在设置LayoutParams
时需要将类型设置为TYPE_SYSTEM_OVERLAY
并使用标志 FLAG_WATCH_OUTSIDE_TOUCH
.这带来了一个问题,因为正如 Android 文档所述:
To create an overlay view, when setting up the LayoutParams
you need to set the type to TYPE_SYSTEM_OVERLAY
and use the flag FLAG_WATCH_OUTSIDE_TOUCH
. This presents a problem because as the Android documentation states:
您不会收到完整的向下/移动/向上手势,只会收到第一个向下的位置作为 ACTION_OUTSIDE
.
you will not receive the full down/move/up gesture, only the location of the first down as an
ACTION_OUTSIDE
.
为了接收完整的触摸事件数组,您需要使用 TYPE_SYSTEM_ALERT
类型,但这会导致覆盖层接管屏幕并停止与其他元素的交互.解决方案是同时使用TYPE_SYSTEM_OVERLAY
和TYPE_SYSTEM_ALERT
,并根据需要通过更改LayoutParams
的类型在它们之间进行切换.
In order to receive the full array of touch events you need to use the TYPE_SYSTEM_ALERT
type, but this causes the overlay to take over the screen and stop interaction with other elements. The solution is to use both TYPE_SYSTEM_OVERLAY
and TYPE_SYSTEM_ALERT
and switch between them by changing the type of the LayoutParams
as needed.
这是通过以下方式完成的:
This is accomplished by:
- 注意
ACTION_OUTSIDE
动作事件. - 当它发生时,测试它是否发生在覆盖层内.
- 如果是,请将
LayoutParams
类型切换为TYPE_SYSTEM_ALERT
- 与叠加层的交互完成后,切换回
TYPE_SYSTEM_OVERLAY
- 重复
- Watch for the
ACTION_OUTSIDE
motion event. - When it occurs, test if it occured within the overlay.
- If it did, switch the
LayoutParams
type toTYPE_SYSTEM_ALERT
- Once the interaction with the overlay is complete, switch back to
TYPE_SYSTEM_OVERLAY
- Repeat
要记住的一件事是 ACTION_OUTSIDE
动作事件总是传递给屏幕上的其余元素.因此,例如,如果叠加层位于按钮的顶部,则该按钮也将接收到动作事件并且无法阻止它.
The one thing to keep in mind is that the ACTION_OUTSIDE
motion event is always passed on to the rest of the elements on the screen. So, for example, if the overlay is on top of a button, that button will also receive the motion event and there is no way to stop it.
还要确保将 SYSTEM_ALERT_WINDOW
权限添加到 mainifest 文件中.
Also make sure you add the SYSTEM_ALERT_WINDOW
permission to the mainifest file.
我已经在这里发布了完整的解决方案:
http://www.jawsware.mobi/code_OverlayView/ (更新)
I've posted the complete solution here:
http://www.jawsware.mobi/code_OverlayView/ (UPDATED)
它包括所有源代码和下载整个示例项目的链接.
It includes all the source code and a link to download the entire example project.
Android 4.0 更新 - 2013 年 1 月 3 日
要创建叠加视图,请在设置 LayoutParams 时不要将类型设置为 TYPE_SYSTEM_OVERLAY
.
To create an overlay view, when setting up the LayoutParams DON'T set the type to TYPE_SYSTEM_OVERLAY
.
改为将其设置为 TYPE_PHONE
.
使用以下标志:
FLAG_NOT_TOUCH_MODAL
FLAG_WATCH_OUTSIDE_TOUCH
FLAG_NOT_TOUCH_MODAL
<<我发现这一点非常重要.没有它,焦点将放在覆盖层上,并且软键(主页、菜单等)按下不会传递到下面的活动.
FLAG_NOT_TOUCH_MODAL
<< I found this one to be quite important. Without it, focus is given to the overlay and soft-key (home, menu, etc.) presses are not passed to the activity below.
此外,之前的链接(上方)已更新以反映此更新.
这篇关于Android : 多点触控和 TYPE_SYSTEM_OVERLAY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!