写点什么

安卓 - 如何用正确的姿势监听 Android 屏幕旋转,flutter 修改 textfield 内容

用户头像
Android架构
关注
发布于: 23 小时前

val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as


WindowManager


val rotation = windowManager.defaultDisplay?.rotation


//rotation 为常量 0、1、2、3,分别表示屏幕的四个?向


通过这样的判断基本上能将屏幕旋转事件监听准确了,onOrientationChanged 这个回调很灵敏,? 机屏幕稍微动?下就会回调。那我希望模拟正常的屏幕旋转事件来修改悬浮球的位置,总不能很频繁 的刷新吧。这?做?下控制就好,全部代码如下:


object ScreenOrientationHelper {


val ORIENTATION_TYPE_0 = 0


val ORIENTATION_TYPE_90 = 90


val ORIENTATION_TYPE_180 = 180


val ORIENTATION_TYPE_270 = 270


private var mOrientationEventListener: OrientationEventListener? = null


private var mScreenOrientationChangeListener:


ScreenOrientationChangeListener? = null


private var currentType = ORIENTATION_TYPE_0


fun init(context: Context, listener: ScreenOrientationChangeListener) {


mScreenOrientationChangeListener = listener


mOrientationEventListener = object :


OrientationEventListener(context) {


override fun onOrientationChanged(orientation: Int) {


if (mScreenOrientationChangeListener == null) {


return


}


if (orientation > 340 || orientation < 20) {


//0


if (currentType == 0) {


return


}


if (getScreenRotation(context) == Surface.ROTATION_0) {


mScreenOrientationChangeListener!!.onChange(ORIENTATION_TYPE_0)


currentType = ORIENTATION_TYPE_0


}


} else if (orientation in 71..109) {


//90


if (currentType == 90) {


return


}


val angle = getScreenRotation(context)


if (angle == Surface.ROTATION_270) {


mScreenOrientationChangeListener!!.onChange(ORIENTATION_TYPE_90)


currentType = ORIENTATION_TYPE_90


}


} else if (orientation in 161..199) {


//180


if (currentType == 180) {


return


}


val angle = getSc


《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
浏览器打开:qq.cn.hn/FTe 免费领取
复制代码


reenRotation(context)


if (angle == Surface.ROTATION_180) {


mScreenOrientationChangeListener!!.onChange(ORIENTATION_TYPE_180)


currentType = ORIENTATION_TYPE_180


}


} else if (orientation in 251..289) {


//270


if (currentType == 270) {


return


}


val angle = getScreenRotation(context)


if (angle == Surface.ROTATION_90) {


mScreenOrientationChangeListener!!.onChange(ORIENTATION_TYPE_270)


currentType = ORIENTATION_TYPE_270


}


}


}


}


register()


}


private fun getScreenRotation(context: Context): Int {


val windowManager =


context.getSystemService(Context.WINDOW_SERVICE) as WindowManager


return windowManager.defaultDisplay?.rotation ?: 0


}


fun register() {


if (mOrientationEventListener != null) {


mOrientationEventListener!!.enable()


}


}


fun unRegister() {


if (mOrientationEventListener != null) {


mOrientationEventListener!!.disable()


}


}


interface ScreenOrientationChangeListener {


/**


  • @param orientation


*/


fun onChange(orientation: Int)


}


}


使?的话,直接这样:


ScreenOrientationHelper.init(this, object :


ScreenOrientationHelper.ScreenOrientationChangeListener {


override fun onChange(orientation: Int) {


when(orientation) {


ScreenOrientationHelper.ORIENTATION_TYPE_0 -> {}


ScreenOrientationHelper.ORIENTATION_TYPE_90 -> {}


ScreenOrientationHelper.ORIENTATION_TYPE_180 -> {}


ScreenOrientationHelper.ORIENTATION_TYPE_270 -> {}


}

用户头像

Android架构

关注

还未添加个人签名 2021.10.31 加入

还未添加个人简介

评论

发布
暂无评论
安卓-如何用正确的姿势监听Android屏幕旋转,flutter修改textfield内容