SCREEN_ORIENTATION_LANDSCAPE upside down - Why?

后端 未结 4 834
眼角桃花
眼角桃花 2021-01-11 13:16

I am using the following code to set orientation locking per user preference:

 private void doLock(boolean locked) {
     if (locked) {
       int o = getRes         


        
相关标签:
4条回答
  • 2021-01-11 13:54

    Simple and functional>

        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
    
        <activity
            android:screenOrientation="sensorLandscape"
        </activity>
    
      </application>
    
    0 讨论(0)
  • 2021-01-11 13:59

    What you are describing is not a bug but rather the expected behavior from Android 2.2 or lower.

    @forgivegod provided a theoretically correct approach, except that for Android 2.2 or lower the screenOrientation.reverseLandscape and screenOrientation.reversePortrait values are not recognized, even if faked (as @forgivegod's code does).

    I bet you are seeing this problem when you rotate the phone clockwise (rotation=3) but not counter-clockwise (rotation=1).

    Try with Android 2.3 or higher and see what happens.

    0 讨论(0)
  • 2021-01-11 14:06

    try with universal landscape orientation

    private static final int ORIENTATION_90 = 1;
    private static final int ORIENTATION_0 = 0;
    private static final int ORIENTATION_180 = 2;
    private static final int ORIENTATION_270 = 3;
    
    switch (orientation)
        {
          default:
          case ORIENTATION_0: // Portrait
            //dostuff
            break;
          case ORIENTATION_90: // Landscape left
            //do stuff
            break;
          case ORIENTATION_180: // Upside down.
            //do stuff
            break;
          case ORIENTATION_270: // Landscape right
            //do stuff
            break;
          }
    
    0 讨论(0)
  • 2021-01-11 14:10

    Try the following to disable and enable orientation via code (this will work in API level 7 and up) :

    public static void disableRotation(Activity activity) {
        final int orientation = activity.getResources().getConfiguration().orientation;
        final int rotation = activity.getWindowManager().getDefaultDisplay()
                .getOrientation();
    
        // Copied from Android docs, since we don't have these values in Froyo
        // 2.2
        int SCREEN_ORIENTATION_REVERSE_LANDSCAPE = 8;
        int SCREEN_ORIENTATION_REVERSE_PORTRAIT = 9;
    
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.FROYO) {
            SCREEN_ORIENTATION_REVERSE_LANDSCAPE = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
            SCREEN_ORIENTATION_REVERSE_PORTRAIT = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
        }
    
        if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90) {
            if (orientation == Configuration.ORIENTATION_PORTRAIT) {
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            } else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            }
        } else if (rotation == Surface.ROTATION_180
                || rotation == Surface.ROTATION_270) {
            if (orientation == Configuration.ORIENTATION_PORTRAIT) {
                activity.setRequestedOrientation(SCREEN_ORIENTATION_REVERSE_PORTRAIT);
            } else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
                activity.setRequestedOrientation(SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
            }
        }
    }
    
    public static void enableRotation(Activity activity) {
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
    }
    
    0 讨论(0)
提交回复
热议问题