自动横屏切换

1.写一个工具类 ScreenSwitchUtils.java

import android.app.Activity;  
import android.content.Context;  
import android.content.pm.ActivityInfo;  
import android.hardware.Sensor;  
import android.hardware.SensorEvent;  
import android.hardware.SensorEventListener;  
import android.hardware.SensorManager;  
import android.os.Handler;  
import android.os.Message;  
  
public class ScreenSwitchUtils {  
    private volatile static ScreenSwitchUtils mInstance;  
    private Activity mActivity;  
    // 是否是竖屏  
    private boolean isPortrait = true;  
       
    private SensorManager sm;  
    private OrientationSensorListener listener;  
    private Sensor sensor;  
   
    private SensorManager sm1;  
    private Sensor sensor1;  
    private OrientationSensorListener1 listener1;  
       
    private Handler mHandler = new Handler() {  
        public void handleMessage(Message msg) {  
            switch (msg.what) {  
            case 888:  
                int orientation = msg.arg1;  
                if (orientation > 45 && orientation < 135) {  
                    if (isPortrait) {  
                        //切换成横屏反向:ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE  
                        mActivity.setRequestedOrientation(8);  
                        isPortrait = false;  
                    }  
                } else if (orientation > 135 && orientation < 225) {  
                    if (!isPortrait) {  
                        /* 
                         * 切换成竖屏反向:ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT(9), 
                         * ActivityInfo.SCREEN_ORIENTATION_SENSOR:根据重力感应自动旋转 
                         * 此处正常应该是上面第一个属性,但是在真机测试时显示为竖屏正向,所以用第二个替代 
                         */  
                        mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);  
                        isPortrait = true;  
                    }  
                } else if (orientation > 225 && orientation < 315) {  
                    if (isPortrait) {  
                        //切换成横屏:ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE  
                        mActivity.setRequestedOrientation(0);  
                        isPortrait = false;  
                    }  
                } else if ((orientation > 315 && orientation < 360) || (orientation > 0 && orientation < 45)) {  
                    if (!isPortrait) {  
                        //切换成竖屏ActivityInfo.SCREEN_ORIENTATION_PORTRAIT  
                        mActivity.setRequestedOrientation(1);  
                        isPortrait = true;  
                    }  
                }  
                break;  
            default:  
                break;  
            }  
   
        };  
    };  
       
    /** 返回ScreenSwitchUtils单例 **/  
    public static ScreenSwitchUtils init(Context context) {  
        if (mInstance == null) {  
            synchronized (ScreenSwitchUtils.class) {  
                if (mInstance == null) {  
                    mInstance = new ScreenSwitchUtils(context);  
                }  
            }  
        }  
        return mInstance;  
    }  
   
    private ScreenSwitchUtils(Context context) {  
        // 注册重力感应器,监听屏幕旋转  
        sm = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);  
        sensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);  
        listener = new OrientationSensorListener(mHandler);  
   
        // 根据 旋转之后/点击全屏之后 两者方向一致,激活sm.  
        sm1 = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);  
        sensor1 = sm1.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);  
        listener1 = new OrientationSensorListener1();  
    }  
       
    /** 开始监听 */  
    public void start(Activity activity) {  
        mActivity = activity;  
        sm.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_UI);  
    }  
   
    /** 停止监听 */  
    public void stop() {  
        sm.unregisterListener(listener);  
        sm1.unregisterListener(listener1);  
    }  
       
    /** 
     * 手动横竖屏切换方向 
     */  
    public void toggleScreen() {  
        sm.unregisterListener(listener);  
        sm1.registerListener(listener1, sensor1,SensorManager.SENSOR_DELAY_UI);  
        if (isPortrait) {  
            isPortrait = false;  
            // 切换成横屏  
            mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);  
        } else {  
            isPortrait = true;  
            // 切换成竖屏  
            mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);  
        }  
    }  
       
    public boolean isPortrait(){  
        return this.isPortrait;  
    }  
      
    /** 
     * 重力感应监听者 
     */  
    public class OrientationSensorListener implements SensorEventListener {  
        private static final int _DATA_X = 0;  
        private static final int _DATA_Y = 1;  
        private static final int _DATA_Z = 2;  
   
        public static final int ORIENTATION_UNKNOWN = -1;  
   
        private Handler rotateHandler;  
   
        public OrientationSensorListener(Handler handler) {  
            rotateHandler = handler;  
        }  
   
        public void onAccuracyChanged(Sensor arg0, int arg1) {  
        }  
   
        public void onSensorChanged(SensorEvent event) {  
            float[] values = event.values;  
            int orientation = ORIENTATION_UNKNOWN;  
            float X = -values[_DATA_X];  
            float Y = -values[_DATA_Y];  
            float Z = -values[_DATA_Z];  
            float magnitude = X * X + Y * Y;  
            // Don't trust the angle if the magnitude is small compared to the y  
            // value  
            if (magnitude * 4 >= Z * Z) {  
                // 屏幕旋转时  
                float OneEightyOverPi = 57.29577957855f;  
                float angle = (float) Math.atan2(-Y, X) * OneEightyOverPi;  
                orientation = 90 - (int) Math.round(angle);  
                // normalize to 0 - 359 range  
                while (orientation >= 360) {  
                    orientation -= 360;  
                }  
                while (orientation < 0) {  
                    orientation += 360;  
                }  
            }  
            if (rotateHandler != null) {  
                rotateHandler.obtainMessage(888, orientation, 0).sendToTarget();  
            }  
        }  
    }  
   
    public class OrientationSensorListener1 implements SensorEventListener {  
        private static final int _DATA_X = 0;  
        private static final int _DATA_Y = 1;  
        private static final int _DATA_Z = 2;  
   
        public static final int ORIENTATION_UNKNOWN = -1;  
   
        public OrientationSensorListener1() {  
        }  
   
        public void onAccuracyChanged(Sensor arg0, int arg1) {  
        }  
   
        public void onSensorChanged(SensorEvent event) {  
            float[] values = event.values;  
            int orientation = ORIENTATION_UNKNOWN;  
            float X = -values[_DATA_X];  
            float Y = -values[_DATA_Y];  
            float Z = -values[_DATA_Z];  
            float magnitude = X * X + Y * Y;  
            // Don't trust the angle if the magnitude is small compared to the y  
            // value  
            if (magnitude * 4 >= Z * Z) {  
                // 屏幕旋转时  
                float OneEightyOverPi = 57.29577957855f;  
                float angle = (float) Math.atan2(-Y, X) * OneEightyOverPi;  
                orientation = 90 - (int) Math.round(angle);  
                // normalize to 0 - 359 range  
                while (orientation >= 360) {  
                    orientation -= 360;  
                }  
                while (orientation < 0) {  
                    orientation += 360;  
                }  
            }  
            if (orientation > 225 && orientation < 315) {// 检测到当前实际是横屏  
                if (!isPortrait) {  
                    sm.registerListener(listener, sensor,SensorManager.SENSOR_DELAY_UI);  
                    sm1.unregisterListener(listener1);  
                }  
            } else if ((orientation > 315 && orientation < 360) || (orientation > 0 && orientation < 45)) {// 检测到当前实际是竖屏  
                if (isPortrait) {  
                    sm.registerListener(listener, sensor,SensorManager.SENSOR_DELAY_UI);  
                    sm1.unregisterListener(listener1);  
                }  
            }  
        }  
    }  
}  
2.在需要的Activity里引用

           private ScreenSwitchUtils instance;  

