Android时时监测手机的旋转角度 根据旋转角度确定在什么角度加载

一、场景描述:

近期开发中遇到个问题,就是我们在做横竖屏切换的功能时,横竖屏布局是操作系统去感知的,作为开发员没法确定Activity在什么时候加载横屏布局,在什么时候加载竖屏布局。因此为了找到加载横屏布局与竖屏布局的分界点,我特别监控了屏幕旋转的角度,看在什么样的角度会加载横屏布局,在什么样的角度加载竖屏布局。

二、屏幕旋转度数变化示意图

度数变化,拿着手机顺时针旋转,度数会越变越大。

三、在Activity中监听手机的旋转角度,上代码。

/** * 时时监测屏幕方向是否发生改变 * @author wilson.xiong */class MyOrientationDetector extends OrientationEventListener {public MyOrientationDetector(Context context) {super(context);}@Overridepublic void onOrientationChanged(int orientation) {//如果屏幕旋转被打开,则设置屏幕可旋转//0-57度 125-236度 306-360度 这些区间范围内为竖屏//58-124度 237-305度 这些区间范围内为横屏if ((orientation == -1 || (orientation >= 0) && (orientation <= 57)) || ((orientation >= 125) && (orientation <= 236)) || (orientation >= 306 && orientation <= 360)) {mScreenOrientation = 1;//竖屏} else if ((orientation >= 58 && orientation <= 124) || ((orientation >= 237 && orientation <= 305))) {mScreenOrientation = 0;//横屏}//mOrientation = orientation;}}

该类的使用方法:

(1)在onResume()中调用enable()方法监听角度变化

@Overridepublic void onResume() {super.onResume();mDetector.enable();if (!isFirst) {if (GTConfig.instance().hasDickLoaded) {GTSQuote.updateGTSQuoteList();}refreshData();} else {isFirst = false;}}

(2)在onPause()方法中调用disable()方法停止监听

@Overridepublic void onPause() {super.onPause();mDetector.disable();}

版权声明:本文为博主原创文章,未经博主允许不得转载。

,『 不可能 』只存在於蠢人的字典里

Android时时监测手机的旋转角度 根据旋转角度确定在什么角度加载

相关文章:

你感兴趣的文章:

标签云: