Get MotionEvent.getRawX/getRawY of other pointers

LZN picture LZN · Jun 29, 2011 · Viewed 7.8k times · Source

Can I get the value of MotionEvent.getRawX()/getRawY() of other pointers ?

MotionEvent.getRawX() api reference

The api says that uses getRawX/getRawY to get original raw X/Y coordinate, but it only for 1 pointer(the last touched pointer), is it possible to get other pointer's raw X/Y coordinate ?

Answer

Sébastien BATEZAT picture Sébastien BATEZAT · Mar 28, 2012

Indeed, the API doesn't allow to do this, but you can compute it. Try that :

public boolean onTouch(final View v, final MotionEvent event) {

    int rawX, rawY;
    final int actionIndex = event.getAction() >> MotionEvent.ACTION_POINTER_ID_SHIFT;
    final int location[] = { 0, 0 };
    v.getLocationOnScreen(location);
    rawX = (int) event.getX(actionIndex) + location[0];
    rawY = (int) event.getY(actionIndex) + location[1];

}