Android 屏幕方向传感器
我想通过设置“它工作正常”来强制屏幕方向在单击按钮时横向
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
。现在我希望应用程序跟随传感器,以便在倾斜回纵向时方向恢复到纵向。我知道可以通过设置 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
但不知道在哪里设置它。如果方向强制为横向,则无论您向任何方向倾斜,方向都将保持横向。谁能指定如何重置方向标志?
I want to force the screen orientation to landscape on button click by setting
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
It works fine. Now I want the application to follow the sensor so that orientation is brought back to portrait when tilted back to portrait. I know this is possible by setting setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
but don't know where to set it. If the orientation is forced to landscape, orientation will remain in landscape no matter you tilt in any direction. Can anyone specify how to reset the orientation flag?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当前的 YouTube 应用可以满足您的要求。
我在视频播放应用程序中处理过同样的问题。
如果用户在纵向时强制方向为横向,请初始化 OrientationEventListener,它会通知您来自
SensorManager
的设备方向(范围从 0 到 360)观察设备是否倾斜到大约
(方向 >= 60 && 方向 <= 120) || 的横向方向范围(方向 >= 240 && 方向 <= 300)
并将此状态保存到枚举或标志,然后如果设备返回到纵向方向范围 (方向 <= 40 | |orientation >= 320
),检查枚举/标志并调用setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
,重置标志/枚举并禁用传感器,直到用户再次强制定向。< br>这是演示代码:
这与 YouTube 功能类似,希望有所帮助。
Current YouTube app does what you are asking for.
I've dealt with same kind of problem in my application for video playback.
If user forces orientation to landscape when he/she was in portrait, initialise OrientationEventListener which notifies you on device orientation from
SensorManager
which ranges from 0 to 360.Watch if device tilts to landscape orientation range which would be around
(orientation >= 60 && orientation <= 120) || (orientation >= 240 && orientation <= 300)
and save this state to a enum or a flag and then if device goes back to Portrait orientation range (orientation <= 40 || orientation >= 320
), check the enum/flag and callsetRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
, reset the flag/enum and disable the sensor until user force orientation again.Here is the demo code:
This worked similar to YouTube functionality, hope this helps.
我认为在这里你应该使用
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
I think here you should use
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
这取决于您希望传感器何时再次检测旋转。
就我个人而言,在我正在开发的应用程序中,我有一个需要处于纵向模式的特定活动,因此我在
onResume()
中使用setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
> 此活动和setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
在onPause()
中,它工作得很好,当我进入活动时,如果不是并且没有,它会设置为纵向不允许更改,退出传感器后再次工作...您试图在哪里启用和禁用传感器?
It depends on when you want the sensor to detect rotation again.
Personally in an app I'm developping I have one specific activity where I need to be in portrait mode, so I use
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
in theonResume()
of this activity andsetRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
inonPause()
and it works just fine, when I enter the activity it sets to portrait if it's not and doesn't allow to change and on exitting the sensor works again...Where are you trying to enable and disable the sensor?