Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
411 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...