我该如何正确翻译像素坐标机器人帆布坐标?坐标、帆布、我该、机器人

2023-09-07 04:47:42 作者:﹥得不到的才是最好的

我捕捉 MotionEvent 的长按在Android SurfaceView 使用 GestureListener 。然后,我需要将 MotionEvent 的坐标转换为画布上的坐标,从中我可以生成自定义地图坐标(不是谷歌地图)。

I am capturing a MotionEvent for a long click in an Android SurfaceView using a GestureListener. I then need to translate the coordinates of the MotionEvent to canvas coordinates, from which I can generate custom map coordinates (not Google Maps).

从我已阅读,我把给定 MotionEventè e.getX() e.getY()得到像素坐标。我怎样才能将这些坐标到 SurfaceView 的画布坐标?

From what I have read, I take that given MotionEvent e, e.getX() and e.getY() get pixel coordinates. How can I convert these coordinates to the SurfaceView's canvas coordinates?

下面是我的 GestureListener 听长点击:

/**
* Overrides touch gestures not handled by the default touch listener
*/
private class GestureListener extends GestureDetector.SimpleOnGestureListener {

  @Override
  public void onLongPress(MotionEvent e) {
     Point p = new Point();
     p.x =  (int) e.getX();
     p.y = (int) e.getY();
     //TODO translate p to canvas coordinates
  }
}

在此先感谢!

Thanks in advance!

编辑: 这是否都与屏幕尺寸/分辨率/深度和画布矩形对象?

Does this have to do with screen size/resolution/depth and the canvas' Rect object?

推荐答案

如果我理解正确的话里面有一个surfaceview画布视图。如果是这样 尝试 VIEW.getLeft()|共达()返回左|相对于它的视图的顶部位置的父。

If i understand correctly you have a canvas View inside surfaceview. If so try VIEW.getLeft() | getTop() that returns the left | top position of the view relative to it's parent.

float x= e.getX() - canvasView.getLeft();
float y= e.getY() - canvasView.getTop();