Android APP旋转屏幕

1、自动感应传感器的变化(不用开启自动旋转功能)


	//在Activity中设置即可
	setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);

2、点击Button旋转屏幕


    /**
    * Button点击事件
    **/
    @Override
    public void onClick(View v) {
        if (isWideScrren()) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        } else {
        	//只能是最简单的横屏,不是反向横屏 SCREEN_ORIENTATION_REVERSE_LANDSCAPE
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
    }



    /**
     * 当前是否是横屏
     * @return true:是横屏;false:是竖屏
     */
    public boolean isWideScrren() {
        WindowManager mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        Display display = mWindowManager.getDefaultDisplay();
        Point pt = new Point();
        display.getSize(pt);
        return pt.x > pt.y;
    }

3、点击Button旋转屏幕(支持正反两种横屏)

步骤1:写一个Listener,监听传感器的旋转角度


/**
 * @author Guan
 * @date Created on 2019/5/5
 */
public class MyOrientationEventListener extends OrientationEventListener {

    private static final String TAG = "OrientationEvent";

    private WindowManager mWindowManager;
    private int mLastOrientation = -1;

    private static final int ANGLE_0 = 0;
    private static final int ANGLE_90 = 90;
    private static final int ANGLE_180 = 180;
    private static final int ANGLE_270 = 270;

    private Context context;
    private OnChangeOrientationListener listener;


    public MyOrientationEventListener(Context context) {
        super(context);
        this.context = context;
        mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    }

    public void setListener(OnChangeOrientationListener listener) {
        this.listener = listener;
    }

    @Override
    public void onOrientationChanged(int orientation) {


        int value = getCurentOrientationEx(orientation);
        if (value == mLastOrientation) {
            //角度不够,不旋转屏幕
            return;
        }
        Log.i(TAG, "旋转屏幕:value=" + value);


        if (mLastOrientation != -1 && !isWideScrren()) {
            //横竖屏切换由Activity中的Button控制
            //所以,如果当前是竖屏,不做处理
            //如果mLastOrientation==-1,说明是系统刚刚初始化,这个时候需要return
            return;
        }

        mLastOrientation = value;

        if (listener != null) {
            if (value == ANGLE_270) {
                Log.i(TAG, "正向横屏显示");
                listener.onLandscape(true);
            } else if (value == ANGLE_90) {
                Log.i(TAG, "反向横屏显示");
                listener.onLandscape(false);
            }
        }


    }

    public boolean isWideScrren() {
        Display display = mWindowManager.getDefaultDisplay();
        Point pt = new Point();
        display.getSize(pt);
        return pt.x > pt.y;
    }


    private int getCurentOrientationEx(int orientation) {
        int value = 0;
        if (orientation >= 315 || orientation < 45) {
            // 0度
            value = ANGLE_0;
        }
        if (orientation >= 45 && orientation < 135) {
            // 90度
            value = ANGLE_90;
        }
        if (orientation >= 135 && orientation < 225) {
            // 180度
            value = ANGLE_180;
        }
        if (orientation >= 225 && orientation < 315) {
            // 270度
            value = ANGLE_270;
        }
        return value;

    }


    /**
     * 感应角度变化,并做出回调
     */
    interface OnChangeOrientationListener {


        /**
         * 横屏分正向横屏(靠左手方向横屏)和反向横屏(靠右手方向横屏)
         *
         * @param isNormalLandscape true:正向横屏;false:反向横屏
         */
        void onLandscape(boolean isNormalLandscape);


        /**
         * 竖屏
         */
        void onPortrait();
    }

}



步骤二:在Activity中实现接口OnChangeOrientationListener ,对旋转手机的动作做出相应


	//1、实例化对象
	MyOrientationEventListener mOrientationListener = new MyOrientationEventListener(mContext);
    mOrientationListener.setListener(this);


	//2、点击Buton进行横竖屏切换
	mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOrientationListener.isWideScrren()) {
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                } else {
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                }
            }
        });


	//3、开启和关闭监听
    @Override
    protected void onResume() {
        super.onResume();
        mOrientationListener.enable();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mOrientationListener.disable();
    }

	//4、对角度变化的回调进行处理()
    @Override
    public void onLandscape(boolean isNormalLandscape) {
        if (isNormalLandscape) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        } else {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
        }
    }

    @Override
    public void onPortrait() {
    	//因为横竖屏的切换交给了button处理,所以这里不再处理
    }


4、点击Button旋转屏幕,同时支持传感器自动调整

未完待续…

注意:
在Manifest文件中添加属性android:configChanges=“orientation”,表示在屏幕旋转后不会重新创建Activity。和上文介绍的旋转屏幕是两码事。

你可能感兴趣的:(Android)