如何将位图上的坐标转换为屏幕上显示的图像视图上的真实坐标 [英] How to convert coordinates on Bitmap to real coordiates on Image View displayed on screen

查看:218
本文介绍了如何将位图上的坐标转换为屏幕上显示的图像视图上的真实坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示图像(例如2000x1000像素)的图像视图,并且在该图像上有一个坐标(X,Y)(不是图像视图).例如,我的图像视图的画布是600x800.如何将点(X,Y)转换为屏幕坐标,以便可以在Image View的OnDraw(...)方法上使用它们绘制路径.任何帮助表示赞赏!谢谢.

I have an Image View which displays an image (e.g 2000x1000 pixels) and I have a coordinate (X,Y) on that image (not the image view). The canvas of my Image View is 600x800 for example. How can I convert the point (X,Y) to screen coordinate so that I can draw a path with them on the OnDraw(...) method of Image View. Any help is appreciated! Thank you.

更新:如果我使用矩阵在坐标之间绘制路径,则可以使用,但是绘制的路径和对象变得很小.这是我使用的代码.

Update: If I use matrix to draw the path between coordinates, it works but the path and objects i draw become really small. Here is the code i used.

final Matrix matrix = canvas.getMatrix();
matrix.preConcat( _view.getImageMatrix() );
matrix.preScale( 1.0f /_inSampleSize, 1.0f / _inSampleSize);
canvas.setMatrix( matrix );
//I draw the path here

更新:我添加了一张图片以显示使用矩阵绘制路径时的效果.我希望4线和4个角球保持正常大小.红色是保存图片的图像视图的边界.

Update: I add a picture to show the effect when using matrix to draw the path. I would like to have the 4 line and the 4 corner balls to be in normal size. The red color is the boundary of the Image View which holds the picture.

推荐答案

我认为这可能取决于您显示图像的精确程度.您的ImageView(600x800)与位图(2000x1000)的宽高比不同.

I think that might depend on how exactly you are displaying your image. Your ImageView (600x800) is not the same aspect ratio as your bitmap (2000x1000).

在缩小位图时,是否保持位图的宽高比稳定?如果是这样,哪个部分(高度或宽度)占据了整个屏幕,哪个部分具有黑色(或其他)填充物?这将帮助您确定比例因子.

You are keeping the bitmap's aspect ratio stable as you scale it down? If so, which part (height or width) takes up the full screen and which has black (or whatever else) as padding? This will help you determine your scale factor.

scale_factor = goal_height/height1;  //if height is what you are scaling by
scale_factor = goal_width/width1;  //if width is what you are scaling by.

我会尝试:

x_goal = x1 * scale_factor;
y_goal = y1 * scale_factor;

也就是说,如果图像中有一个点(1333,900),并且图像占据了整个宽度,则将x和y乘以600/2000即可得到(399.9,270). (您可能需要四舍五入该小数).

That is, if you have a point (1333, 900) in your image, and your image takes up the full width, you would multiply both x and y by 600/2000 to get (399.9, 270). (you might want to round that decimal).

如果不保持位图的宽高比稳定(也就是说,要压缩它以适合),则将具有height_scale_factor和width_scale因子.因此,您需要(1333,900),然后将x乘以600/2000,将y乘以800/1000得到(399.9,720).

If you are NOT keeping the bitmaps aspect ratio stable (that is, you're squeezing it to fit), then you'd have a height_scale_factor and a width_scale factor. So you'd take (1333,900) and multiply x by 600/2000 and y by 800/1000 to get (399.9,720).

这篇关于如何将位图上的坐标转换为屏幕上显示的图像视图上的真实坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