I've found an even easier way. Just make an overlay as the first overlay in the list that does not draw anything and use it to recognize gestures using the GestureDetector. It should then return true if it handled the event so it doesn't get propagated.
List<Overlay> overlays = mapView.getOverlays();
overlays.clear();
overlays.add(new MapGestureDetectorOverlay(new MyOnGestureListener()));
And here's the class:
public class MapGestureDetectorOverlay extends Overlay implements OnGestureListener {
private GestureDetector gestureDetector;
private OnGestureListener onGestureListener;
public MapGestureDetectorOverlay() {
gestureDetector = new GestureDetector(this);
}
public MapGestureDetectorOverlay(OnGestureListener onGestureListener) {
this();
setOnGestureListener(onGestureListener);
}
@Override
public boolean onTouchEvent(MotionEvent event, MapView mapView) {
if (gestureDetector.onTouchEvent(event)) {
return true;
}
return false;
}
@Override
public boolean onDown(MotionEvent e) {
if (onGestureListener != null) {
return onGestureListener.onDown(e);
}
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
if (onGestureListener != null) {
return onGestureListener.onFling(e1, e2, velocityX, velocityY);
}
return false;
}
@Override
public void onLongPress(MotionEvent e) {
if (onGestureListener != null) {
onGestureListener.onLongPress(e);
}
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
if (onGestureListener != null) {
onGestureListener.onScroll(e1, e2, distanceX, distanceY);
}
return false;
}
@Override
public void onShowPress(MotionEvent e) {
if (onGestureListener != null) {
onGestureListener.onShowPress(e);
}
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
if (onGestureListener != null) {
onGestureListener.onSingleTapUp(e);
}
return false;
}
public boolean isLongpressEnabled() {
return gestureDetector.isLongpressEnabled();
}
public void setIsLongpressEnabled(boolean isLongpressEnabled) {
gestureDetector.setIsLongpressEnabled(isLongpressEnabled);
}
public OnGestureListener getOnGestureListener() {
return onGestureListener;
}
public void setOnGestureListener(OnGestureListener onGestureListener) {
this.onGestureListener = onGestureListener;
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…