正常的判断屏幕方向的代码:
/** 获取屏幕是否是竖屏
* @return
*/
@SuppressLint("SwitchIntDef")
public boolean isScreenPortrait(){
int or = getRequestedOrientation();
switch (or) {
case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE :// 横屏
case ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE:
return false;
case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT :// 竖屏
case ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT:
return true;
default:
return true;
}
}
但是在onConfigurationChanged
switch (newConfig.orientation) {
case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE :// 横屏
case ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE:
APP.isScreenPortrait = false;
break;
case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT :// 竖屏
case ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT:
APP.isScreenPortrait = true;
break;
}
我就很随意的这样写了,本来我都不想测试,我觉得一定是对的。然后跑了一下程序,发现,是错的。我点击去
onConfigurationChanged(Configuration newConfig) 的switch (newConfig.orientation) 这个类,发现:
/**
* Overall orientation of the screen. May be one of
* {@link #ORIENTATION_LANDSCAPE}, {@link #ORIENTATION_PORTRAIT}.
*/
public int orientation;
发现这里的值只能是#ORIENTATION_LANDSCAPE}, {@link #ORIENTATION_PORTRAIT其中的一个。啊,自己的无知。应该去点进去看看人家会传递回来什么信息的。然后正确的判断是:
switch (newConfig.orientation) {
case Configuration.ORIENTATION_LANDSCAPE :// 横屏
APP.isScreenPortrait = false;
break;
case Configuration.ORIENTATION_PORTRAIT :// 竖屏
APP.isScreenPortrait = true;
break;
}