Android multitouch getting wrong pointers and/or XY values
I have an issue getting the X and Y values from mutlitouch events. Below is the code showing how I get the value when the POINTER_DOWN and POINTER_UP events are fired, however the X and Y values seem to get mixed up / duplicated on the POINTER_UP event.
@Override
public void onTouchEvent(MotionEvent event) {
int id, pointerIndex;
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_POINTER_DOWN:
pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_ID_MASK)
>> MotionEvent.ACTION_POINTER_ID_SHIFT;
id = event.getPointerId(pointerIndex);
Log.e("Down", ""+id+" "+event.getX(id)+" "+event.getY(id));
break;开发者_Go百科
case MotionEvent.ACTION_POINTER_UP:
pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_ID_MASK)
>> MotionEvent.ACTION_POINTER_ID_SHIFT;
id = event.getPointerId(pointerIndex);
Log.e("UP", ""+id+" "+event.getX(id)+" "+event.getY(id));
break;
}
super.onTouchEvent(event);
}
This typically results in the following lolcat:
E/DOWN (25070): 0 279.60922 279.17447
E/DOWN (25070): 1 513.20044 520.3252
E/DOWN (25070): 2 422.6651 358.72418
E/UP (25070): 0 279.60922 279.17447
E/UP (25070): 1 422.6651 358.72418
E/UP (25070): 2 422.6651 358.72418
Here you can see that the XY location for id 1 is wrong, showing instead the values id 2.
Note that no ACTION_CANCEL events are called. I imagine it's something wrong with my use of the MASKS/ANDing. Any help would be much appreciated!
Apparently event.getX and event.getY should be fed the pointer index, not the pointer id.
精彩评论