有没有办法执行“setRotation()”用方法或代码?
所以我希望我的应用程序始终锁定设备的自然方向。所以基本上,我想强制旋转到 0 度。这里有一个 getRotation()
方法:
http:// developer.android.com/reference/android/view/Display.html
但是,没有 setRotation(introtation) 可以接受此处找到的 Surface.ROTATION_X
参数之一: http://developer.android.com/reference/android/view/Surface.html
那么,有没有什么方法可以用一行代码来实现这一点并且始终有效呢?或者这必须通过一些代码来完成?
我唯一能想到的手动解决方案是以下内容,但我对此有一些担忧。
我可以使用 display.getRotation()
。然后我将检查它是 0 度还是 180 度,之后我将获取显示高度和宽度,并查看哪一个比另一个长。如果我看起来处于横向模式,那么我就会知道设备的自然方向是横向,因此我可以将其设置为横向。但是,如果旋转是 270 或 90,那么我就会知道自然方向是纵向,并且我可以将其设置为纵向。
然而,我关心的是在哪里执行此操作才能始终有效。假设用户在执行这段代码时旋转屏幕,如果设备在我使用 getRotation() 方法之间改变方向,并使用一些 if 检查宽度和高度声明等等,我认为这可能会让一切都变得混乱。如果我将其放入 onCreate()
或 onResume()
中,会发生什么?事情有可能变得不正常吗?
onCreate()
或 onResume()
是否可能因用户转动设备而导致屏幕重新定向而中断?另一个问题是,如果将设备翻转 180 度,活动永远不会被破坏,因此我认为无论是 90 度旋转还是 180 度旋转,这种情况的行为都可能会发生变化。
So I want my app to always lock onto the natural orientation of the device. So basically, I want to force the rotation to 0 degrees. There's a getRotation()
method here:
http://developer.android.com/reference/android/view/Display.html
However, there is no setRotation(int rotation) that would accept one of the Surface.ROTATION_X
parameters found here:
http://developer.android.com/reference/android/view/Surface.html
So, is there some way to do this with a single line of code that will always work? Or would this have to be done with some code?
The only thing I can think of as a manual solution is the following, but I have some concerns about it.
I can use the display.getRotation()
. Then I'll check if it's 0 or 180 degrees, after that I'll get the display height and width, and see which one is longer than the other. If it appears I'm in landscape mode, then I'll know that the device's natural orientation is landscape so I could set it to landscape. However if the rotation was 270 or 90, then I would know the natural orientation is portrait, and I could set it to portrait.
However, my concern is where to do this such that it always works. Let's say the user is rotating the screen as this piece of code is executing, if the device changes orientation in between the time I use the getRotation()
method, and check the width against the height with some if statements and what not, I think it could throw everything off. What happens if I put this in onCreate()
or onResume()
? Is it possible that things can get out of wack?
Is it possible for a the onCreate()
or onResume()
to be interrupted by a screen re-orientation due to the user turn the device around? The other issue is that if you flip the device by 180 degrees, the activity is never destroyed, so I think the behavior of this situation could very whether it's a 90 degree turn, or a 180 degree turn.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在清单中“设置”屏幕方向。请参阅此处: http://developer.android.com/guide/ topic/manifest/activity-element.html#screen
您可能正在寻找的值是; “user” 用户当前的首选方向。不过,为了确定这一点,我会在几个测试用例上进行测试。哈!
You can "set" the screen orientation in the manifest. See here: http://developer.android.com/guide/topics/manifest/activity-element.html#screen
The value you're probably looking for is; "user" The user's current preferred orientation. However, I would test this on several test cases to be sure. HTH!