Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get MotionEvent.getRawX/getRawY of other pointers

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 ?

like image 398
LZN Avatar asked Jun 29 '11 08:06

LZN


2 Answers

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];

}
like image 187
Sébastien BATEZAT Avatar answered Oct 23 '22 03:10

Sébastien BATEZAT


A solution worth trying for most use cases is to add this to the first line of the onTouchEvent it simply finds the difference between the raw and processed, and shifts the location of the MotionEvent event, by that amount. So that all the getX(int) values are now the raw values. Then you can actually use the getX() getY() stuff as the Raw values.

event.offsetLocation(event.getRawX()-event.getX(),event.getRawY()-event.getY());

While Ivan's point is valid, it's simply the case that applying a matrix directly to the view itself sucks so bad you likely shouldn't do it. It's weird and inconsistent between devices, cause the touch events to fall out of view and get declined, etc. If you are moving a view around like that you are better off simply overloading the onDraw() and applying that matrix to the canvas, then applying the inverse matrix to the MotionEvent so everything meshes up right. Then you can properly react to the events with proper and fine grain control. And, if you do that, my solution here wouldn't be subject to Ivan's objection.

like image 23
Tatarize Avatar answered Oct 23 '22 02:10

Tatarize