How to detect screen rotation through 180 degrees from landscape to landscape orientation?

后端 未结 3 1138
天涯浪人
天涯浪人 2020-12-03 10:43

I\'ve seen a few other questions similar to this but no answers.

Rotating from portrait to landscape (either direction) and back again, we get the helpful call to on

3条回答
  •  渐次进展
    2020-12-03 11:03

    I use this code to have it work for my case.

      OrientationEventListener mOrientationEventListener = new OrientationEventListener(mActivity)
      {
         @Override
         public void onOrientationChanged(int orientation)
         {
            if (orientation == ORIENTATION_UNKNOWN) return;
    
            int rotation = mActivity.getWindowManager().getDefaultDisplay().getRotation();
            switch (rotation) {
              case Surface.ROTATION_0:
                 android.util.Log.i(TAG, "changed ROTATION_0 - " + orientation);
                 break;
              case Surface.ROTATION_90:
                 android.util.Log.i(TAG, "changed ROTATION_90 - " + orientation);
                 break;
              case Surface.ROTATION_180:
                 android.util.Log.i(TAG, "changed ROTATION_180 - " + orientation);
                 break;
              case Surface.ROTATION_270:
                 android.util.Log.i(TAG, "changed ROTATION_270 - " + orientation);
                 break;
            }
            if ((rotation != mLastRotation) && (rotation & 0x1) == (mLastRotation & 0x1))
            {
               android.util.Log.i(TAG, "unhandled orientation changed >>> " + rotation);
            }
            mLastRotation = rotation;
         }
      };
    
      if (mOrientationEventListener.canDetectOrientation()){
         mOrientationEventListener.enable();
      }
    

提交回复
热议问题