Looking at a lot of cases about mobile phone rotation, I summarized them;
1. By using the onConfigurationChanged() method, the screen is dynamically loaded when it rotates, but this method can only obtain the horizontal and vertical screens, and can't judge the current rotation angle;
@Override
protected void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Configuration mConfiguration = this.getResources().getConfiguration();
int ori = mConfiguration.orientation;
if (ori == mConfiguration.ORIENTATION_LANDSCAPE) {
//Horizontal screen
horizontalScreen();
} else if (ori == mConfiguration.ORIENTATION_PORTRAIT) {
//Vertical screen
VerticalScreen();
}
}
2. Obtain the screen rotation angle of the current window through the dispatchTouchEvent event event;
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
int orientation = getScreenOrientation();
if(orientation==0){
//Vertical screen processing logic
}else{
//Horizontal screen processing logic
}
}
}
}
mWindowManager = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
int rotation = mWindowManager.getDefaultDisplay().getRotation();
//0 means vertical screen; 90 means left horizontal screen; 180 means reverse vertical screen; 270 means right horizontal screen
if (rotation == Surface.ROTATION_0) {
return 0;
} else if (rotation == Surface.ROTATION_90) {
return 90;
} else if (rotation == Surface.ROTATION_180) {
return 180;
} else if (rotation == Surface.ROTATION_270) {
return 270;
}
return 0;
}
3. The other way is to implement it through OrientationEventListener. There are many implementations on the Internet, so I won't write them here;