有关于android屏幕横屏竖屏设置的问题

在manifest文件中在android的manifest文件中设置横屏竖屏,方向等的设置
比如需要监听apk横竖屏状态,就在这里加入orientation属性,如果需要指定当前apk为横屏或者竖屏,那么需要加上
android:screenOrientation=“portrait” 其中 值为portrait时强制为竖屏, 值为landscape时强制为横屏,这个时候就无法让apk自动切换横屏竖屏了。
在manifest文件中加上监听类型之后,在该service或者activity中添加监听处理的地方
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.i(“change orientation to”,newConfig.orientation+"");
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(getApplicationContext(), “横屏”, Toast.LENGTH_SHORT).show();

    } else {
        Toast.makeText(getApplicationContext(), "竖屏", Toast.LENGTH_SHORT).show();
      
    }
}
比如我这里监听到的横屏竖屏会在前面展示出来。
还有监测设备旋转角度的方法:
//屏幕方向监听
    OrientationEventListener mOrientationListener = new OrientationEventListener(this,
            SensorManager.SENSOR_DELAY_NORMAL) {
        @Override
        public void onOrientationChanged(int orientation) {
            if (orientation > 350 || orientation < 10) { //0度
                orientation = 0;
                Point outSize = new Point();
                getWindowManager().getDefaultDisplay().getRealSize(outSize);
                int x = outSize.x;
                int y = outSize.y;
                System.out.println("x = " + x + ",y = " + y);
                System.out.println("xxxxxxxxx:竖屏 ");
                try {
                    JSONObject obj = new JSONObject();
                    obj.put("width", x);
                    obj.put("height", y);
                    obj.put("orientation", "portrait");
                    JavascriptWXBridge.SrcChange(obj);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else if (orientation > 80 && orientation < 100) { //90度
                orientation = 90;
                Point outSize = new Point();
                getWindowManager().getDefaultDisplay().getRealSize(outSize);
                int x = outSize.x;
                int y = outSize.y;
                System.out.println("x = " + x + ",y = " + y);
                System.out.println("xxxxxxxxx:向右横屏 ");
                try {
                    JSONObject obj = new JSONObject();
                    obj.put("width", x);
                    obj.put("height", y);
                    obj.put("orientation", "landscape");
                    JavascriptWXBridge.SrcChange(obj);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else if (orientation > 170 && orientation < 190) { //180度
                orientation = 180;
                Point outSize = new Point();
                getWindowManager().getDefaultDisplay().getRealSize(outSize);
                int x = outSize.x;
                int y = outSize.y;
                System.out.println("x = " + x + ",y = " + y);
                System.out.println("xxxxxxxxx:竖屏 ");
                try {
                    JSONObject obj = new JSONObject();
                    obj.put("width", x);
                    obj.put("height", y);
                    obj.put("orientation", "portrait");
                    JavascriptWXBridge.SrcChange(obj);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else if (orientation > 260 && orientation < 280) { //270度
                orientation = 270;
                Point outSize = new Point();
                getWindowManager().getDefaultDisplay().getRealSize(outSize);
                int x = outSize.x;
                int y = outSize.y;
                System.out.println("x = " + x + ",y = " + y);
                System.out.println("xxxxxxxxx:向左横屏 ");
                try {
                    JSONObject obj = new JSONObject();
                    obj.put("width", x);
                    obj.put("height", y);
                    obj.put("orientation", "landscape");
                    JavascriptWXBridge.SrcChange(obj);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                return;
            }
        }
    };

顺带一提,切换横竖屏有个很复杂的过程,现简单记录如下https://blog.csdn.net/qq_36713816/article/details/80538467。连接有切换过程的生命周期

你可能感兴趣的:(java,android)