简体   繁体   English

铸造设备朝向LANDSCAPE,PORTRAIT,LANDSCAPE_REVERTED和PORTRAIT_REVERTED

[英]Casting device orientation to LANDSCAPE, PORTRAIT, LANDSCAPE_REVERTED and PORTRAIT_REVERTED

I have a task where I have to cast device (not activity) orientation to 4 values (LANDSCAPE, PORTRAIT, LANDSCAPE_REVERTED and PORTRAIT_REVERTED). 我有一个任务,必须将设备 (非活动)方向转换为4个值(LANDSCAPE,PORTRAIT,LANDSCAPE_REVERTED和PORTRAIT_REVERTED)。 I came up with a solution but it uses fixed values, and does not represent the android functionality of how and when it changes the activities orientations. 我想出了一个解决方案,但它使用固定值,并不代表如何以及何时更改活动方向的android功能。

I was unable to find an api that could easily notify a listener of when the device has changed it's orientation to the specified 4 values. 我找不到能轻松通知侦听器的api,该api的更改是将设备的方向更改为指定的4个值。

It would be grate if someone could share the function of how the android computes the orientation and informs WindowManager to change the orientation of the activities. 如果有人可以共享android如何计算方向并通知WindowManager更改活动方向的功能,将不胜感激。

[SOLVED] To does who might end up in the same situation as me. [已解决]可能会遇到和我一样的情况。 frameworks/base/core/java/android/view/WindowOrientationListener.java frameworks / base / core / java / android / view / WindowOrientationListener.java

this listener contains all the magic that informs the activity to change it's orientation. 该侦听器包含所有通知活动更改其方向的魔术。

In onConfigChanges()you 'll always be notified whenever ur screen orientation changes and you can always get orientation of ur screen like this : 在onConfigChanges()中,只要您的屏幕方向发生变化,就会始终收到通知,您总是可以这样获得屏幕的方向:

Display getOrient = getWindowManager().getDefaultDisplay();
switch(orientation){
case Configuration.ORIENTATION_LANDSCAPE:
Log.e();
break;
case Configuration.ORIENTATION_PORTRAIT:
Log.e();
break;
}

Use the screenOrientation in your project manifest and set the orientation. 在项目清单中使用screenOrientation并设置方向。

<activity
            android:name=".SplashScreen"
            android:screenOrientation="portrait"
</activity>

If u want the activity to show and align in landscape mode then add a folder in drawable namely layout-landscape and if u want the app to run only in portrait mode then declare in the manifest as the following 如果您希望活动在横向模式下显示和对齐,请在可绘制的文件夹中添加一个文件夹,即layout-landscape;如果您希望该应用程序仅在纵向模式下运行,请在清单中声明以下内容

android:screenOrientation="portrait"

or u want in the landscape mode: 或者你想在横向模式下:

 android:screenOrientation="landscape"

I am pretty sure the OS calculate the rotation of the device and change orientation when the rotation angle change by 60 degrees. 我很确定操作系统会在旋转角度改变60度时计算设备的旋转并更改方向。 Thus if the device is in Portrait orientation with rotation angle equal to 0, then when the device is rotated say counterclockwise, the rotation angle will increase and when the rotation angle is about 60 degree, the OS will change to Landscape. 因此,如果设备处于纵向方向且旋转角度等于0,则当设备逆时针旋转时,旋转角度将增加,而当旋转角度约为60度时,操作系统将变为横向。 Now suppose the device is in Landscape orientation when you rotate the device clockwise, the angle will decrease and at about 30 degree the OS will change to Portrait orientation. 现在,假设您顺时针旋转设备时设备处于横向,该角度将减小,并且在大约30度时,操作系统将变为纵向。

Orientation change state 方向变化状态
Left side is the current orientation. 左侧是当前方向。
Right side is the new orientation when the device is rotated. 旋转设备时,右侧是新的方向。
Portrait ---> Landscape rotation counterclockwise rotation angle > 60 纵向--->横向旋转逆时针旋转角度> 60
Portrait ---> Landscape_Reverse rotation clockwise rotation angle < -60 纵向--->横向旋转逆时针旋转角度<-60
Landscape ---> Portrait rotation clockwise rotation angle < 30 横向--->纵向旋转顺时针旋转角度<30
Landscape ---> Portrait_Reverse rotation counterclockwise rotation angle > 150 横向--->纵向反转逆时针旋转角度> 150
Landscape_Reverse ---> Portrait rotation counterclockwise rotation angle > -30 Landscape_Reverse --->纵向旋转逆时针旋转角度> -30
Landscape_Reverse ---> Portrait_Reverse rotation clockwise rotation angle < -150 横向反转--->纵向反转顺时针旋转角度<-150

The rotation angle can not be calculated when the device is laying flat. 设备平放时无法计算旋转角度。 This is why if you start an app when the device is laying flat and rotate the device no orientation change occurs. 这就是为什么如果在设备平放时启动应用程序并旋转设备,则不会发生方向变化的原因。
You can see how the rotation angle is calculated at my answer at How to measure the tilt of the phone in XY plane using accelerometer in Android 您可以在如何使用Android的加速计在XY平面上测量手机的倾斜度的答案中看到如何计算旋转角度

Use getRotation() 使用getRotation()

Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int rotation = display.getRotation();

from the docs 从文档

Returns the rotation of the screen from its "natural" orientation. 从“自然”方向返回屏幕的旋转角度。 The returned value may be Surface.ROTATION_0 (no rotation), Surface.ROTATION_90, Surface.ROTATION_180, or Surface.ROTATION_270. 返回的值可以是Surface.ROTATION_0(不旋转),Surface.ROTATION_90,Surface.ROTATION_180或Surface.ROTATION_270。 For example, if a device has a naturally tall screen, and the user has turned it on its side to go into a landscape orientation, the value returned here may be either Surface.ROTATION_90 or Surface.ROTATION_270 depending on the direction it was turned. 例如,如果设备的屏幕自然高大,并且用户已将其侧面旋转以进入横向,则根据设备的旋转方向,此处返回的值可以是Surface.ROTATION_90或Surface.ROTATION_270。 The angle is the rotation of the drawn graphics on the screen, which is the opposite direction of the physical rotation of the device. 角度是屏幕上绘制的图形的旋转,与设备物理旋转的方向相反。 For example, if the device is rotated 90 degrees counter-clockwise, to compensate rendering will be rotated by 90 degrees clockwise and thus the returned value here will be Surface.ROTATION_90. 例如,如果将设备逆时针旋转90度,则补偿渲染将顺时针旋转90度,因此此处返回的值为Surface.ROTATION_90。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM