检测android中的镜像方向更改

S.D*_*.D. 5 android screen-orientation

通常将设备旋转90度(纵向到横向或反向)会导致配置更改,活动被破坏并重新创建等,因此它可以Display.getRotation()在开始时保存值并使用它.

但是,当设备直接从0到180(纵向到纵向)或90到270(横向到横向)旋转时,不会进行任何配置更改,设备只需重新映射屏幕.这是有道理的,因为布局的纵横比不会改变,也不需要改变.但这使得Activity无法检测何时发生这种变化,即何时Surface.ROTATION_90进入Surface.ROTATION_270等等.

除了民意调查Display.getRotation(),有没有更好的方法来检测这种变化?

Gab*_*tti 5

您可以使用辅助类OrientationEventListener.

像这样的东西:

public void onOrientationChanged(int orientation) {
   if (orientation == ORIENTATION_UNKNOWN) return;

   int rotation = activity.getWindowManager().getDefaultDisplay()
         .getRotation();
   int degrees = 0;
   switch (rotation) {
     case Surface.ROTATION_0: ....
     case Surface.ROTATION_90: ....
     case Surface.ROTATION_180: .....
     case Surface.ROTATION_270: .....
   }

}
Run Code Online (Sandbox Code Playgroud)