RonenD
RonenD

Reputation: 63

Why motionEvent.getX() return inconsistent values, when using setScaleX()?

I've child view resides in a ViewGroup. I'd like to scale (zoom) the child with a pinch. I'm using setScaleX/Y() to scale the child. The scale is calculated form distance between 2 pointers, in OnTouch(). But when I set the scale of the child it makes motionEvent.getX() inconsistent, which makes the scale to be inconsistent:

case MotionEvent.ACTION_MOVE:
    p1.set(ev.getX(firstPointerIndex), v.getY(firstPointerIndex));
    Log.d(TAG,"Touch xy: " + ev.getX(firstPointerIndex) + "," +  ev.getY(firstPointerIndex));
    this.setScaleX(scale);
    this.setScaleY(scale);
    break;

Output

This is the output of one pointer:

12-09 11:55:14.828: D/IconItem(14408): Touch xy:54.622437, 135.79865
12-09 11:55:14.843: D/IconItem(14408): Touch xy:35.761047, 192.07361
12-09 11:55:14.863: D/IconItem(14408): Touch xy:54.669342, 135.65869
12-09 11:55:14.878: D/IconItem(14408): Touch xy:35.85121, 191.8046
12-09 11:55:14.898: D/IconItem(14408): Touch xy:54.715958, 135.51959
12-09 11:55:14.898: D/IconItem(14408): Touch xy:35.94078, 191.53735
12-09 11:55:14.913: D/IconItem(14408): Touch xy:54.776947, 135.33765

Question

The problem is the jumps 35-54 at x-coord or 135-191 at y-coord. I can't understand where these jumps are coming from. When I don't use setScaleX/Y(), the ev.getX/T() are consistent - no jumps. Could anyone light me? Shouldn't I use these scale methods? Does they affect the ev.getX/Y()? How can I implement the zoom(with pinch) of the view in other way?

Upvotes: 4

Views: 1034

Answers (1)

dsandler
dsandler

Reputation: 2481

Yes, each motion event is given in the view's coordinates, taking into account the view's matrix (which in turn includes the scale). You could try to do your scaling via canvas.scale() instead of scaling the View, as in Making Sense of Multitouch, or possibly by catching the MotionEvents in another view that is not undergoing the scale operation.

Upvotes: 2

Related Questions