旋转屏幕,使用的是重力感应自旋转,但是一切手动切换了屏幕的旋转,那么重力旋转就无效了

This commit is contained in:
wujie
2022-11-02 09:32:52 +08:00
parent 6786cf2908
commit d63d7ecee3
3 changed files with 58 additions and 28 deletions

View File

@ -5,6 +5,7 @@ import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.hardware.SensorManager;
import android.media.MediaPlayer;
@ -52,29 +53,14 @@ public class MainActivity extends AppCompatActivity implements ResultListener{
private ProgressBar progressBar;
private ActionBar actionBar;
// private OrientationEventListener orientationEventListener;
// public static int rotate = 0; // 屏幕旋转的角度
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT); // 始终竖屏,由重力感应
// 实时监控屏幕旋转
// orientationEventListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL)
// {
//
// @Override
// public void onOrientationChanged(int i) {
// rotate = i;
// }
// };
setContentView(R.layout.activity_main);
initView();
initData();
// this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
// this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
}
@ -205,7 +191,7 @@ public class MainActivity extends AppCompatActivity implements ResultListener{
}
@Override
protected void onDestroy() {
adapter.stop();
// adapter.stop();
super.onDestroy();
if (diPlayer != null) {
diPlayer.stop();
@ -306,4 +292,37 @@ public class MainActivity extends AppCompatActivity implements ResultListener{
}
};
/* 监听角度的变化
class MyOrientationDetector extends OrientationEventListener {
private int rotate;
public MyOrientationDetector(Context context) {
super(context);
}
@Override
public void onOrientationChanged(int orientation) {
//假设屏幕旋转被打开。则设置屏幕可旋转
//0-57度 125-236度 306-360度 这些区间范围内为竖屏
//58-124度 237-305度 这些区间范围内为横屏
if (orientation >=0 && orientation <= 57 ) {
rotate = 1; // 正竖屏
}else if (orientation >=125 && orientation <= 236 ) {
rotate = 2; // 倒竖屏
}else if (orientation >=306 && orientation <= 360 ) {
rotate = 1; // 正竖屏
}else if (orientation >=58 && orientation <= 124 ) {
rotate = 3; // 正横屏
}else if (orientation >=237 && orientation <= 305 ) {
rotate = 4; // 倒横屏
}else {
rotate = 1; // 默认正竖屏
}
System.out.println("屏幕旋转角度监控::" + rotate);
}
}*/
}