-1

This is the code I made for my onTouchListener:

if(event.getAction() == MotionEvent.ACTION_UP) {
                Log.i("", "double touch  : " + timeLastTouch + "... time passed: " + (System.currentTimeMillis() - timeLastTouch));
                if (System.currentTimeMillis() - timeLastTouch < 500) {
                    Log.i("", "double touch TRUE");
                    timeLastTouch = System.currentTimeMillis();
                    return true;
                } else {
                    Log.i("", "double touch FALSE");
                    timeLastTouch = System.currentTimeMillis();
                    return false;
                }
            }else if(event.getAction() == MotionEvent.ACTION_DOWN) {
                Log.i("", "double touch DOWN : " + timeLastTouch + "... time passed: " + (System.currentTimeMillis() - timeLastTouch));
                if (System.currentTimeMillis() - timeLastTouch < 500) {
                    Log.i("", "double touch TRUE DOWN");
                    timeLastTouch = System.currentTimeMillis();
                    return true;
                }else {
                    Log.i("", "double touch FALSE DOWN");
                    return false;
                }
            }else {
                timeLastTouch = System.currentTimeMillis();
                Log.i("","double touch ELSE FALSE");
                return false;
            }

But it doesn't work. This view is over a Surface view. Now I only need to send the events to the SurfaceView for pinching (pinch to zoom) Now I tried to make the logic, so that if it has double touch, then I return true (it has consumed the touch, so then the surface view, doesn't get the touch). BUT if I do this, the pinch to zoom doesn't work neither. If I remove this logic, pinch zoom, works, so I added some logs, and followed them. I noticed that for pinch zoom, you get 2 down events, and only after the ACTION_MOVE is being called. Presumably, because when you pinch to zoom, you put down 2 fingers, and then zoom. MY problem is that my code was setting it, for the second Action_down to consume the touch, which means the pinch to zoom doesn't start. Any ideeas how to fix this?

rosu alin
  • 5,674
  • 11
  • 69
  • 150
  • You might want to look at [existing pinch implementations](https://android-arsenal.com/search?q=pinch) and see how they do it. – CommonsWare Jul 21 '17 at 13:32
  • Well, I don't need to implement the pinch logic. that already exists. on the view that is below my view. All I want is so my view consumes the touch for double tap, but not for pinch. (logging the event.action) returns : 0 0 1 (action_down, action_down, action_up) for double tap, and returns 0 0 2/1 (action_down, action_down, move or up) for pinching. Hence my issue. Cause If I block the second Action_down event, I won't get the pinch zoom. If I don't block it, it's being sent down to the next view, and that MUST not happen – rosu alin Jul 21 '17 at 14:49
  • Hence I need a logic to know when a pinch-zoom happens, before the MotionEvent for "move" is being called. and I tried using event.getIndexCounter() or check if it is multi touch, but it just returns 1 for me – rosu alin Jul 21 '17 at 14:50

1 Answers1

0

I did manage to do this, with the following logic:

viewTop.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Log.i("","double touch event action: ===========================");
            Log.i("","double touch event action:" + event.getAction());
            if(event.getAction() == MotionEvent.ACTION_DOWN) {
                Log.i("", "double touch DOWN : " + timeLastTouch + "... time passed: " + (System.currentTimeMillis() - timeLastTouch) + "..... location diff: " + (event.getX() - xLastTouch));
                if (System.currentTimeMillis() - timeLastTouch < 1000 && Math.abs(event.getX() - xLastTouch) < 150 && Math.abs(event.getY() - yLastTouch) < 150) {
                    Log.i("", "double touch TRUE DOWN");
                    timeLastTouch = System.currentTimeMillis();
                    xLastTouch = event.getX();
                    yLastTouch = event.getY();
                    return true;
                } else {
                    timeLastTouch = System.currentTimeMillis();
                    xLastTouch = event.getX();
                    yLastTouch = event.getY();
                    Log.i("", "double touch FALSE DOWN");
                    return false;
                }
            }else if(event.getAction() == MotionEvent.ACTION_UP) {
                timeLastTouch = System.currentTimeMillis();
                xLastTouch = -1f;
                yLastTouch = -1f;
                return false;
            }else {
                timeLastTouch = System.currentTimeMillis();
                xLastTouch = event.getX();
                yLastTouch = event.getY();
                Log.i("","double touch ELSE FALSE");
                return false;
            }
        }
    });

But if I double tap, and the second tap is in another place, it will happen. Not so happy about that, so I need to look more into this, so I won't just yet accept this answer. I somehow need to know if I consume or not the second Action_Down, before the action_move is being called by the pinch.

rosu alin
  • 5,674
  • 11
  • 69
  • 150