Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get x and y coordinate of bitmap image in image control?

Tags:

android

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.

like image 676
Muhammad Imran Avatar asked Aug 05 '11 05:08

Muhammad Imran


People also ask

What are the X and Y coordinates of an image?

Image or graphics coordinates The origin of the image or graphics coordinate system is the bottom-left corner of an image. The x coordinate extends from left to right; the y coordinate extends upward.

What is the Y coordinate of the image?

The y coordinate is a given number of pixels along the vertical axis of a display starting from the pixel (pixel 0) at the top of the screen.


1 Answers

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

like image 124
Shash316 Avatar answered Nov 15 '22 01:11

Shash316