jorgepr
jorgepr

Reputation: 307

GestureOverlayView is intercepting common gestures, how to avoid this?

I'm developing an app which needs to detect common (double tap, fling, etc) and custom gestures (circle). To achieve this i'm using the GestureOverlayView as you can see in my code:

private GestureDetectorCompat mGestureDetector;
private GestureLibrary mGestureLibrary;

public void onCreate(Bundle bundle) {   
    super.onCreate(bundle);
    setContentView(R.layout.main);

    mGestureDetector = new GestureDetectorCompat(this, mGestureListener);
    mGestureLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);

    if (!mGestureLibrary.load()) {
        Log.e(TAG, "unable to load the custom gestures");    
        finish();
    }

    GestureOverlayView gestureOverlay = (GestureOverlayView) findViewById(R.id.gestures_overlay);
    gestureOverlay.addOnGesturePerformedListener(mGesturePerformedListener);
}

private final SimpleOnGestureListener mGestureListener = new SimpleOnGestureListener(){
    public boolean onDoubleTap(MotionEvent event) {
        // DO STUFF             
        return true;        
    }
};


private final OnGesturePerformedListener mGesturePerformedListener = new OnGesturePerformedListener() {
    public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
        // DO MORE STUFF
    }       
};


public boolean onTouchEvent(MotionEvent event){
    return mGestureDetector.onTouchEvent(event) || super.onTouchEvent(event);       
} 

And the layout:

<?xml version="1.0" encoding="utf-8"?>    
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frame"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<SurfaceView android:id="@+id/camera_preview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

<android.gesture.GestureOverlayView
    android:id="@+id/gestures_overlay"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gestureColor="#00000000" 
    android:uncertainGestureColor="#00000000"
    android:gestureStrokeType="multiple"
    android:eventsInterceptionEnabled="false"/>

</FrameLayout>

I have no problems detecting the custom gestures, but looks like the GestureOverlayView is intercepting the events so the onTouchEvent method isn't reached and the stuff related to the double tap is never executed.

I set the eventsInterceptionEnabled attribute to false in the GestureOverlayView configuration but the code is still not working.

Any ideas?

Upvotes: 3

Views: 2439

Answers (1)

Dany Joumaa
Dany Joumaa

Reputation: 2040

According to the Android docs for onTouchEvent:

Called when a touch screen event was not handled by any of the views under it. This is most useful to process touch events that happen outside of your window bounds, where there is no view to receive it.

So that rules out onTouchEvent as a way to capture the double tap without further modification. However, you can change the way the Activity handles touch events to ensure your GestureDetector works as you expect. Add the following method to your activity:

@Override
public boolean dispatchTouchEvent(MotionEvent e)
{
    detector.onTouchEvent(e);

    return super.dispatchTouchEvent(e);
}

Upvotes: 2

Related Questions