public SreenOrientationListener mSreenOrientationListener;
mSreenOrientationListener = new SreenOrientationListener(getContext());
@Override
protected void onResume() {
super.onResume();
mSreenOrientationListener.enable();
}
@Override
protected void onPause() {
super.onPause();
mSreenOrientationListener.disable();
}
/**
* 屏幕旋转
* */
class SreenOrientationListener extends OrientationEventListener {
public SreenOrientationListener(Context context) {
super(context);
}
@Override
public void onOrientationChanged(int orientation) {
if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN) {
return; // 手机平放时,检测不到有效的角度
}
// 只检测是否有四个角度的改变
if (orientation > 350 || orientation < 10) {
// 0度:手机默认竖屏状态(home键在正下方)
orientation = 0;
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
Log.i("orientation","orientation"+orientation);
} else if (orientation > 80 && orientation < 100) {
// 90度:手机顺时针旋转90度横屏(home建在左侧)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
Log.i("orientation","orientation"+orientation);
} else if (orientation > 170 && orientation < 190) {
// 手机顺时针旋转180度竖屏(home键在上方)
orientation = 180;
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Log.i("orientation","orientation"+orientation);
} else if (orientation > 260 && orientation < 280) {
// 手机顺时针旋转270度横屏,(home键在右侧)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Log.i("orientation","orientation"+orientation);
}
}
}
ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED,
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
ActivityInfo.SCREEN_ORIENTATION_USER,
ActivityInfo.SCREEN_ORIENTATION_BEHIND,
ActivityInfo.SCREEN_ORIENTATION_SENSOR,
ActivityInfo.SCREEN_ORIENTATION_NOSENSOR,
ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE,
ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT,
ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE,
ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR,