安卓屏幕旋转

做过视屏的同学肯定经常与屏幕旋转打交道,今天我们就简单说一下,安卓手机中横竖屏旋转的实现。大神略过

public class ScreenRotateUtil extends OrientationEventListener{

    private boolean isBack;  // true 表示在横屏的状态下,按下返回键了,false 表示这时候已经是竖屏了
    private static final int ORIENTATION_PORTRAIT = 1;      // 竖屏
    private static final int ORIENTATION_LANDSCAPE = 2;     // 横屏
    private static final int ORIENTATION_REVERSE_LANDSCAPE = 3; // 反向横屏
    public int currentOreation = ORIENTATION_PORTRAIT; //当前重力感应方向

    public ScreenRotateUtil(Context context,ScreenRotateListener mScreenRotateListener) {
        super(context);
        this.mScreenRotateListener = mScreenRotateListener;
    }

    // 开启监听  建议activity的onCreate方法中调用
    @Override
    public void enable() {
        super.enable();
    }

    // 关闭监听  建议activity的onDestroy方法中调用
    @Override
    public void disable() {
        super.disable();
    }

    // 在横屏的状态下点击了返回键
    public void setBack(boolean b) {
        isBack = b;
    }

    /**
     * 是否是横屏 ,true为横屏<br/>
     * Configuration.ORIENTATION_LANDSCAPE-2横屏<br/>
     * Configuration.ORIENTATION_PORTRAIT-1竖屏
     *  context = Application.getAppContext()
     */
    public static boolean isLandscape(Context context) {
        return context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
    }

    /**
     * 检查当前用户是否系统锁定了屏幕
    * true : 可以转
    * false :  不可以转
    * */
    private boolean checkIsSystemOrientationEnabled() {
        boolean isOrientationEnabled;
        try {
            isOrientationEnabled = Settings.System.getInt(Application.getAppContext().getContentResolver(), Settings.System.ACCELEROMETER_ROTATION) == 1;
        } catch (Settings.SettingNotFoundException e) {
            isOrientationEnabled = false;
        }
        return isOrientationEnabled;
    }

    /**
     * 手动切换横屏的时候调用的时候
     */
    public void manualSwitchingLandscape(){
        currentOreation = ORIENTATION_LANDSCAPE;
    }

    @Override
    public void onOrientationChanged(int orientation) {
        int screenOrientation = Application.getAppContext().getResources().getConfiguration().orientation;
        if (((orientation >= 0) && (orientation < 45)) || (orientation > 315)) { // 设置竖屏
            if (currentOreation != ORIENTATION_PORTRAIT) {
                currentOreation = ORIENTATION_PORTRAIT;
                if (checkIsSystemOrientationEnabled()) {
                    LogUtil.e("ScreenRotateUtil", "设置竖屏screenOrientation = " + screenOrientation);
                    if (!isBack) {
                        mScreenRotateListener.onPortrait();
                    }
                    isBack = false;
                }
            }
            ((Activity) getContext()).setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
        } else if (orientation > 225 && orientation < 315) { // 设置横屏
            if (currentOreation != ORIENTATION_LANDSCAPE) {
                currentOreation = ORIENTATION_LANDSCAPE;
                if (checkIsSystemOrientationEnabled()) {
                    LogUtil.e("ScreenRotateUtil", "设置横屏screenOrientation = " + screenOrientation);
                    if (!isBack) {
                        mScreenRotateListener.onLandscape();
                    }
                }
            }
             ((Activity) getContext()).setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        } else if (orientation > 45 && orientation < 135) {  // 设置反向横屏
            if (currentOreation != ORIENTATION_REVERSE_LANDSCAPE) {
                currentOreation = ORIENTATION_REVERSE_LANDSCAPE;
                if (checkIsSystemOrientationEnabled()) {
                    LogUtil.e("ScreenRotateUtil", "设置反向横屏screenOrientation = " + screenOrientation);
                    if (!isBack) {
                        mScreenRotateListener.onReverseLandscape();
                    }
                }
            }
             ((Activity) getContext()).setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
        }
    }

    private ScreenRotateListener mScreenRotateListener;
    public interface ScreenRotateListener {   // 屏幕监听的接口
        void onPortrait();           // 竖屏
        void onLandscape();          // 横屏
        void onReverseLandscape();   // 反向横屏
    }
}


猜你喜欢

转载自blog.csdn.net/pengyu1801/article/details/79231796
今日推荐