3

I have a custom layout pinch zoom code as a parent and a child layout that handle the click feature. Therefore I use on touch intercept, but the problem is that with this it would not know when to click or drag.

 @Override
    public boolean onInterceptTouchEvent(MotionEvent ev){


    switch (ev.getAction() & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
            startClickTime = System.currentTimeMillis(); //start time when first finger land
            Log.i("Zoom", " actionDown");
            if ( scale > MIN_ZOOM){
                mode = Mode.DRAG;
                startX = ev.getX() - prevDx;
                startY = ev.getY() - prevDy;
            }
            return false; //go to child layout

        case MotionEvent.ACTION_POINTER_DOWN:
            mode = Mode.ZOOM;
            return true;

        case MotionEvent.ACTION_UP:
            long clickDuration = System.currentTimeMillis() - startClickTime;
            mode = Mode.NONE;
            if(clickDuration < MAX_CLICK_DURATION){
                return false;
            }
            else {
                // letting go from drag or zooming
                return true;
            }

        case MotionEvent.ACTION_MOVE:
            clickDuration = System.currentTimeMillis() - startClickTime;
            if (clickDuration > MAX_CLICK_DURATION){
                return true;
            }
            else {
                return false;
            }
    }
    return false;
}

In my child layout for click feature:

switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {

                case MotionEvent.ACTION_DOWN:
                        // primary finger down
                     return true;

                case MotionEvent.ACTION_POINTER_DOWN:
                        // non-primary finger down
                    return false;

                case MotionEvent.ACTION_CANCEL:
                    return false;

                case MotionEvent.ACTION_UP:
                    // primary finder up
                    Intent intent = new Intent(context, DeviceActivity.class);
                    context.startActivity(intent);
                    return true;

                case MotionEvent.ACTION_POINTER_UP:
                    // non-primary finger up
                    return false;
            }

So is there a way to distinguish drag and click.

Zheng Xian
  • 310
  • 1
  • 3
  • 13

2 Answers2

0

There are four states in a click and drag events: Started -> Continuing -> Dropped -> Ended

The DragEvent class provides integers representing them:

  • ACTION_DRAG_STARTED
  • ACTION_DRAG_ENTERED
  • ACTION_DRAG_LOCATION
  • ACTION_DRAG_EXITED
  • ACTION_DROP
  • ACTION_DRAG_ENDED

Always remember that a drag operation requires a selected item on the screen (you are picking up something and moving). But in zooming you don't have a selected item, but what you get instead is the view. Using this property you can distinguish between the Drag and Zoom operations.

Sampath
  • 1,144
  • 1
  • 21
  • 38
0

Try this:

final GestureDetector gestureDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener() {
public void onLongPress(MotionEvent e) {
    Log.e("", "Longpress detected");
}
});

public boolean onTouchEvent(MotionEvent event) {
   return gestureDetector.onTouchEvent(event);
};
  • 3
    Why should the OP "try this code"? A **good answer** will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO. – B001ᛦ Jul 08 '16 at 09:36