7

I have an image in image-view control. User can perform pan and zooming on this image, due to that x and y coordinates of image continously change. Is there any way to find out the current set x and y coordinates of image inside image control ? Any code regarding it would be more beneficial.

Muhammad Imran
  • 299
  • 3
  • 5
  • 10
  • 1
    Check it out this link may be helpful..... http://stackoverflow.com/questions/2447564/detect-touch-on-bitmap – Uttam Aug 05 '11 at 05:50

1 Answers1

13

ImageView uses Matrix class to do scaling (Zoom) and translation (pan). If you can get hold of matrix, then you can retrieve the (left, top) coordinate of the zoomed/panned view.

Code for zoomable & pannable extended imageview is available in one of the stackoverflow posts

In that post you will see extended ImageView class has matrix as a member variable used for scaling / translation.

You can use the following Code to get bounds of the zoomed / panned ImagevIew (actually bitmap) from matrix.

RectF r = new RectF(); 
matrix.mapRect(r);
Log.i(TAG, "Rect " + r.left + " " + r.top + " " + r.right + " " + r.bottom + " " + mOverAllScale + " ");

NEWLY ADDED ANSWER

To understand the below code, you need to go thru this link and understand.

Translation

When user drags the image, the screen co-ordinate (Absolute) and Bitmap co-ordinate go out of sync. [ Anything you write to the bitmap gets displayed through canvas (in onDraw()) ] If apply coloring, user would see it getting applied on a different region on the bitmap image. To correct this you need to do translation. The translation would change the user input co-ordinates (which are screen co-ordinates) to bitmap co-ordinates (as intended by the user) Following code snippet would help you do the translation.

Let User input co-ordinates be (x,y) then,

RectF r = new RectF(); 
matrix.mapRect(r);
Log.i(TAG, "Rect " + r.left + " " + r.top + " " + r.right + " " + r.bottom + " " + mOverAllScale + " ");
float newX =  x - r.left;
float newY =  y - r.top;

You use new co-ordinates in place of (x,y).

Zooming (Scaling)

When user pinches in / out ; the bitmap gets Zoomed out / Zoomed in. You must observe that original bitmap does not get changed, zooming is only from display perspective. This again result in sync loss between screen and the bitmap. The coloring applied on screen should get reflected in the displayed image. Following code snippet would help you do the scaling.

// mScalingFactor shall contain the scale/zoom factor
float scaledX = (event.getX() - r.left);
float scaledY = (event.getY() - r.top);

scaledX /= mScalingFactor;
scaledY /= mScalingFactor;

Shash

Community
  • 1
  • 1
Shash316
  • 2,218
  • 18
  • 19
  • Hi @Shash316, This works for me only in translating and scaling. How about the rotation can you help me. This is only my last problem. Thanks much. – donmj Nov 07 '15 at 14:10
  • @Shash316 *Log.i(TAG, "Rect " + r.left + " " + r.top + " " + r.right + " " + r.bottom + " " + mOverAllScale + " ");* - r.left / r.top / r.right / r.bottom are these values in pixels? – Aniruddha Jul 15 '16 at 15:24