如何在 Android 中正确地将像素坐标转换为画布坐标?
我正在使用 GestureListener
在 Android SurfaceView
中捕获长按的 MotionEvent
。然后,我需要将 MotionEvent
的坐标转换为画布坐标,从中我可以生成自定义地图坐标(不是 Google 地图)。
根据我所读到的内容,我采用给定的 MotionEvent e
、e.getX()
和 e.getY()
获取像素坐标。如何将这些坐标转换为 SurfaceView 的画布坐标?
这是我的 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
}
}
提前致谢!
编辑: 这与屏幕尺寸/分辨率/深度和画布的 Rect 对象有关吗?
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).
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?
Here is my GestureListener
for listening for long clicks:
/**
* 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!
Edit:
Does this have to do with screen size/resolution/depth and the canvas' Rect object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以尝试使用
MotionEvent.getRawX()/getRawY()
方法而不是getX()/getY()
。You might try using the
MotionEvent.getRawX()/getRawY()
methods instead of thegetX()/getY()
.好吧,您在 x,y 中获得了显示屏上的屏幕 px,并且您可以通过以下方式调用画布 px:
然后,您可以通过计算来映射屏幕触摸视图,并在视图和屏幕中使用给定的 px。
请参阅http://developer.android.com/reference/android/view/WindowManager.html有关屏幕管理器的更多信息。我认为它需要在活动内部初始化才能工作。
Well, you have the screen px from the display in x,y, and you can call the canvas px via:
Then, you can do the math to map the screen touch view , with the given px in both the view and the screen.
See http://developer.android.com/reference/android/view/WindowManager.html for more info on the screen manager. I think it needs to be initialized inside an activity to work.
我最近在做一些非常类似的事情时遇到了这个问题,经过一些尝试和错误以及大量的谷歌搜索,我最终适应了这个答案(https: //stackoverflow.com/a/9945896/1131180) :
(e 是一个 MotionEvent,因此使用此代码的最佳位置是在 onTouch 或onLongPress)
这里可以应用一些明显的优化,但我认为这种方式更清晰。
I recently came upon this problem while doing something very similiar and after some trial and error and lots of googling I ended up adapting this answer (https://stackoverflow.com/a/9945896/1131180) :
(e is a MotionEvent, so the best place to use this code would be in onTouch or onLongPress)
There are some obvious optimizations that can be applied here but I think it is more clear this way.
如果我理解正确的话,你在表面视图中有一个画布视图。如果是这样
尝试 VIEW.getLeft() | getTop() 返回左侧 |视图相对于其父视图的顶部位置。
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.如果您使用滚动,那么画布上的实际 y 是
if you are using scrolling, then the actual y on the canvas is
在这些情况下我所做的只是:
并且翻译完成了...
What I do in these situations is just:
And translation is done...