Android - How to distinguish the first pointer and the second pointer move in onTouchEvent

I want to process a gesture in my activity. To do this, I override the public boolean onTouchEvent (MotionEvent MEvent) method in my work. The content is as follows:

motionaction = MEvent.getAction(); if(motionaction == MotionEvent.ACTION_DOWN) { ... return true; } if(motionaction == MotionEvent.ACTION_UP) { ... return true; } if(motionaction == MotionEvent.ACTION_MOVE) { ... return true; } motionaction = MEvent.getActionMasked(); if(motionaction == MotionEvent.ACTION_POINTER_DOWN) { ... return true; } if(motionaction == MotionEvent.ACTION_POINTER_UP) { ... return true; } return true; 

Gesture of this:

-finger1 on the screen holds its position (almost because there is always a slight movement)

-finger2 moves around the screen. This is the movement that I want to capture.

I can capture action 5, but the problem is that when two fingers are on the screen, ACTION_MOVE captures the movement of both the first and second fingers. The MEvent.getActionIndex () method does not work for ACTION_MOVE, which always returns 0; The only thing I can do is to maintain the position of finger1 and discard the movement near this point. As a result, this is not the ideal movement of the finger2 once, which it “ruined” with the movement of the small finger1, because although the finger holds its position on the screen, the listener feels every minimal movement.

How can I improve this?

+1
source share
2 answers

Referring to Quintin Balsdon's answer :

There is a similar thing in my code. I keep the position of finger1 in the case of ACTION_DOWN, and then, when the motion of finger2, I see if the Y coordinate corresponds to the stored coordinate of finger Y. If so, the movement that it refers to finger2, otherwise, refers to finger1, and I refuse this in two-finger mode.

If I try to draw a circle on my view in one finger mode, I have something like this: http://img208.imageshack.us/img208/8113/onefingercircle.jpg

If I try to draw a circle on my two-finger view, I have something like this: http://img256.imageshack.us/img256/6778/twofingercircle.jpg

Thus, in single-finger mode, it works fine, but not in two-finger mode. I do not know if this is due to the multi-touch processor of the phone or the touch screen. This can only be due to hardware or to my misunderstanding of the API.

+1
source

This is managed by you as a developer. You will need to create some boolean variable to set (to true) when the "DOWN" action occurs and then cancel (false) when the "UP" or "MOVE" event occurs. In the code below, I save the coordinates when "DOWN" even occurs and performs adjustments while the user is moving.

 switch (e.Action) //e is a MotionEvent type { case MotionEvent.ACTION_DOWN: { _prevx = e.getX(); _prevy = e.getY(); } break; case MotionEvent.ACTION_MOVE: { _xoffset += e.GetX() - _prevx; _yoffset += e.GetY() - _prevy; Invalidate(); _prevx = e.GetX(); _prevy = e.GetY(); } break; } 

If you want to perform a multi-finger drag, you need to implement ScaleGestureDetector.OnScaleGestureListener (http://developer.android.com/reference/android/view/ScaleGestureDetector.OnScaleGestureListener.html)

0
source

Source: https://habr.com/ru/post/919794/


All Articles