旋转画布会影响 TouchEvents

发布于 2024-10-14 16:56:54 字数 581 浏览 2 评论 0原文

我有一个在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不离久伴 2024-10-21 16:56:54

您可以轻松获得两个连续移动事件之间的增量 x 和增量 y。要纠正画布旋转的这些值,您可以使用一些简单的三角学:

void correctPointForRotate(PointF delta, float rotation) {

    // Get the angle of movement (0=up, 90=right, 180=down, 270=left)
    double a = Math.atan2(-delta.x,delta.y); 
    a = Math.toDegrees(a);  // a now ranges -180 to +180
    a += 180;

    // Adjust angle by amount the map is rotated around the center point
    a += rotation;
    a = Math.toRadians(a);

    // Calculate new corrected panning deltas
    double hyp = Math.sqrt(x*x + y*y);
    delta.x = (float)(hyp * Math.sin(a));
    delta.y = -(float)(hyp * Math.cos(a));
}

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:

void correctPointForRotate(PointF delta, float rotation) {

    // Get the angle of movement (0=up, 90=right, 180=down, 270=left)
    double a = Math.atan2(-delta.x,delta.y); 
    a = Math.toDegrees(a);  // a now ranges -180 to +180
    a += 180;

    // Adjust angle by amount the map is rotated around the center point
    a += rotation;
    a = Math.toRadians(a);

    // Calculate new corrected panning deltas
    double hyp = Math.sqrt(x*x + y*y);
    delta.x = (float)(hyp * Math.sin(a));
    delta.y = -(float)(hyp * Math.cos(a));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文