onConfiguration更改为窗口管理器中的视图不可靠



我正在向WindowManager添加一个View并覆盖它onConfigurationChanged如下所示的功能:

查看代码

// onConfigurationChanged should be called after config change has finished
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
// also tried executing following in the `GlobalLayoutListener` without any difference
onConfigOrSystemUIChanged(newConfig)
}
fun onConfigOrSystemUIChanged(newConfig: Configuration?) {
val screen = getScreenSize(this)
// screen should hold current screen size
// but this sometimes contains the wrong size, the one from before the config change
L.d("[%s] Config changed (%s)", logBaseInfo, screen)
}

服务代码

override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
val screen = getScreenSize(this)
L.d("Config changed in SERVICE: Screen: %s | newConfig: %s", screen, newConfig);
}

问题

我可以看到这在大约 99% 的情况下有效。我旋转我的设备,打开,关闭等等,一切正常。但是我时不时地看到它失败一次,这意味着我的视图在设备旋转后读取了错误的屏幕尺寸。知道为什么吗?

附加信息 - 日志

我在服务的onConfigurationChanged和视图的onConfigurationChanged中添加了一些日志。我有 5 个视图添加到WindowManager,并看到以下内容:

[OverlayService:168 onConfigurationChanged]: Config changed in SERVICE: Screen: Point(2076, 1080) | newConfig: {0 1.0 themeSeq = 0 showBtnBg = 0 232mcc1mnc [de_AT] ldltr sw360dp w692dp h336dp 480dpi nrml long hdr land finger -keyb/v/h -nav/h appBounds=Rect(144, 0 - 2220, 1080) s.839 mkbd/h desktop/d ?dc}
[BaseMviOverlayView:115 onConfigOrSystemUIChanged]: [MviHandleView[Index: 24]] Config changed (Point(2076, 1080))
[BaseMviOverlayView:115 onConfigOrSystemUIChanged]: [MviHandleView[Index: 26]] Config changed (Point(2076, 1080))
[BaseMviOverlayView:115 onConfigOrSystemUIChanged]: [MviHandleView[Index: 52]] Config changed (Point(2076, 1080))
[BaseMviOverlayView:115 onConfigOrSystemUIChanged]: [MviHandleView[Index: 53]] Config changed (Point(2076, 1080))
=> [BaseMviOverlayView:115 onConfigOrSystemUIChanged]: [MviHandleView[Index: 65]] Config changed (Point(1080, 2076))
// ... here some time that wents by ...
[OverlayService:168 onConfigurationChanged]: Config changed in SERVICE: Screen: Point(1080, 2076) | newConfig: {0 1.0 themeSeq = 0 showBtnBg = 0 232mcc1mnc [de_AT] ldltr sw360dp w360dp h668dp 480dpi nrml long hdr port finger -keyb/v/h -nav/h appBounds=Rect(0, 0 - 1080, 2076) s.840 mkbd/h desktop/d ?dc}
[BaseMviOverlayView:115 onConfigOrSystemUIChanged]: [MviHandleView[Index: 26]] Config changed (Point(1080, 2076))
[BaseMviOverlayView:115 onConfigOrSystemUIChanged]: [MviHandleView[Index: 52]] Config changed (Point(1080, 2076))
[BaseMviOverlayView:115 onConfigOrSystemUIChanged]: [MviHandleView[Index: 53]] Config changed (Point(1080, 2076))
[BaseMviOverlayView:115 onConfigOrSystemUIChanged]: [MviHandleView[Index: 65]] Config changed (Point(1080, 2076))
[BaseMviOverlayView:115 onConfigOrSystemUIChanged]: [MviHandleView[Index: 24]] Config changed (Point(1080, 2076))

在这里你看到一个视图(在日志行的开头标有=>,第 6 行(确实得到了错误的屏幕尺寸,我不知道为什么......

附加信息 - 函数

fun getScreenSize(context: Context) {
val wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
val outSize = Point()
wm.getDefaultDisplay().getSize(outSize)
return outSize
}

这可能是因为在屏幕旋转尚未完成时测量视图的原因。尝试设置超时。

clearTimeout(t)
t = setTimeout(function(){/*do your stuff here*/},200);

这里 t 是一个全局变量 -- 如果在测量屏幕尺寸之前旋转屏幕,则清除超时。尝试以毫秒为单位(在我的代码 200 中(使用时间以优化所需的行为。

最新更新