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.
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
精彩评论