actual aplicación YouTube hace lo que usted está pidiendo.
He tratado el mismo tipo de problema en mi aplicación para la reproducción de videos.
Si las fuerzas de usuario a la orientación horizontal cuando él/ella estaba en el retrato, inicializar OrientationEventListener la que le notifica sobre la orientación del dispositivo de SensorManager
que va de 0 a 360.
reloj si el dispositivo se inclina a la gama de orientación horizontal que estaría alrededor de (orientation >= 60 && orientation <= 120) || (orientation >= 240 && orientation <= 300)
y guarde este estado en una enumeración o un indicador y luego, si el dispositivo vuelve al rango de orientación vertical (orientation <= 40 || orientation >= 320
), marque la enum/flag y llame al setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
, reinicie el indicador/enum y deshabilite el sensor hasta que el usuario fuerce la orientación nuevamente.
Aquí está el código de demostración:
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();
}
Esto funcionó similar a la funcionalidad de YouTube, espero que esto ayude.
Mi aplicación está en modo retrato de forma predeterminada. El usuario puede cambiar al modo horizontal haciendo clic en un botón o inclinando el dispositivo. Pero en modo paisaje, no hay forma de devolver la actividad al modo vertical. Si se debe proporcionar otro botón para forzar la orientación a retrato, esta será la última opción. Quería saber si es posible establecer el modo de sensor después de forzar a paisaje. Probé la opción t configurar en modo sensor en pausa, pero volverá al modo vertical cuando quiero forzar al modo horizontal cuando el dispositivo está en modo vertical. – Kulai