Search code examples
javaandroidandroid-mapviewgesturepinch

Android catch two fingers tap on MapView


In my code I want to add zoom in on double-tap and zoom out on two-fingers-tap (like Google Maps). I'm using this code:

gestureDetector = new GestureDetector(new DoubleTapDetector());
touchListener = new View.OnTouchListener() {
    public boolean onTouch(View view, MotionEvent motionEvent) {
        final int action = motionEvent.getAction();
        final int fingersCount = motionEvent.getPointerCount();

        if ((action == MotionEvent.ACTION_POINTER_UP) && (fingersCount == 2)) {
            onTwoFingersTap();

            return false;
        }

        return gestureDetector.onTouchEvent(motionEvent);
    }
};

Double-tap works fine, but when I trying to pinch map, it zoom as usual but zoom out by one step because of onTwoFingersTap(); is catched too.

How can I avoid this?


Solution

  • return true if your condition is satified:

        touchListener = new View.OnTouchListener() {    
        public boolean onTouch(View view, MotionEvent motionEvent) {    
            final int action = motionEvent.getAction();       
            final int fingersCount = motionEvent.getPointerCount();        
            if ((action == MotionEvent.ACTION_POINTER_UP) && (fingersCount == 2)) {             
                onTwoFingersTap();       
                return true;         
            } 
         return gestureDetector.onTouchEvent(motionEvent);     
        } 
     };