android - GestureOverlayView 正在拦截常见的手势,如何避免这种情况?

标签 android gestures

我正在开发一款需要检测常见手势(双击、快速滑动等)和自定义手势(圆圈)的应用。为实现这一点,我使用了 GestureOverlayView,如您在我的代码中所见:

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);       
} 

布局:

<?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>

我在检测自定义手势方面没有问题,但看起来 GestureOverlayView 正在拦截事件,因此未到达 onTouchEvent 方法并且与双击相关的内容从未执行。

我在 GestureOverlayView 配置中将 eventsInterceptionEnabled 属性设置为 false,但代码仍然无法正常工作。

有什么想法吗?

最佳答案

根据 onTouchEvent 的 Android 文档:

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.

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

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

    return super.dispatchTouchEvent(e);
}

关于android - GestureOverlayView 正在拦截常见的手势,如何避免这种情况?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18038446/

相关文章:

android - 如何在 android web View 中用另一个浏览器替换默认的 webkit 浏览器?

java - RSS 阅读器为 ListView 创建自定义适配器

jquery - 允许使用 jQuery.Wipetouch 进​​行捏合缩放

android - 滑动手势输入数字

android - 在 Android 上使用 HTTPClient 进行多个 HTTP 连接的最佳方式

android - glide v4 中的 FifoPriorityThreadPoolExecutor

google-chrome - 在 Chrome 浏览器窗口之间切换

ios - Mapbox ios SDK 子类 contentOffset 和 contentSize 或 UIpanGestures

iOS:使用 2 个手指时取消 UIScrollView 触摸

android - 以编程方式单击通知