直接上完整代码:
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.view.OrientationEventListener;
import android.view.Surface;
import android.widget.Button;
import com.ldx.canvasdrawdemo.R;
public class Main17Activity extends AppCompatActivity {
private Button btn1;
private Button btn2;
//默认位置
private int mOrientation = -1;
//锁定屏幕方向,无论如何改变,屏幕方向不变
private boolean mLockScreen = false;
private OrientationEventListener mOrientationListener ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main17xxx);
btn1 = findViewById(R.id.btn_portrait);
btn2 = findViewById(R.id.btn_revert_portrait);
//横竖屏切换
btn1.setOnClickListener(view -> {
changeScreenOrientation();
});
//锁定 解锁
btn2.setOnClickListener(view -> {
mLockScreen = !mLockScreen;
if (mLockScreen){
btn2.setText("解锁屏幕");
}else{
btn2.setText("锁定屏幕");
}
lockScreen();
});
mOrientationListener = new OrientationEventListener(this) {
@Override
public void onOrientationChanged(int orientation) {
if (isFinishing()) {
return;
}
//记录用户手机上一次放置的位置
int mLastOrientation = mOrientation;
if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN) {
//手机平放时,检测不到有效的角度
mOrientation = -1;
return;
}
if (orientation > 350 || orientation < 10) {
//0度,用户竖直拿着手机
mOrientation = 0;
} else if (orientation > 80 && orientation < 100) {
//90度,用户右侧横屏拿着手机
mOrientation = 90;
} else if (orientation > 170 && orientation < 190) {
//180度,用户反向竖直拿着手机
mOrientation = 180;
} else if (orientation > 260 && orientation < 280) {
//270度,用户左侧横屏拿着手机
mOrientation = 270;
}
//如果用户锁定了屏幕,不再开启代码自动旋转了,直接return
if (mLockScreen) {
return;
}
//如果用户关闭了手机的屏幕旋转功能,但是横屏之间可以切换
//关闭了自动旋转,也就是锁定了屏幕
if (isLock()) {
if (mLastOrientation != mOrientation){
if (mOrientation == 90){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
}else if (mOrientation == 270){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
return;
}
//当检测到用户手机位置距离上一次记录的手机位置发生了改变,开启屏幕自动旋转
if (mLastOrientation != mOrientation) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}
}
};
if (mOrientationListener.canDetectOrientation()) {//判断设备是否支持
mOrientationListener.enable();
} else {
mOrientationListener.disable();//注销
//当前设备不支持手机旋转!
}
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
System.out.println("=========orientation=========="+newConfig.orientation);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mOrientationListener != null){
mOrientationListener.disable();
}
}
@Override
protected void onResume() {
super.onResume();
if (mOrientationListener != null){
mOrientationListener.enable();
}
}
@Override
protected void onStop() {
super.onStop();
if (mOrientationListener != null){
mOrientationListener.disable();
}
}
/**
* 1 手机已开启屏幕旋转功能
* 0 手机未开启屏幕旋转功能
*/
private boolean isLock(){
try {
int flag = Settings.System.getInt( Main17Activity.this.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION );
if (flag == 1)return false;
return true;
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
return false;
}
}
/**
* 横竖屏切换
*/
private void changeScreenOrientation() {
//获取用户当前屏幕的横竖位置
int currentOrientation = getResources().getConfiguration().orientation;
//判断并设置用户点击全屏/半屏按钮的显示逻辑
if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {
//如果屏幕当前是横屏显示,则设置屏幕锁死为竖屏显示
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else if (currentOrientation == Configuration.ORIENTATION_PORTRAIT) {
//如果屏幕当前是竖屏显示,则设置屏幕锁死为横屏显示
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
/**
* 锁定/解锁屏幕点击事件
*/
private void lockScreen(){
if(mLockScreen){
//锁定屏幕
//获取用户当前屏幕的横竖位置
int currentOrientation = getResources().getConfiguration().orientation;
//判断并设置用户点击锁定屏幕按钮的显示逻辑
if(currentOrientation == Configuration.ORIENTATION_LANDSCAPE){
//如果屏幕当前是横屏显示,则设置屏幕锁死为横屏显示
if(mOrientation == 90){
//正向横屏
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else if(mOrientation == 270){
//反向横屏
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
}
} else if(currentOrientation == Configuration.ORIENTATION_PORTRAIT) {
//如果屏幕当前是竖屏显示,则设置屏幕锁死为竖屏显示
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
} else {
//解锁屏幕
if (!isLock()){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}
}
}
/**
* 获取当前屏幕旋转角度
*
* 0 - 表示是竖屏 90 - 表示是左横屏(正向) 180 - 表示是反向竖屏 270表示是右横屏(反向)
*/
public int getDisplayRotation() {
int rotation = getWindowManager().getDefaultDisplay().getRotation();
switch (rotation) {
case Surface.ROTATION_0:
return 0;
case Surface.ROTATION_90:
return 90;
case Surface.ROTATION_180:
return 180;
case Surface.ROTATION_270:
return 270;
}
return 0;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal">
<Button
android:id="@+id/btn_portrait"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="横竖屏切换"/>
<Button
android:id="@+id/btn_revert_portrait"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="锁定屏幕"/>
</LinearLayout>
public int getDisplayRotation() {
int rotation = getWindowManager().getDefaultDisplay().getRotation();
switch (rotation) {
case Surface.ROTATION_0:
return 0;
case Surface.ROTATION_90:
return 90;
case Surface.ROTATION_180:
return 180;
case Surface.ROTATION_270:
return 270;
}
return 0;
}