private enum SensorStateChangeActions {
WATCH_FOR_LANDSCAPE_CHANGES, SWITCH_FROM_LANDSCAPE_TO_STANDARD, WATCH_FOR_POTRAIT_CHANGES, SWITCH_FROM_POTRAIT_TO_STANDARD;
}
public void goFullScreen() {
if(flimartAPP.isPortrait()) {
// setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}else {
updateImmersive(getResources().getConfiguration(), true);
updatePlayerLayout(getResources().getConfiguration());
}
mSensorStateChanges = SensorStateChangeActions.WATCH_FOR_LANDSCAPE_CHANGES;
if (null == sensorEvent)
initialiseSensor(true);
else
sensorEvent.enable();
}
public void shrinkToPotraitMode() {
if(!flimartAPP.isPortrait()) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}else {
updateImmersive(getResources().getConfiguration(), false);
updatePlayerLayout(getResources().getConfiguration());
}
mSensorStateChanges = SensorStateChangeActions.WATCH_FOR_POTRAIT_CHANGES;
if (null == sensorEvent)
initialiseSensor(true);
else
sensorEvent.enable();
}
public void shrink() {
updateImmersive(getResources().getConfiguration(), false);
updatePlayerLayout(getResources().getConfiguration());
mSensorStateChanges = SensorStateChangeActions.WATCH_FOR_POTRAIT_CHANGES;
if (null == sensorEvent)
initialiseSensor(true);
else
sensorEvent.enable();
}
private SensorStateChangeActions mSensorStateChanges;
OrientationEventListener sensorEvent;
public 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(orientation == OrientationEventListener.ORIENTATION_UNKNOWN) {
return; //手机平放时,检测不到有效的角度
}
//只检测是否有四个角度的改变
if( null != mSensorStateChanges &&
mSensorStateChanges == SensorStateChangeActions.WATCH_FOR_LANDSCAPE_CHANGES &&
orientation > 350 || orientation< 10 && (orientation > 170 && orientation < 190)) { //0度,竖屏
mSensorStateChanges = SensorStateChangeActions.SWITCH_FROM_LANDSCAPE_TO_STANDARD;
}
else if(null != mSensorStateChanges
&& mSensorStateChanges == SensorStateChangeActions.WATCH_FOR_POTRAIT_CHANGES
&& (orientation > 80 &&orientation < 100) && (orientation > 170 &&orientation < 190)) { //90度,横屏
mSensorStateChanges = SensorStateChangeActions.SWITCH_FROM_POTRAIT_TO_STANDARD;
}
else if(null != mSensorStateChanges &&
mSensorStateChanges == SensorStateChangeActions.SWITCH_FROM_POTRAIT_TO_STANDARD &&
(orientation > 80 &&orientation < 100) && (orientation > 170 &&orientation < 190) ) { //180度,竖屏
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
mSensorStateChanges = null;
sensorEvent.disable();
}
else if(null != mSensorStateChanges
&& mSensorStateChanges == SensorStateChangeActions.SWITCH_FROM_LANDSCAPE_TO_STANDARD &&
orientation > 350 || orientation< 10 && orientation > 260 &&orientation < 280 ) { //270度,横屏
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
mSensorStateChanges = null;
sensorEvent.disable();
}
else {
return;
}
}
};
if (enable)
sensorEvent.enable();
}