简体   繁体   English

如何在不旋转/重新创建活动的情况下检测屏幕旋转?

[英]How to detect screen rotation without rotating/recreating the activity?

I want to be able to detect whether the phone is in landscape or portrait mode, but I don't want my activity and all my buttons to rotate along.我希望能够检测手机是处于横向模式还是纵向模式,但我不希望我的活动和所有按钮一起旋转。

This code detects the rotation, but it also rotates the screen:此代码检测旋转,但它也旋转屏幕:

Display display = getWindowManager().getDefaultDisplay();
int rotation = display.getRotation();

rotVal.setText("Rotation: " + rotation);

I tried to lock the activity in portrait mode with this in the MainActivity, but then rotation value doesn't change.我试图在 MainActivity 中使用此功能将活动锁定为纵向模式,但rotation值不会改变。 (Makes sense I guess) (我想是有道理的)

this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

I also tried android:screenOrientation="portrait" in the manifest file but the rotVal stays zero.我还在清单文件中尝试了android:screenOrientation="portrait"rotVal保持为零。

This answer have a rotating animation, and I don't want that. 这个答案有一个旋转动画,我不想要那个。 I want the screen to stay as it is even in landscape mode, no rotating, no animation, etc.我希望屏幕即使在横向模式下也能保持原样,没有旋转,没有动画等。

This answer doesn't work. 这个答案不起作用。 When I rotate the phone, the rotVal stays zero.当我旋转手机时, rotVal保持为零。

Is there a way to detect the orientation without rotating/recreating the layout?有没有办法在不旋转/重新创建布局的情况下检测方向?

The second Stack Overflow answer you link to will work, but you can't inquire for the orientation like you do.您链接到的第二个 Stack Overflow答案将起作用,但您不能像您那样查询方向。

First, lock the app in portrait mode.首先,将应用程序锁定为纵向模式。 The following code should work and does not include all the animation code.以下代码应该可以工作,但不包括所有动画代码。

private lateinit var orientationListener: OrientationListener

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    // Code here to do Activity-related stuff.

    // Set up a listener to detect orientation changes.
    orientationListener = OrientationListener(this)
}

override fun onStart() {
    orientationListener.enable()
    super.onStart()
}

override fun onStop() {
    orientationListener.disable()
    super.onStop()
}

private class OrientationListener(context: Context?) : OrientationEventListener(context) {
    val ROTATION_O = 1 // portrait
    val ROTATION_90 = 2 // landscape counter-clockwise
    val ROTATION_180 = 3 // portrait inverted
    val ROTATION_270 = 4 // landscape clockwise

    private var rotation = 0
    override fun onOrientationChanged(orientation: Int) {
        if ((orientation < 35 || orientation > 325) && rotation != ROTATION_O) { // PORTRAIT
            rotation = ROTATION_O
        } else if (orientation > 145 && orientation < 215 && rotation != ROTATION_180) { // REVERSE PORTRAIT
            rotation = ROTATION_180
        } else if (orientation > 55 && orientation < 125 && rotation != ROTATION_270) { // REVERSE LANDSCAPE
            rotation = ROTATION_270
        } else if (orientation > 235 && orientation < 305 && rotation != ROTATION_90) { //LANDSCAPE
            rotation = ROTATION_90
        }

        // We'll just display the value, but it should be stashed for further use or acted upon immediately.
        Log.d("Applog", "rotation = $rotation")
    }
}

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

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