我正在向View
添加WindowManager
并覆盖它的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
}
答案 0 :(得分:0)
这可能是由于在屏幕旋转尚未完成时测量视图而引起的。尝试设置超时时间。
clearTimeout(t)
t = setTimeout(function(){/*do your stuff here*/},200);
这里t是全局变量-如果在测量屏幕尺寸之前旋转屏幕,则将clearTimeout清除。尝试以毫秒为单位播放时间(在我的代码200中)以优化所需的行为。