android系统屏幕旋转角度,应用界面横竖屏,设备旋转角度,三者的区别以及使用。

注意区分以下三种概念的区别!!!。以及使用这三种方式判断横竖屏的方式。

  1. 系统屏幕旋转角度

fun getSystemRotation(): Int {

val angle = (getSystemService(WINDOW_SERVICE) as WindowManager).defaultDisplay.rotation//系统屏幕旋转的角度值

return when(angle){

Surface.ROTATION_90->return 90//横屏

Surface.ROTATION_180->return 180//竖屏

Surface.ROTATION_270->return 270//横屏

else ->0//竖屏

}

}

与如下方式获取的角度值相同。但是下面方式在多屏时也可以应用。

var mDisplayManager = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE)

mDisplayManager?.getDisplay(DEFAULT_DISPLAY)?.rotation

  1. 应用界面横竖屏判断(注意:多屏模式下横竖屏判断,与UI界面上View的显示相关)

横竖屏参考点为应用界面的宽高比的变化:宽比高小于1为竖屏,宽比高大于1为横屏

fun getConfigureRotaion():Int{

//Configuration.ORIENTATION_PORTRAIT 1 ,Configuration.ORIENTATION_LANDSCAPE 2

return resources.configuration.orientation//1为竖屏,2为横屏

}

  1. 设备旋转角度

inner class MyOrientationEventListener(context: Context) : OrientationEventListener(context) {

override fun onOrientationChanged(orientation: Int) {

//orientation在竖屏时数值,同系统屏幕旋转角度数值相同。在横屏时数值,相差180度.

//当设备的方向改变时调用。 orientation 参数以度为单位,取值范围为 0 到 359。

//当设备处于自然位置时,orientation 为 0 度->竖屏

//当它的左侧在顶部,orientation 为90 度->横屏

//当它倒置时,orientation 为180 度->竖屏

//当它的右边是顶部,270 度->横屏

// ORIENTATION_UNKNOWN 当设备接近平面且无法确定方向时返回。即,水平放置桌面时。

}

}

猜你喜欢

转载自blog.csdn.net/github_27263697/article/details/129140104