2011-01-19 71 views
9

我想通過設置Android的屏幕方向傳感器

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

它正常工作,迫使屏幕方向上的按鈕,點擊景觀。現在,我希望應用程序遵循傳感器,以便在傾斜回肖像時將方向恢復爲縱向。我知道這可以通過設置setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);但不知道在哪裏設置。如果方向被強制爲橫向,無論您向任何方向傾斜,方向都將保持在橫向。任何人都可以指定如何重置方向標誌?

回答

1

這取決於您希望傳感器何時再次檢測到旋轉。

個人在一個應用程序,我深化發展我有一個特定的活動,我需要在縱向模式,所以我在這個活動中onPause()onResume()setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);使用setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);和它工作得很好,當我進入活動設置爲縱向如果它不是並且不允許改變並且退出傳感器再次工作...

你在哪裏試圖啓用和禁用傳感器?

+0

默認情況下,我的應用程序處於肖像模式。用戶可以通過單擊按鈕或傾斜設備切換到橫向模式。但在橫向模式下,無法將活動恢復到縱向模式。如果必須提供另一個按鈕來強制指向肖像,那麼這將是最後一個選項。我想知道是否可以在逼近風景後設置爲傳感器模式。我在暫停時嘗試將選項設置爲傳感器模式,但當設備處於縱向模式時,如果我想強制橫向模式,它會恢復爲縱向模式。 – Kulai 2011-01-19 07:43:39

4

我覺得這裏應該使用setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)

7

當前YouTube應用做你所要求的。
我在處理視頻播放應用程序時遇到了同樣的問題。

如果用戶強制方向爲橫向時,他/她在肖像,初始化OrientationEventListener它通知你設備的方向從SensorManager取值範圍從0到360

看,如果設備傾斜至橫向範圍這將是圍繞(orientation >= 60 && orientation <= 120) || (orientation >= 240 && orientation <= 300)並將此狀態保存爲枚舉或標誌,然後如果設備返回到縱向方向範圍(orientation <= 40 || orientation >= 320),請檢查枚舉/標誌並調用setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);,重置標誌/枚舉並禁用傳感器,直到用戶再次強制方向爲止。

下面是演示代碼:

private enum SensorStateChangeActions { 
     WATCH_FOR_LANDSCAPE_CHANGES, SWITCH_FROM_LANDSCAPE_TO_STANDARD, WATCH_FOR_POTRAIT_CHANGES, SWITCH_FROM_POTRAIT_TO_STANDARD; 
} 

private SensorStateChangeActions mSensorStateChanges; 

public void goFullScreen() { 
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE); 
     mSensorStateChanges = SensorStateChangeActions.WATCH_FOR_LANDSCAPE_CHANGES; 
     if (null == sensorEvent) 
      initialiseSensor(true); 
     else 
      sensorEvent.enable(); 
} 

public void shrinkToPotraitMode() { 
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
     mSensorStateChanges = SensorStateChangeActions.WATCH_FOR_POTRAIT_CHANGES; 
     if (null == sensorEvent) 
      initialiseSensor(true); 
     else 
      sensorEvent.enable(); 
} 
/** 
* Initialises system sensor to detect device orientation for player changes. 
* Don't enable sensor until playback starts on player 
* 
* @param enable if set, sensor will be enabled. 
*/ 
private void initialiseSensor(boolean enable) { 
    sensorEvent = new OrientationEventListener(this, 
      SensorManager.SENSOR_DELAY_NORMAL) { 

     @Override 
     public void onOrientationChanged(int orientation) { 
      /* 
      * This logic is useful when user explicitly changes orientation using player controls, in which case orientation changes gives no callbacks. 
      * we use sensor angle to anticipate orientation and make changes accordingly. 
      */ 
      if (null != mSensorStateChanges 
        && mSensorStateChanges == SensorStateChangeActions.WATCH_FOR_LANDSCAPE_CHANGES 
        && ((orientation >= 60 && orientation <= 120) || (orientation >= 240 && orientation <= 300))) { 
       mSensorStateChanges = SensorStateChangeActions.SWITCH_FROM_LANDSCAPE_TO_STANDARD; 
      } else if (null != mSensorStateChanges 
        && mSensorStateChanges == SensorStateChangeActions.SWITCH_FROM_LANDSCAPE_TO_STANDARD 
        && (orientation <= 40 || orientation >= 320)) { 
       setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); 
       mSensorStateChanges = null; 
       sensorEvent.disable(); 
      } else if (null != mSensorStateChanges 
        && mSensorStateChanges == SensorStateChangeActions.WATCH_FOR_POTRAIT_CHANGES 
        && ((orientation >= 300 && orientation <= 359) || (orientation >= 0 && orientation <= 45))) { 
       mSensorStateChanges = SensorStateChangeActions.SWITCH_FROM_POTRAIT_TO_STANDARD; 
      } else if (null != mSensorStateChanges 
        && mSensorStateChanges == SensorStateChangeActions.SWITCH_FROM_POTRAIT_TO_STANDARD 
        && ((orientation <= 300 && orientation >= 240) || (orientation <= 130 && orientation >= 60))) { 
       setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); 
       mSensorStateChanges = null; 
       sensorEvent.disable(); 
      } 
     } 
    }; 
    if (enable) 
     sensorEvent.enable(); 
} 

這個工作與YouTube類似的功能,希望這有助於。

+0

這正是我所期待的:一種模仿YouTube應用程序的方式!謝謝:) – wblaschko 2015-03-26 23:39:11