java 使用 Display.getRotation() 进行屏幕旋转

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4757632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 07:53:20  来源:igfitidea点击:

Screen rotation using Display.getRotation()

javaandroidrotation

提问by SamRowley

I am trying to make it so my app never goes into portrait mode but is able to switch between the 2 landscape screen views. I know this can be done easily in Gingerbread (2.3) but I am having trouble doing it manually for the other versions of android, my code is as follows:

我正在努力使我的应用程序永远不会进入纵向模式,但能够在 2 个横向屏幕视图之间切换。我知道这可以在 Gingerbread (2.3) 中轻松完成,但我无法为其他版本的 android 手动执行此操作,我的代码如下:

Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        int orientation = display.getRotation();
        if (orientation == 1) {
            /* The device is rotated to the left. */
            Log.v("Left", "Rotated Left");
        } else if (orientation == 3) {
            /* The device is rotated to the right. */
        Log.v("Right", "Rotated Right");
        } else { 

        }

My problem is how do I flip the x and y-axis of the screen view depending on the rotation detected? How do I get hold of them in order to reverse them?

我的问题是如何根据检测到的旋转翻转屏幕视图的 x 和 y 轴?我如何抓住它们以逆转它们?

采纳答案by Vincent Mimoun-Prat

As far as I know there is no support for inverted layout before 2.3. So unless you are drawing the screen with a custom SurfaceView I'd say you cannot do it using standard widgets. With a surface view, you would simply need to transform the whole canvas before rendering to it.

据我所知,2.3 之前不支持倒排布局。因此,除非您使用自定义 SurfaceView 绘制屏幕,​​否则我会说您不能使用标准小部件来完成。使用表面视图,您只需要在渲染之前转换整个画布。

Additionally, you should use constants in order to know the current orientation (no magic numbers), see the Configuration class API documentation

此外,您应该使用常量来了解当前的方向(无幻数),请参阅配置类 API 文档

if (orientation==Configuration.ORIENTATION_LANDSCAPE) 

回答by Max Lebedev

getRotation for class 'Display' is explained at https://developer.android.com/reference/android/view/Display.html#getRotation()

https://developer.android.com/reference/android/view/Display.html#getRotation() 中解释了“Display”类的 getRotation

getRotation returns:

getRotation 返回:

Surface.ROTATION_0 (no rotation),
Surface.ROTATION_90,
Surface.ROTATION_180,
or 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. 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.

例如,如果设备有一个自然高的屏幕,并且用户已将其翻转为横向,则此处返回的值可能是 Surface.ROTATION_90 或 Surface.ROTATION_270,具体取决于它转动的方向。角度是绘制图形在屏幕上的旋转方向,与设备的物理旋转方向相反。例如,如果设备逆时针旋转 90 度,为了补偿渲染将顺时针旋转 90 度,因此这里的返回值将为 Surface.ROTATION_90。