天天看點

Android Activity螢幕旋轉方向完全控制

2016-11-17 17:06

我們知道,系統提供了Android:configChanges="orientation" 清單檔案屬性以監聽螢幕旋轉,進而觸發onConfigurationChanged方法。

但這有時不能完全滿足我們的需求。比如典型的應用場景:視訊播放器的螢幕方向鎖功能。

下面貢獻螢幕旋轉完全控制類ActivityRotationController,解決一切螢幕旋轉問題。

[java]  view plain  copy

  1. import android.app.Activity;  
  2. import android.content.ContentResolver;  
  3. import android.content.pm.ActivityInfo;  
  4. import android.provider.Settings;  
  5. import android.provider.Settings.SettingNotFoundException;  
  6. import android.view.OrientationEventListener;  
  7. public class ActivityRotationController extends OrientationEventListener {  
  8.     private int systemRotation;  
  9.     private boolean activityRotation;  
  10.     private int activityOrientation;  
  11.     private Activity activity;  
  12.     public ActivityRotationController(Activity activity) {  
  13.         super(activity);  
  14.         this.activity = activity;  
  15.         activityOrientation = activity.getResources().getConfiguration().orientation;  
  16.         try {  
  17.             systemRotation = getScreenRotation(activity.getContentResolver());  
  18.         } catch (SettingNotFoundException e) {  
  19.             e.printStackTrace();  
  20.             systemRotation = -1;  
  21.         }  
  22.         openActivityRotation();  
  23.         enable();  
  24.     }  
  25.     public void openActivityRotation() {  
  26.         activityRotation = true;  
  27.     }  
  28.     public void closeActivityRotation() {  
  29.         activityRotation = false;  
  30.     }  
  31.     public boolean isActivityRotationEnabled() {  
  32.         return activityRotation;  
  33.     }  
  34.     public int getActivityOrientation() {  
  35.         return activityOrientation;  
  36.     }  
  37.     @Override  
  38.     public void enable() {  
  39.         super.enable();  
  40.         setScreenRotation(activity.getContentResolver(), 0);  
  41.     }  
  42.     @Override  
  43.     public void disable() {  
  44.         super.disable();  
  45.         if (systemRotation == -1) {  
  46.             return;  
  47.         }  
  48.         setScreenRotation(activity.getContentResolver(), systemRotation);  
  49.     }  
  50.     @Override  
  51.     public void onOrientationChanged(int orientation) {  
  52.         if (orientation < 0) {  
  53.             return;  
  54.         }  
  55.         int newOrientation= ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;  
  56.         if (orientation >= 0 && orientation <= 60) {  
  57.             newOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;  
  58.         }else if (orientation >60 && orientation <120) {  
  59.             newOrientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;  
  60.         }else if (orientation >=120 && orientation <=240) {  
  61.             newOrientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;  
  62.         }else if (orientation >240 && orientation <300) {  
  63.             newOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;  
  64.         }else if (orientation >=300 && orientation <=360) {  
  65.             newOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;  
  66.         }else{  
  67.             return;  
  68.         }  
  69.         if ((newOrientation != orientation) && activityRotation) {  
  70.             activity.setRequestedOrientation(newOrientation);  
  71.             activityOrientation = newOrientation;  
  72.         }  
  73.     }  
  74.     private void setScreenRotation(ContentResolver cr, int rotation) {  
  75.         Settings.System.putInt(cr, Settings.System.ACCELEROMETER_ROTATION,  
  76.                 rotation);  
  77.     }  
  78.     private int getScreenRotation(ContentResolver cr)  
  79.             throws SettingNotFoundException {  
  80.         return Settings.System.getInt(cr,  
  81.                 Settings.System.ACCELEROMETER_ROTATION);  
  82.     }  
  83. }  

作者:薄荷記賬  (轉載請注明原作者)