【问题标题】:How to track reverse orientations(reverseLandscape and reversePortrait) through any listeners?如何通过任何听众跟踪反向方向(reverseLandscape 和 reversePortrait)?
【发布时间】:2013-03-05 02:51:30
【问题描述】:

浏览了几篇关于配置和方向更改的帖子和线程,但我仍然没有被清除。

因为onConfigurationChanged 仅在orientationlandscape 更改为portraitportrait 更改为landscape(顾名思义)时才被调用,但是有什么方法可以收听reverseLandscape 或@ 987654329@ 方向通过任何 api 级别的任何种类的列表更改。

或者是否可以像onConfigurationChanged一样收听rotation的变化??

任何帮助将不胜感激!

【问题讨论】:

标签: android android-sensors android-orientation android-resolution


【解决方案1】:

您可以使用加速度计和磁场传感器计算设备旋转然后调用

getWindowManager().getDefaultDisplay().getRotation();  

当旋转处于某个值时。计算旋转的方法在我的回答How to measure the tilt of the phone in XY plane using accelerometer in Android

【讨论】:

  • 感谢您的回复,但我不想进行那么多计算来了解方向,因为我认为他们将是任何回调侦听器,只要设备旋转就会给出旋转或方向,不好幸运的是我还没有找到答案! +1
  • 实际上,您不需要使用传感器来获取旋转。只需注册 OrientationEventListener。它会给你旋转角度。
  • 我的问题中已经提到了我想要的,reverseLandscape 和 reversePortrait !!
  • 是的,我明白了。 OrientationEventListener 在您旋转设备时为您提供角度。它不会告诉您设备是纵向还是横向或其他。我的意思是您可以使用 OrientationEventListener 返回的值在该值通过某个值时调用 getRotation,例如 +- 160。
  • 听起来不错让我试试,如果可行我会接受你的回答。
【解决方案2】:

您可以通过调用来检索 4 个方向中的哪一个当前处于活动状态

getWindowManager().getDefaultDisplay().getRotation();

知道何时调用它可能很棘手,因为如果您直接从portrait 转到reversePortrait,系统不会通知您。实现这一目标的唯一方法是使用设备的传感器。这个article from the Android Developers Blog 应该可以帮到你。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-10
    • 1970-01-01
    • 1970-01-01
    • 2012-04-23
    • 1970-01-01
    相关资源
    最近更新 更多