How to implement double tap for surface view in android

前提是你 提交于 2021-02-04 12:27:05

问题


Please tell how to implement double tap for SurfaceView in Android using gesture detector. Can anybody provide code example?


回答1:


You could try following.. actually i tested this and it works pretty well:

1) Extend GestureDetector.SimpleOnGestureListener and override it's onDoubleTap() method:

    class DoubleTapGestureDetector extends GestureDetector.SimpleOnGestureListener {

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

    }

2) Instantiate the 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:

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



回答2:


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



来源:https://stackoverflow.com/questions/7917419/how-to-implement-double-tap-for-surface-view-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!