Android:屏幕方向报告太慢?

发布于 2024-10-20 12:23:00 字数 772 浏览 1 评论 0原文

我正在使用 Display.getRotation() 方法来确定我的应用程序中屏幕的当前方向,根据 Google 的这篇有用的博客文章:

http://android-developers.blogspot.com/2010/09/one-screen-turn-deserves- another.html

简而言之,我有一个绘图应用程序,允许用户在屏幕上绘制形状等,并且绘图画布需要保持相同的方向,但我仍然想要选项菜单,当用户开始使用不同方向的设备进行绘图时,等等,以翻转到正确的位置。

在画布的 onDraw() 中,我从 getRotation() 请求方向,然后将画布旋转回“0”位置,以便从最终用户的角度来看,画布永远不会移动。工作起来就像一个魅力除了,如果用户非常快速翻转设备......那么getRotation()报告旋转为0,因此画布不会向后旋转,使绘图看起来翻转或旋转。

有趣的是,标题栏和选项菜单确实会翻转……所以不确定他们从哪里获得方向。如果用户随后开始绘图,则对 onDraw() 的后续调用将获取正确的旋转值,并且绘图将返回到正确的位置,但正如您可以想象的那样,这有点令人不安/烦人。有什么策略来处理这个问题吗?

谢谢。

I am using the Display.getRotation() method to determine the current orientation of the screen in my App, per this helpful blog entry from Google:

http://android-developers.blogspot.com/2010/09/one-screen-turn-deserves-another.html

In a nutshell, I have a drawing app that allows the user to draw shapes, etc to the screen, and the drawing canvas needs to stay in the same orientation, but I still wanted to the Options menu, etc to flip to the correct location if and when the user starts drawing with the device in a different orientation.

In onDraw() for the canvas, I request the orientation from getRotation() and then rotate the canvas back to the '0' location so that from the end-user's perspective, the canvas never moves. Works like a charm EXCEPT if the user flips the device VERY quickly... then getRotation() reports a rotation of 0, and thus the canvas doesn't rotate back, making the drawing appear flipped or turned.

Interestingly, the title bar and options menus do flip... so not sure where they are getting the orientation from. If the user then starts to draw, the subsequent call to onDraw() gets the correct rotation vale and the drawing goes back to the correct location, but it is a bit unnerving/annoying as you can imagine. Any strategies on dealing with this?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

疑心病 2024-10-27 12:23:00

您可以尝试使用 getOrientation() 代替...我不确定您的问题源于何处,但谁知道,它可能会给您带来不同的结果。

Display myDisplay = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();         
int rotation = myDisplay.getOrientation();
switch (rotation) {
    case Surface.ROTATION_0: { doSomething(); break; }
    case Surface.ROTATION_90: { doSomething(); break; }                 
    case Surface.ROTATION_270: { doSomething(); break; }                    
    case Surface.ROTATION_180: { doSomething(); break; }
}

You could try using getOrientation() instead... I'm not sure where your issue stems from but who knows, it could give you a different result.

Display myDisplay = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();         
int rotation = myDisplay.getOrientation();
switch (rotation) {
    case Surface.ROTATION_0: { doSomething(); break; }
    case Surface.ROTATION_90: { doSomething(); break; }                 
    case Surface.ROTATION_270: { doSomething(); break; }                    
    case Surface.ROTATION_180: { doSomething(); break; }
}
仅此而已 2024-10-27 12:23:00

使用 onOrientationChanged()。它返回当前角度(以度为单位)。它似乎可以可靠地适应显示方向的变化。我将角度转换为 4 个标准方向,如下所示:

    myOrientationEventListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL)
    {

        @Override
        public void onOrientationChanged(int iAngle)
        {                         // 0  15 30 45 60 75, 90 105, 120, 135, 150, 165, 180, 195, 210, 225, 240, 255, 270, 285, 300, 315, 330, 345
            final int iLookup[] = {0,   0, 0,90,90, 90,90, 90,  90, 180, 180, 180, 180, 180, 180, 270, 270, 270, 270, 270, 270, 0, 0, 0}; // 15-degree increments 
            if (iAngle != ORIENTATION_UNKNOWN)
            {
                int iNewOrientation = iLookup[iAngle / 15];
                if (iOrientation != iNewOrientation)
                {
                iOrientation = iNewOrientation;
                <do something with new orientation>
                }  
            }
        }
    };

Use onOrientationChanged(). It returns the current angle in degrees. It seems to work reliably for display orientation changes. I convert the angle into the 4 standard orientations like this:

    myOrientationEventListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL)
    {

        @Override
        public void onOrientationChanged(int iAngle)
        {                         // 0  15 30 45 60 75, 90 105, 120, 135, 150, 165, 180, 195, 210, 225, 240, 255, 270, 285, 300, 315, 330, 345
            final int iLookup[] = {0,   0, 0,90,90, 90,90, 90,  90, 180, 180, 180, 180, 180, 180, 270, 270, 270, 270, 270, 270, 0, 0, 0}; // 15-degree increments 
            if (iAngle != ORIENTATION_UNKNOWN)
            {
                int iNewOrientation = iLookup[iAngle / 15];
                if (iOrientation != iNewOrientation)
                {
                iOrientation = iNewOrientation;
                <do something with new orientation>
                }  
            }
        }
    };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文