Sunday, 29 September 2013

Dealing with multitouch events in android

Dealing with multitouch events in android

I'm having some trouble with android's multitouch system. I've been to
their docs about how it works and from that I thought this bit of code
here might work (note : pointers is an array of points so that I may track
each point by ID) However, I found that when it came to dragging, it would
only update the pointer with ID as 0. I'm not sure what's going wrong here
(also worth nothing, this method is called from the onTouch() event in
another class and yes, it returns true)
public void tap(MotionEvent e) {
int index = MotionEventCompat.getActionIndex(e);
int ID = MotionEventCompat.getPointerId(e, index);
switch(MotionEventCompat.getActionMasked(e)) {
case MotionEvent.ACTION_DOWN:
pointers[ID] = new Point((int)MotionEventCompat.getX(e, index),
(int)MotionEventCompat.getY(e, index));
break;
case MotionEvent.ACTION_POINTER_DOWN:
pointers[ID] = new Point((int)MotionEventCompat.getX(e, index),
(int)MotionEventCompat.getY(e, index));
break;
case MotionEvent.ACTION_MOVE:
pointers[ID] = new Point((int)MotionEventCompat.getX(e, index),
(int)MotionEventCompat.getY(e, index));
break;
case MotionEvent.ACTION_UP:
pointers[ID] = new Point((int)MotionEventCompat.getX(e, index),
(int)MotionEventCompat.getY(e, index));
break;
}
movementControl.tap(e);
gunControl.tap(e);
}

No comments:

Post a Comment