【问题标题】:android compass rotateanimationandroid 指南针旋转动画
【发布时间】:2012-07-05 02:56:29
【问题描述】:

我正在制作一个指向用户定义位置的指南针。我正在使用旋转动画来旋转针。当针指向手机的方向时,我知道手机指向我想要的方向。但是,无论手机的方位如何,我都希望指针指向正确的方向。

问题在于,rotateanimation 似乎没有根据真实世界坐标旋转指针,而是相对于手机的屏幕。所以针的 58 度旋转与现实世界中的 58 度旋转不匹配。这是真的还是我的代码有误?

指南针旨在通过将手机背面平放在表面上来使用。我也试过输出方位角,它看起来像这样:

Azimuth           Actual Phone angle
0                        0
45                       90
90                       180

当它接近一整圈时,它会在 120 到 340 之间反弹。

代码如下:

direction = 360 - azimuth + rotate;

RotateAnimation animate = new RotateAnimation(rotateLast, direction, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
            animate.setFillAfter(true);
            animate.setInterpolator(new LinearInterpolator());
            animate.setDuration(10);
            needle.startAnimation(animate);

            rotateLast = direction;

azimuth 是手机与传感器的方位角,rotate 是用户指定的方向(以度为单位),direction 是指针所需的旋转方向。

rotateLast 是针的最后一个位置,我使用它是因为没有它,针会恢复到零度并闪烁。

谢谢, 附言这让我发疯了

【问题讨论】:

    标签: android compass-geolocation rotateanimation


    【解决方案1】:

    我想通了,我的数学全错了,我误解了方位角如何影响旋转。

    当我将图像旋转到方位角时,我意识到它导致指针指向北方。我需要做的只是添加用户方向。我使用的数学运算导致指针以不可预知的方式旋转。

    答案很简单:

    RotateAnimation animate = new RotateAnimation(-azimuth+rotation, -azimuth+rotation, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    

    -azimuth 是因为 rotateanimation 是逆时针旋转的。

    【讨论】:

    • 嗯,为什么你使用 fromDegree,toDegree 具有相同的值?它直接到该位置吗?
    猜你喜欢
    • 2013-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-22
    • 1970-01-01
    相关资源
    最近更新 更多