简体   繁体   中英

How to get the origin for drawable image in an imageview

See these other two questions for reference:

Get drawable displayed size after scaling

Trying to get the display size of an image in an ImageView

I can now get the size of the scaled image, but I want to also know the origin (0,0) of the scaled image inside of the image view. In other words, what x,y location inside of image #1 corresponds to 0,0 of image #2?

http://image.noelshack.com/fichiers/2013/06/1360144144-sans-titre.png

If you want to know how (0,0) of drawable coresponds to (x,y) of the imageView, you can map points using imageView's transformation matrix like so

float[] drawableCoords = new float[] {0, 0};
imageView.getImageMatrix().mapPoints(drawableCoords);
float onViewX = drawableCoords[0];
float onViewY = drawableCoords[1];

you can also reverse the process (to map from imageView coordinates to drawable coordinates by creating inverted matrix and maping points on it

float[] viewCoords= new float[] {0, 0};
Matrix invertedMatrix = new Matrix()
imageView.getImageMatrix().invert(invertedMatrix);
invertedMatrix.mapPoints(viewCoords);
float onDrawableX= viewCoords[0];
float onDrawableY= viewCoords[1];

Note that transformed coordinates in certain cases can have a negative coordinate value if drawable is smaller/bigger than imageview.

I am assuming Image #1 is the parentview to Image #2.

int x = image2.getLeft();
int y = image2.getTop();

will return x,y of the pixel coordinates relative to image2's parentview, image1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM