监听屏幕旋转及旋转屏幕

对象:

android.view.OrientationEventListener

实现方法:

@Override
public void onOrientationChanged(int orientation) {
// 转换角度为:0,90,180,270
if ((orientation = checkOrientation(orientation)) == mOrientation)
return;
mOrientation = orientation;

switch (orientation) {
case Surface.ROTATION_180: // 反向竖屏
break;
case Surface.ROTATION_0: // 竖屏
break;
case Surface.ROTATION_90: // 右横屏
this.mActivity.setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
break;
case Surface.ROTATION_270: // 左横屏
this.mActivity.setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
}
Log.i(TAG, "onOrientationChanged -> " + orientation);
}

// 屏幕旋转角度改变转换
private int checkOrientation(int orientation) {
return orientation / 90;
}

使用方法:

mOrientationListener = new OrientationListener(this);
if (mOrientationListener.canDetectOrientation()) {
mOrientationListener.enable();
} else {
mOrientationListener.disable();
}

 

 
posted @ 2018-09-15 16:27  后天打老虎  阅读(860)  评论(0编辑  收藏  举报