HarmonyOS鸿蒙Next中折叠屏设备折叠时如何解决获取到屏幕尺寸延迟的问题
HarmonyOS鸿蒙Next中折叠屏设备折叠时如何解决获取到屏幕尺寸延迟的问题
【问题现象】
- 添加折叠屏设备的折叠监听,折叠完成(回调FOLD_STATUS_FOLDED|FOLD_STATUS_EXPANDED)后立即获取屏幕size仍是折叠状态变更前的尺寸,需延迟(如50ms)才能获取当前状态的屏幕尺寸。
FolderStack({
}.onFolderStateChange((msg) => {
this.status = msg.foldStatus.toString();
display.getAllDisplays((err, data) => {
let screenWidth: number = data[0].width
let screenHeight: number = data[0].height
this.width1 = 'width = ' + screenWidth;
this.height1 = 'height = ' + screenHeight;
console.log('width = ' + screenWidth + ', height = ' + screenHeight)
})
})
【背景知识】
- 折叠屏悬停能力相关API:FolderStack继承于Stack(层叠布局)控件,新增了折叠屏悬停能力。
- getAllDisplays():获取当前所有的display对象,使用callback异步回调。
- getDefaultDisplaySync():获取当前默认的display对象。
【定位思路】
基于现象和问题代码,再查阅相关API可以发现,问题代码调用了异步获取屏幕尺寸的方法,所以获取屏幕尺寸存在时延。
【解决方案】
HarmonyOS有提供同步获取屏幕尺寸的方法getDefaultDisplaySync(): Display。
代码示例如下:
FolderStack({
}.onFolderStateChange((msg) => {
let displayClass: display.Display | null = null;
displayClass = display.getDefaultDisplaySync(); // 修改为同步方法
this.width1 = displayClass.width;
this.height1 = displayClass.height;
}))
更多关于HarmonyOS鸿蒙Next中折叠屏设备折叠时如何解决获取到屏幕尺寸延迟的问题的实战教程也可以访问 https://www.itying.com/category-93-b0.html
1 回复
更多关于HarmonyOS鸿蒙Next中折叠屏设备折叠时如何解决获取到屏幕尺寸延迟的问题的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
使用 getDefaultDisplaySync()
方法同步获取屏幕尺寸,避免异步回调导致的延迟问题。