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