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
。
为了接收完整的数组触摸事件需要使用 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更新 - 1/3/2013
要创建重叠视图,当设置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.
此外,以前的链接(上)已更新以反映此更新。
Also, the previous link (above) has been updated to reflect this update.
这篇关于Android:多点触控和TYPE_SYSTEM_OVERLAY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!