【问题标题】:How to compare MotionEvent coordinates to original image coordinates?如何将 MotionEvent 坐标与原始图像坐标进行比较?
【发布时间】:2014-01-14 21:32:12
【问题描述】:

我有一张图片 (ImageView)。我在图像上有某些区域。当用户点击屏幕时,我想检测选择了哪个区域。

我已经确定了原始图像上的区域边界,但是MotionEvent 的 x 和 y 是关闭的。

我尝试了 dip-to-pixel 转换 (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dipValue, metrics)),但它们仍然关闭。它当然与屏幕尺寸/密度/等有关,但是如何准确获取原始图像的触摸事件的像素值? (或者,反之亦然 - 如何将原始图像坐标转换为与运动事件的 x 和 y 相当的坐标)

【问题讨论】:

标签: android


【解决方案1】:

我设法使用ImageView (pic) 的右下坐标与实际图像尺寸 (568x1207) 进行比较来计算它。

float xCoef = 568f / pic.getRight();
float yCoef = 1207f / pic.getBottom();
float x = event.getX() * xCoef;
float y = event.getY() * yCoef;

【讨论】:

  • 这不是应该的,再次使用 ImageView.getImageMatrix()
  • 怎么样?我尝试映射点(matrix.mapPoints(new float[] {eventX, eventY},但结果不正确。
  • 它们是正确的,点 (0, 0) 映射到绘制左上像素的物理点,如果要反向映射使用 invert()ed Matrix
  • 我不要反向映射,0,0总是0,0。但是当在右下角 clkcing 时,我得到 x,y=540,830,当我运行 matrix.mapPoints 时,我得到 x,y=317,390。我想得到568,1207,也就是实际图片的右下角
  • 好的,所以你需要反向映射:from。查看坐标到位图坐标,然后使用 Matrix.invert(invertedMatrix)
猜你喜欢
  • 2021-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-28
  • 1970-01-01
  • 2023-04-06
  • 1970-01-01
  • 2016-10-29
相关资源
最近更新 更多