attuale app YouTube fa quello che stai chiedendo.
Mi sono occupato dello stesso tipo di problema nella mia applicazione per la riproduzione di video.
Se le forze utente orientamento orizzontale quando lui/lei era in ritratto, inizializzare OrientationEventListener che notifica sull'orientamento dispositivo da SensorManager
che va da 0 a 360.
Guarda se il dispositivo si inclina alla gamma orientamento orizzontale che sarebbe intorno (orientation >= 60 && orientation <= 120) || (orientation >= 240 && orientation <= 300)
e salva questo stato su enum o su una bandiera e poi se il dispositivo torna alla gamma di orientamento verticale (orientation <= 40 || orientation >= 320
), controlla enum/flag e chiama setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
, reimposta il flag/enum e disabilita il sensore fino a quando l'utente forza nuovamente l'orientamento.
ecco il codice demo:
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();
}
questo ha funzionato simile alla funzionalità di YouTube, spero che questo aiuta.
La mia applicazione è in modalità verticale per impostazione predefinita. L'utente può passare alla modalità orizzontale facendo clic su un pulsante o inclinando il dispositivo. Ma in modalità orizzontale, non c'è modo di riportare l'attività in modalità verticale. Se è necessario fornire un altro pulsante per forzare l'orientamento in verticale, questa sarà l'ultima opzione. Volevo sapere se è possibile impostare la modalità sensore dopo la forzatura in orizzontale. Ho provato la tua opzione t impostata sulla modalità sensore in pausa, ma tornerà alla modalità verticale quando voglio forzare in modalità orizzontale quando il dispositivo è in modalità verticale. – Kulai