instance = ScreenSwitchUtils.init(this.getApplicationContext()); 


 //
    @Override  
    protected void onStart() {  
        super.onStart();  
        instance.start(this);  
    }  
   
    @Override  
    protected void onStop() {  
        super.onStop();  
        instance.stop();  
    }  
    //

 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
UniApp是支持横屏和竖屏切换的,可以通过一些简单的设置实现。首先,在页面配置文件(manifest.json)中设置“autoRotateScreen”属性,该属性用于指定是否支持自动切换屏幕方向。如果autoRotateScreen设置为true,则应用将自动根据用户设备方向旋转屏幕。如果不需要自动切换屏幕方向,可以将autoRotateScreen属性设置为false。 其次,UniApp还提供了一个插件“uni-rotate-screen”,可以手动控制屏幕方向。使用该插件,可以在需要横屏时,手动将屏幕旋转到横屏模式。在需要竖屏时,手动将屏幕旋转回竖屏模式。 具体实现步骤如下: 1. 在页面配置文件(manifest.json)中添加如下代码: ``` "app-plus": { "autoRotateScreen": true } ``` 2. 安装uni-rotate-screen插件,并在需要控制屏幕方向的页面中引入该插件。 ``` import uniRotateScreen from '@/uni_modules/uni-rotate-screen/js_sdk/uni-rotate-screen.js'; ``` 3. 使用uni-rotate-screen插件中的方法控制屏幕方向。 在需要横屏时: ``` uniRotateScreen.lockLandscape(); // 锁定横屏 ``` 在需要竖屏时: ``` uniRotateScreen.lockPortrait(); // 锁定竖屏 ``` 通过以上步骤,就可以在UniApp中实现横屏和竖屏的切换。值得注意的是,如果需要使用uni-rotate-screen插件实现手动控制屏幕方向,需要在app.vue组件的onShow和onHide生命周期方法中,手动添加如下代码: ``` uniRotateScreen.preventScreenShutoff(true); // 防止屏幕熄灭 ``` 这样才能够保证插件的正常使用,避免在使用过程中出现问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值