Android: how to detect touch location on ImageView if the image view is scaled by matrix?

hzxu picture hzxu · May 18, 2011 · Viewed 13.3k times · Source

I set OnTouchListener of an ImageView and implement onTouch method, but if the image is scaled using matrix, how do I calculate the location of the touch?

Or does the motion event automatically takes that into account when returning getX() and getY() ?

Answer

zorgbargle picture zorgbargle · Sep 14, 2011

getX and getY will return the touch location in the ImageView's coordinate system. If you're looking for the point within the image's coordinate system, you can use the inverse matrix of the matrix used by the ImageView. I've done something like the following:

// calculate inverse matrix
Matrix inverse = new Matrix();
imageView.getImageMatrix().invert(inverse);

// map touch point from ImageView to image
float[] touchPoint = new float[] {event.getX(), event.getY()};
inverse.mapPoints(touchPoint);
// touchPoint now contains x and y in image's coordinate system