class MyOrientoinListener extends OrientationEventListener {
public MyOrientoinListener(Context context) {
super(context);
}
public MyOrientoinListener(Context context, int rate) {
super(context, rate);
}
@Override
public void onOrientationChanged(int orientation) {
Log.d(TAG, "orention" + orientation);
int screenOrientation = getResources().getConfiguration().orientation;
if (((orientation >= 0) && (orientation < 45)) || (orientation > 315)) {
if (screenOrientation != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT && orientation != ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT) {
Log.d(TAG, "设置竖屏");
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
oriBtn.setText("竖屏");
}
} else if (orientation > 225 && orientation < 315) {
Log.d(TAG, "设置横屏");
if (screenOrientation != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
oriBtn.setText("横屏");
}
} else if (orientation > 45 && orientation < 135) {
Log.d(TAG, "反向横屏");
if (screenOrientation != ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
oriBtn.setText("反向横屏");
}
} else if (orientation > 135 && orientation < 225) {
Log.d(TAG, "反向竖屏");
if (screenOrientation != ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
oriBtn.setText("反向竖屏");
}
}
}
}