Android 中的屏幕和视图坐标
如果用户触摸屏幕,我可以使用 MotionEvent getX() 和 getY() 方法获取此触摸的坐标。
但如果我理解正确的话,将使用其他坐标来绘制画布。
我同事的意思是,这些是视图和屏幕坐标。但他不知道,是否有一些转化的方法。
也许你可以帮助我。
预先感谢您,
Mur
UPD
我有点困惑。我正在使用 osmdroid,他们正在将叠加层绘制为画布。
因此,如果我将它们用于绘图的画布坐标与 MotionEvent 坐标进行比较,我什么都不懂。
这些是我打印出来的矩形坐标(左、右、下、上)16899.0 / 16948.0 -86483.0 / -86515.0。
MotionEvent 的坐标为 252.0 / 223.0
If user touchs the screen I can get coordinates of this touch by using methods of MotionEvent getX() and getY().
But if I understood it right for drawing of canvas other coordinates will be used.
My colleague meant, these are view and screen coordinates. But he didn't know, if there are some transformations methods.
Maybe you can help me.
Thank you in advance,
Mur
UPD
I've got a bit confusion. I'm using osmdroid, they are drawing overlays as canvas.
So if I'm comparing canvas coordinates they are using for drawing with MotionEvent coordinates I don't understand anything.
These are coordinates of rectangle (left, right, bottom, top) 16899.0 / 16948.0 -86483.0 / -86515.0 I've printed out.
Coordinates of MotionEvent are 252.0 / 223.0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
他错了。 Canvas 没有使用任何不同的东西。您只需考虑画布在屏幕上相对于用户触摸位置的位置。
He is wrong. Canvas doesn't use anything different. You just have to consider where the canvas is located on the screen in relation to where the user touched.
如果您在画布上的位置 0,0 处绘制,您将在左上角绘制。所以你不需要做任何特别的事情。
也许您的朋友对屏幕/窗口位置有点困惑:http://developer.android.com/reference/android/view/View.html#getLocationInWindow(int[])
If you draw at location 0,0 on your canvas you will draw in the top left corner. So you don't have to do anything special.
Perhaps your friend has got a bit confused with screen/window locations: http://developer.android.com/reference/android/view/View.html#getLocationInWindow(int[])
看起来 osmdroid 正在转换坐标。这对于地图来说是有意义的,所以如果你在 0,0 处绘制一些东西,你就会在你的位置绘制一些东西。
我认为返回的坐标是地图坐标,从位置 (0,0) 开始,因为图像 16899.0+86483.0=103382.0 像素高和 16948.0+86515.0=103463.0 像素宽是 10200 gigapixel 的图像,消耗近 30 Tb 的内存,并且我我怀疑智能手机是否可以有如此多的可用内存......
因此,对于您的应用程序,我建议采用如下方法:
Looks like osmdroid is trasforming coordinates. This makes sense for a map, so if you draw something at 0,0 you will draw something at your location.
I suppose returning coordinates are map coordinates, starting from location (0,0) because an image 16899.0+86483.0=103382.0 pixel tall and 16948.0+86515.0=103463.0 pixel wide is an image of 10200 gigapixel, consuming almost 30 Tb of memory, and I am in doubt that a smarphone can have such a lot of memory free...
So for your app I suggest an approach like this: