Capture orientation change event before it happens

前端 未结 2 737
甜味超标
甜味超标 2021-01-28 04:30

It\'s simple to lock the orientation to portrait or landscape.


         


        
2条回答
  •  忘掉有多难
    2021-01-28 05:00

    1. Force the activity to stay in one mode (e.g. landscape) via setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); in onCreate(..)

    2. Attach your own orientation listener,

      orientationEventListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_UI) {

              @Override
              public void onOrientationChanged(int orientation) {
                  if (Math.abs(orientation - 90) < 30){
                         // your device orientation has changed, you can update UI                        accordingly, (layout won't rotate).
              }
          };
          orientationEventListener.enable();
      

    Keep in mind that if you do this you will receive x & y axis of touch events 'inversed' when you discard the configuration change. Also, if you have a navigation bar, it will remain where it is and won't rotate as well!

提交回复
热议问题