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

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

@ -19,7 +19,9 @@
android:fitsSystemWindows="true" android:fitsSystemWindows="true"
android:name=".application.InitApplication" android:name=".application.InitApplication"
android:theme="@style/Theme.PdaWeb" android:theme="@style/Theme.PdaWeb"
android:usesCleartextTraffic="true"> android:usesCleartextTraffic="true"
android:configChanges="orientation|screenSize|keyboardHidden"
>
<activity <activity
android:name=".activity.NetworkSettingActivity" android:name=".activity.NetworkSettingActivity"
android:label="@string/title_activity_setting_network" /> android:label="@string/title_activity_setting_network" />

View File

@ -5,6 +5,7 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.ActivityInfo; import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.hardware.SensorManager; import android.hardware.SensorManager;
import android.media.MediaPlayer; import android.media.MediaPlayer;
@ -52,29 +53,14 @@ public class MainActivity extends AppCompatActivity implements ResultListener{
private ProgressBar progressBar; private ProgressBar progressBar;
private ActionBar actionBar; private ActionBar actionBar;
// private OrientationEventListener orientationEventListener;
// public static int rotate = 0; // 屏幕旋转的角度
@Override @Override
protected void onCreate(@Nullable Bundle savedInstanceState) { protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(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); setContentView(R.layout.activity_main);
initView(); initView();
initData(); initData();
// this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT); this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
// this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} }
@ -205,7 +191,7 @@ public class MainActivity extends AppCompatActivity implements ResultListener{
} }
@Override @Override
protected void onDestroy() { protected void onDestroy() {
adapter.stop(); // adapter.stop();
super.onDestroy(); super.onDestroy();
if (diPlayer != null) { if (diPlayer != null) {
diPlayer.stop(); 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);
}
}*/
} }

View File

@ -7,7 +7,9 @@ import android.content.Context;
import android.content.pm.ActivityInfo; import android.content.pm.ActivityInfo;
import android.os.Build; import android.os.Build;
import android.util.Log; import android.util.Log;
import android.view.Surface;
import android.view.View; import android.view.View;
import android.view.WindowManager;
import android.webkit.JavascriptInterface; import android.webkit.JavascriptInterface;
import java.net.Inet4Address; import java.net.Inet4Address;
@ -135,18 +137,25 @@ public class LocalAddressUtil {
@SuppressLint("JavascriptInterface") @SuppressLint("JavascriptInterface")
@JavascriptInterface @JavascriptInterface
public void rotateScreen(int type) { public void rotateScreen() {
// 使用的是view对象旋转出现的问题键盘并没有出现旋转 int angle = ((WindowManager)activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation();
// view.setRotationX(type);
// view.setRotationY(type);
// activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT); switch (angle) {
if (type == 0) { case Surface.ROTATION_0:
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}else {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT); activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
break;
case Surface.ROTATION_90:
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
break;
case Surface.ROTATION_180:
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
case Surface.ROTATION_270:
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
default:
break;
} }
// System.out.println("MainActivity.rotate :: " + MainActivity.rotate);
} }