如何检测屏幕旋转屏幕

2023-09-12 22:04:57 作者:乱世劣徒

我有一个显示指南针的活动,我需要知道屏幕的正常旋转针的方向。我需要的90°和270°度旋转来区分妥善处理这个问题。

I have an activity that is showing a compass, and I need to know the orientation of the screen to properly rotate the needle. I need to distinguish between 90° and 270° degree rotation to properly handle this.

当我旋转屏幕(在我的Nexus S至少)活动的的onCreate 被调用,如:当我从纵向旋转为横向模式。当我从一个横向模式旋转到其他同提出的顶边,的onCreate()被调用两次(一次为纵向,一旦目标横向模式)。

When I rotate the screen (on my Nexus S at least) the activity's onCreate gets called, e.g. when I rotate from portrait to landscape mode. When I rotate from one landscape mode to the other with the top edge raised, onCreate() gets called twice (once for portrait orientation and once for the target landscape mode).

然而,当我在旋转,底边向上,屏幕旋转180°,的onCreate 是没有得到调用。是否有被炒鱿鱼在这种情况下,一个事件?

However, when I rotate with the bottom edge up, the screen rotates 180° and onCreate is not getting called. Is there an event that gets fired in this case?

推荐答案

您是正确的,在一个180度旋转,你不会得到重新启动或得到一个配置更改,因为配置实际上并没有改变。 (它仍然是横向或纵向,以及其他一切都是一样的。)

You are correct that at a 180 degree rotation you will not get restarted or get a configuration change, because the configuration has not actually changed. (It is still in landscape or portrait, and everything else is the same.)

不过,如果你是显示一个指南针,那一定意味着你是从传感器事件进行更新。因此,你可以()每次只需拨打Display.getRotation你得到一个传感器的更新来获取屏幕的当前旋转。事实上,你的需要的是除preting传感器事件的旋转(或实际上如何当它到达屏幕的他们的描绘将被修改),而不仅仅是取向

However if you are showing a compass, that must mean you are updating it from sensor events. Thus you can just call Display.getRotation() each time you get a sensor update to get the current rotation of the screen. In fact that you need is rotation of interpreting sensor events (or actually how your drawing of them will be modified when it gets to the screen), not just the orientation.

我发现了一个相关的博客文章也就是值得一读

I found a relevant blog post that is well worth reading.

请务必阅读 SDK文档。

另外,请查阅the讨论有关使用Display.getRotation()来正确地重新映射传感器坐标。

Also check out the discussion about using Display.getRotation() to correctly remap sensor coordinates.