旋转画布会影响 TouchEvents
我有一个在 Android 上使用内部地图引擎的地图应用程序。我正在开发一个旋转地图视图,该视图使用传感器服务根据手机的方向旋转地图。除了当手机指向北以外的方向时拖动地图外,一切正常。例如,如果手机面向西,则向上拖动地图仍会将地图移至南而不是东,如预期的那样。我假设翻译画布是一种可能的解决方案,但老实说我不确定执行此操作的正确方法。
这是我用来旋转画布的代码:
public void dispatchDraw(Canvas canvas)
{
canvas.save(Canvas.MATRIX_SAVE_FLAG);
// mHeading is the orientation from the Sensor
canvas.rotate(-mHeading, origin[X],origin[Y]);
mCanvas.delegate = canvas;
super.dispatchDraw(mCanvas);
canvas.restore();
}
无论手机方向如何,使拖动地图保持一致的最佳方法是什么?传感器管理器有一个“remapcoordinates()”方法,但不清楚这是否能解决我的问题。
I have a map application using an in-house map engine on Android. I'm working on a rotating Map view that rotates the map based on the phone's orientation using the Sensor Service. All works fine with the exception of dragging the map when the phone is pointing other than North. For example, if the phone is facing West, dragging the Map up still moves the Map to the South versus East as would be expected. I'm assuming translating the canvas is one possible solution but I'm honestly not sure the correct way to do this.
Here is the code I'm using to rotate the Canvas:
public void dispatchDraw(Canvas canvas)
{
canvas.save(Canvas.MATRIX_SAVE_FLAG);
// mHeading is the orientation from the Sensor
canvas.rotate(-mHeading, origin[X],origin[Y]);
mCanvas.delegate = canvas;
super.dispatchDraw(mCanvas);
canvas.restore();
}
What is the best approach to make dragging the map consistent regardless of the phones orientation? The sensormanager has a "remapcoordinates()" method but it's not clear that this will resolve my problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以轻松获得两个连续移动事件之间的增量 x 和增量 y。要纠正画布旋转的这些值,您可以使用一些简单的三角学:
You can trivially get the delta x and delta y between two consecutive move events. To correct these values for your canvas rotation you can use some simple trignometry: