3

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?

4

1 回答 1

2

根据 Android 文档onTouchEvent

当触摸屏事件未被其下的任何视图处理时调用。这对于处理在窗口边界之外发生的触摸事件最有用,因为没有视图可以接收它。

因此,这排除onTouchEvent了一种无需进一步修改即可捕获双击的方式。但是,您可以更改 Activity 处理触摸事件的方式,以确保您的 GestureDetector 按预期工作。将以下方法添加到您的活动中:

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

    return super.dispatchTouchEvent(e);
}
于 2013-08-04T00:19:47.513 回答