我想通过设置在点击按钮时强制屏幕方向为横向
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
它工作正常。现在我希望应用程序跟随传感器,以便在倾斜回纵向时将方向恢复为纵向。我知道这可以通过设置 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
但不知道在哪里设置。如果方向强制为横向,则无论您向任何方向倾斜,方向都将保持横向。任何人都可以指定如何重置方向标志吗?
最佳答案
当前的 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 功能类似,希望对您有所帮助。
关于Android 屏幕方向传感器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4732448/