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

发布于 2024-10-13 08:08:17 字数 635 浏览 2 评论 0原文

我正在努力让我的应用程序永远不会进入纵向模式,但能够在两个横向屏幕视图之间切换。我知道这可以在 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 { 

        }

我的问题是如何翻转屏幕视图的 x 和 y 轴取决于检测到的旋转?我如何抓住它们以扭转它们?

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:

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情归归情 2024-10-20 08:08:17

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

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

if (orientation==Configuration.ORIENTATION_LANDSCAPE) 

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.

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

if (orientation==Configuration.ORIENTATION_LANDSCAPE) 
幸福丶如此 2024-10-20 08:08:17

'Display' 类的 getRotation 在 https://developer 中进行了解释.android.com/reference/android/view/Display.html#getRotation()

getRotation 返回:

Surface.ROTATION_0 (no rotation),
Surface.ROTATION_90,
Surface.ROTATION_180,
or Surface.ROTATION_270.

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

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

getRotation returns:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文