简体   繁体   English

如何在android中实现双击表面视图

[英]How to implement double tap for surface view in android

Please tell how to implement double tap for SurfaceView in Android using gesture detector.请告诉如何使用手势检测器在 Android 中为SurfaceView实现双击。 Can anybody provide code example?有人可以提供代码示例吗?

You could try following.. actually i tested this and it works pretty well:您可以尝试以下...实际上我对此进行了测试,并且效果很好:

1) Extend GestureDetector.SimpleOnGestureListener and override it's onDoubleTap() method: 1)扩展GestureDetector.SimpleOnGestureListener并覆盖它的onDoubleTap()方法:

    class DoubleTapGestureDetector extends GestureDetector.SimpleOnGestureListener {

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            Log.d("TAG", "Double Tap Detected ...");
            return true;
        }

    }

2) Instantiate the GestureDetector : 2)实例化GestureDetector

final GestureDetector mGesDetect = new GestureDetector(this, new DoubleTapGestureDetector());

3) Set an OnTouchListener on your SurfaceView , override its onTouch() method and call the onTouchEvent() on your GestureDetector object: 3)SurfaceView上设置OnTouchListener ,覆盖其onTouch()方法并在GestureDetector对象上调用onTouchEvent()

    surfview.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            mGesDetect.onTouchEvent(event);
            return true;
        }
    });

I have tried above solution.我已经尝试过上述解决方案。 But, it didn't work for my case.但是,它对我的​​情况不起作用。 I got the touch event in surface view touch listener , but didn't find any callback either in onDoubleTap or onSingleTapConfirmed methods.我在表面视图触摸监听器中得到了触摸事件,但在onDoubleTaponSingleTapConfirmed方法中都没有找到任何回调。 My Code is given below:我的代码如下:

final GestureDetector gestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener(){
        @Override
        public boolean onDoubleTap(MotionEvent e) {
            // code here
            return super.onDoubleTap(e);
        }

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            // code here
            return super.onSingleTapConfirmed(e);
        }

    });

surfaceView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return gestureDetector.onTouchEvent(event);
        }
    });

When I made the surface view clickable, it worked.当我使表面视图可点击时,它起作用了。

 surfaceView.setClickable(true);

Detailed explanation can be found in this link可以在此链接中找到详细说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM