Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
453 views
in Technique[技术] by (71.8m points)

events - Android : Multi touch and TYPE_SYSTEM_OVERLAY

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

you will not receive the full down/move/up gesture, only the location of the first down as an ACTION_OUTSIDE.

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:

  1. Watch for the ACTION_OUTSIDE motion event.
  2. When it occurs, test if it occured within the overlay.
  3. If it did, switch the LayoutParams type to TYPE_SYSTEM_ALERT
  4. Once the interaction with the overlay is complete, switch back to TYPE_SYSTEM_OVERLAY
  5. Repeat

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.

Also make sure you add the SYSTEM_ALERT_WINDOW permission to the mainifest file.

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.


Update for Android 4.0 - 1/3/2013


To create an overlay view, when setting up the LayoutParams DON'T set the type to TYPE_SYSTEM_OVERLAY.

Instead set it to TYPE_PHONE.

Use the following flags:

FLAG_NOT_TOUCH_MODAL

FLAG_WATCH_OUTSIDE_TOUCH

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...