Reputation: 257
I'm trying to understand how multi-touch events work, but my code is not acting as I anticipated. Multiple touches are only recognized if I rapidly subsequently place three or more fingers on the screen:
@Override
public boolean onTouchEvent(MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
doSomeUnimportantStuff();
return true;
case MotionEvent.ACTION_POINTER_DOWN:
for (int i = 0;i < event.getPointerCount()-1;i++) {
Log.d("Multi-Touch", Float.toString(event.getX(i)));
return true;
}
}
return true;
}
Why isn't the second finger I place on the screen recognized, and why does the speed at which I place my fingers on the screen affect whether the multiple touches are recognized?
Upvotes: 0
Views: 103
Reputation: 652
According to the android docs:
You should always use the method getActionMasked(event) (or better yet, the compatability version MotionEventCompat.getActionMasked(event)to retrieve the action of a MotionEvent
As for knowing if you have a single or multi touch event, use the following code snippet, again taken from the android docs. You basically check to see if there is more than one pointer, if there is, then obviously there is more than one touch on the screen.
if (event.getPointerCount() > 1) {
//MULTI TOUCH EVENT IS TRIGGERED!
// The coordinates of the current screen contact, relative to
// the responding View or Activity.
xPos = (int)MotionEventCompat.getX(event, index);
yPos = (int)MotionEventCompat.getY(event, index);
} else {
// Single touch event
xPos = (int)MotionEventCompat.getX(event, index);
yPos = (int)MotionEventCompat.getY(event, index);
}
Upvotes: 2