自动旋转屏幕
如果不想Activity重建 需要配置
android:configChanges="orientation|keyboardHidden|screenSize"
然后还要监听手机的旋转角度,这个一般搭配 看手机是否开启 自动旋转
功能。因为一般只有开启了 自动旋转才会用到处理自动旋转屏幕这块的逻辑,而且和锁定屏幕方向的处理情况会不太一样。
public class ScreenAutoRotationHelper {
private OrientationEventListener orientationEventListener; //屏幕旋转监听
private ContentObserver rotationObserver;
private OnRotationChangeListener onRotationChangeListener; //回调监听
private Context context;
private int currentOrientation;//当前方向
public ScreenAutoRotationHelper(final Context context) {
this.context = context;
//当可以自动旋转时监听
orientationEventListener = new OrientationEventListener(context) {
@Override
public void onOrientationChanged(int orientation) {
currentOrientation = orientation;
if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN) {
return; //手机平放时,检测不到有效的角度
}
if (onRotationChangeListener != null) {
onRotationChangeListener.onChanged(orientation);
}
}
};
rotationObserver = new ContentObserver(new Handler()) {
@Override
public void onChange(boolean selfChange) {
boolean isAuto = isScreenAutoRotate(context);
if (isAuto && orientationEventListener.canDetectOrientation()) {
orientationEventListener.enable();
} else {
orientationEventListener.disable();
}
}
};
}
/**
* 注册监听
*/
public void register() {
//注册 Settings.System.ACCELEROMETER_ROTATION
context.getContentResolver().registerContentObserver(Settings.System.getUriFor(
Settings.System.ACCELEROMETER_ROTATION), true, rotationObserver);
boolean isAuto = isScreenAutoRotate(context);
if (isAuto && orientationEventListener.canDetectOrientation()) {
orientationEventListener.enable();
}
}
/**
* 取消注册
*/
public void unregister() {
context.getContentResolver().unregisterContentObserver(rotationObserver);
if (orientationEventListener != null) {
orientationEventListener.disable();
}
}
public int getCurrentOrientation() {
return currentOrientation;
}
/***
* 一般根据旋转的角度 和 业务需求 来判断是否可以旋转
**/
public static boolean isCanRotation(int lastValue, int curValue) {
final int VALUE = 80;
if (lastValue < VALUE) {
return curValue > lastValue + VALUE && curValue < 360 + lastValue - VALUE;
} else if (lastValue >= 360 - VALUE) {
return curValue < lastValue - VALUE && curValue > VALUE - (360 - lastValue);
} else {
return Math.abs(lastValue - curValue) >= VALUE;
}
}
public interface OnRotationChangeListener {
void onChanged(int orientation);
}
public void setOnOrientationEventListener(OnRotationChangeListener listener) {
this.onRotationChangeListener = listener;
}
/**
* 判断是否开启了 “屏幕自动旋转”
*/
public static boolean isScreenAutoRotate(Context context) {
int isAutoRotation = 0;
try {
isAutoRotation = Settings.System.getInt(context.getContentResolver(),
Settings.System.ACCELEROMETER_ROTATION);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
return isAutoRotation == 1;
}
}
public class MainActivity extends AppCompatActivity {
private ScreenAutoRotationHelper screenAutoRotationHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
screenAutoRotationHelper = new ScreenAutoRotationHelper(this);
screenAutoRotationHelper.setOnOrientationEventListener(new ScreenAutoRotationHelper.OnRotationChangeListener() {
@Override
public void onChanged(int orientation) {
//当可以自动旋转时,自动切换
boolean isAuto = TCScreenOritationUtil.isScreenAutoRotate(MainActivity.this);
if (isAuto) {
if (orientation > 345 || orientation < 15) { //旋转
//处理旋转逻辑
} else if (orientation < 295 && orientation > 75) {
//处理旋转逻辑
}
}
}
});
screenAutoRotationHelper.register();
}
@Override
protected void onDestroy() {
super.onDestroy();
screenAutoRotationHelper.unregister();
}
}
可以参考哔哩哔哩客户端,视频详情页旋转逻辑。里面其实还可以再加一些逻辑,当用户手动切换横竖屏时,不会立即自动旋转,而是当再旋转过一定角度后,才生效。