PDA旋转,不重置activity;采取配置化,是否强制竖屏、强制横屏

This commit is contained in:
2024-05-29 14:06:56 +08:00
parent 23df4da1ff
commit 4af4aac1e5
8 changed files with 164 additions and 13 deletions

View File

@ -1,12 +1,16 @@
package chaoran.business.activity;
import android.annotation.SuppressLint;
import android.app.Activity;
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.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.MediaPlayer;
import android.os.Build;
@ -16,7 +20,9 @@ import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.OrientationEventListener;
import android.view.Surface;
import android.view.View;
import android.view.WindowManager;
import android.webkit.*;
import android.widget.ProgressBar;
import android.widget.Toast;
@ -25,6 +31,8 @@ import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.Map;
@ -49,6 +57,12 @@ import chaoran.business.utils.StatusBarUtil;
*/
public class MainActivity extends AppCompatActivity implements ResultListener{
public static int SCREEN_ROTATION = 3; // 屏幕旋转的设置
MyOrientationDetector myOrientationDetector;
private static int latestOrientation = 0; // 添加一个标志位;最后屏幕的方向
private WebView webView;
private Adapter adapter;
private VoiceEngine voiceEngine;
@ -66,6 +80,8 @@ public class MainActivity extends AppCompatActivity implements ResultListener{
//SCREEN_ORIENTATION_FULL_SENSOR 都可以旋转,但是在不打开旋转的情况下,一样可以旋转
//SCREEN_ORIENTATION_FULL_USER 关闭旋转则不会旋转了
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_USER);
myOrientationDetector = new MyOrientationDetector(this, this);
myOrientationDetector.enable();
}
@ -221,6 +237,7 @@ public class MainActivity extends AppCompatActivity implements ResultListener{
// voiceEngine.reload();
super.onResume();
if (adapter != null) {
myOrientationDetector.disable();
adapter.stop2();
adapter.start();
}
@ -287,6 +304,7 @@ public class MainActivity extends AppCompatActivity implements ResultListener{
String address = spf.getString("address", "").replaceAll(" ", "");
String path = spf.getString("path", "").replaceAll(" ", "");
String link = address.concat(":").concat(String.valueOf(port)).concat(path);
SCREEN_ROTATION = spf.getInt("screen_rotation", 3);
return link.startsWith("http://") ? link : "http://".concat(link);
}
@ -357,13 +375,16 @@ public class MainActivity extends AppCompatActivity implements ResultListener{
});
}
/* 监听角度的变化
/* 监听角度的变化 */
class MyOrientationDetector extends OrientationEventListener {
private int rotate;
public MyOrientationDetector(Context context) {
private Activity activity;
public MyOrientationDetector(Context context, Activity activity) {
super(context);
this.activity = activity;
}
@Override
@ -372,21 +393,104 @@ public class MainActivity extends AppCompatActivity implements ResultListener{
//0-57度 125-236度 306-360度 这些区间范围内为竖屏
//58-124度 237-305度 这些区间范围内为横屏
if (orientation >=0 && orientation <= 57 ) {
rotate = 1; // 正竖屏
rotate = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; // 正竖屏
}else if (orientation >=125 && orientation <= 236 ) {
rotate = 2; // 倒竖屏
rotate = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT; // 倒竖屏
}else if (orientation >=306 && orientation <= 360 ) {
rotate = 1; // 正竖屏
rotate = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; // 正竖屏
}else if (orientation >=58 && orientation <= 124 ) {
rotate = 3; // 正横屏
rotate = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE; // 正横屏
}else if (orientation >=237 && orientation <= 305 ) {
rotate = 4; // 倒横屏
rotate = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; // 倒横屏
}else {
rotate = 1; // 默认正竖屏
rotate = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; // 默认正竖屏
}
if (SCREEN_ROTATION == 1 || SCREEN_ROTATION == 3) {
if (rotate == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT || rotate == ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT) {
activity.setRequestedOrientation(rotate);
latestOrientation = rotate;
}
}
if (SCREEN_ROTATION == 2 || SCREEN_ROTATION == 3) {
if (rotate == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE || rotate == ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE) {
activity.setRequestedOrientation(rotate);
latestOrientation = rotate;
}
}
System.out.println("屏幕旋转角度监控::" + rotate);
}
}
/*class MyOrientationDetector implements SensorEventListener {
private Activity activity;
public MyOrientationDetector(Activity activity) {
this.activity = activity;
Sensor orientationSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
sensorManager.registerListener(this, orientationSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public void onSensorChanged(SensorEvent event) {
int angle = ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation();
if (latestOrientation == angle) {
return;
}
if (SCREEN_ROTATION == 1) {
if (Surface.ROTATION_0 == angle) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
latestOrientation = angle;
}else if (Surface.ROTATION_180 == angle) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
latestOrientation = angle;
}
return;
}else if (SCREEN_ROTATION == 2) {
if (Surface.ROTATION_90 == angle) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
latestOrientation = angle;
}else if (Surface.ROTATION_270 == angle) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
latestOrientation = angle;
}
return;
}
int tmpAngle = latestOrientation;
latestOrientation = angle;
switch (angle) {
case Surface.ROTATION_0:
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
case Surface.ROTATION_90:
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
case Surface.ROTATION_180:
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
break;
case Surface.ROTATION_270:
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
break;
default:
latestOrientation = tmpAngle;
break;
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}*/
@SuppressLint("WrongConstant")
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
}