在我的应用程序中,我需要让用户查看一些照片上的眼睛。
在OnTouchListener.onTouch(...)中,我获得了ImageView的坐标。
如何将这些坐标转换为触摸的位于位图上的点?
至少在API 10及以上版本上,以下方法适用:
final float[] getPointerCoords(ImageView view, MotionEvent e)
{
final int index = e.getActionIndex();
final float[] coords = new float[] { e.getX(index), e.getY(index) };
Matrix matrix = new Matrix();
view.getImageMatrix().invert(matrix);
matrix.postTranslate(view.getScrollX(), view.getScrollY());
matrix.mapPoints(coords);
return coords;
}
好的,我没有尝试过这个,但经过一番思考,我有一个建议:
ImageView imageView = (ImageView)findViewById(R.id.imageview);
Drawable drawable = imageView.getDrawable();
Rect imageBounds = drawable.getBounds();
//original height and width of the bitmap
int intrinsicHeight = drawable.getIntrinsicHeight();
int intrinsicWidth = drawable.getIntrinsicWidth();
//height and width of the visible (scaled) image
int scaledHeight = imageBounds.height();
int scaledWidth = imageBounds.width();
//Find the ratio of the original image to the scaled image
//Should normally be equal unless a disproportionate scaling
//(e.g. fitXY) is used.
float heightRatio = intrinsicHeight / scaledHeight;
float widthRatio = intrinsicWidth / scaledWidth;
//do whatever magic to get your touch point
//MotionEvent event;
//get the distance from the left and top of the image bounds
int scaledImageOffsetX = event.getX() - imageBounds.left;
int scaledImageOffsetY = event.getY() - imageBounds.top;
//scale these distances according to the ratio of your scaling
//For example, if the original image is 1.5x the size of the scaled
//image, and your offset is (10, 20), your original image offset
//values should be (15, 30).
int originalImageOffsetX = scaledImageOffsetX * widthRatio;
int originalImageOffsetY = scaledImageOffsetY * heightRatio;
尝试一下这个想法,看看它是否适用于你。
xImageOffsetX
和 yImageOffset
没有定义,使用它们时您指的是 scaledImageOffsetX
和 scaledImageOffsetY
吗?非常感谢您的帮助,对旧问题打扰了,敬请谅解!;) - MatteoImageView
上的一个点(x,y)与Bitmap
的一个像素对应起来。但是,这种解决方案如何考虑到可能存在比ImageView
的点更多的像素的事实?难道对应关系不应该是一对多吗?因为像素比点多。如果我的问题不清楚,请告诉我(如果您有时间的话;D),我可以尝试更好地解释自己!无论如何,非常感谢! ;D - Matteo除了考虑填充偏移量(边距是布局的一部分,它是视图外的空间,不必考虑),如果图像被缩放,您可以获取图像矩阵 (ImageView.getImageMatrix()
) 来缩放坐标。
编辑: 您可以获取值数组并使用相应的索引常量来获取 x/y 缩放因子和平移量:
float[] values;
matrix.getValues(values);
float xScale = values[Matrix.MSCALE_X];
matrix.mapPoints()
将位图点转换为ImageView的点。
目前,我已将ImageView的比例类型设置为“FitXY”,并使用类似于此的代码:xCoord = (xCoord / imagView.getWidth())*bitmap.getWidth();
- Alex当我遇到这个问题并尝试自己解决时,这是我的解决方案。 它似乎适用于拉伸和居中的图像。
class MyEditableImageView(context: Context, attrs: AttributeSet) :
androidx.appcompat.widget.AppCompatImageView(context, attrs) {
override fun onTouchEvent(event: MotionEvent): Boolean {
val image = drawable.toBitmap().copy(Bitmap.Config.ARGB_8888, true)
val xp = (event.x - imageMatrix.values()[Matrix.MTRANS_X]) / imageMatrix.values()[Matrix.MSCALE_X]
val yp = (event.y - imageMatrix.values()[Matrix.MTRANS_Y]) / imageMatrix.values()[Matrix.MSCALE_Y]
if (xp >= 0 && xp < image.width && yp >= 0 && yp < image.height) {
doSomethingOnImage(image, xp, yp)
setImageBitmap(image)
}
return super.onTouchEvent(event)
}
...
}
//original height and width of the bitmap
int intrinsicHeight = drawable.getIntrinsicHeight();
int intrinsicWidth = drawable.getIntrinsicWidth();
可能会返回意想不到的答案。这些值取决于你从哪个 dpi 的 drawable 文件夹加载图像。例如,如果你从 /drawable vs /drawable-hdpi vs /drawable-ldpi 中加载图像,则可能会得到不同的值。
获取楼层宽度和高度
float floorWidth = floorImage.getWidth();
float floorHeight = floorImage.getHeight();
计算比例值
float proportionateWidth = bitmapWidth / floorWidth;
float proportionateHeight = bitmapHeight / floorHeight;
你的 X & Y
float x = 315;
float y = 119;
按比例值进行多重计算
x = x * proportionateWidth;
y = y * proportionateHeight;