安卓屏幕旋转

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

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();   // 反向横屏
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值