HarmonyOS 鸿蒙Next 添加折叠屏设备的折叠监听,折叠完成 需延迟(如50ms)才能获取当前状态的屏幕尺寸

发布于 1周前 作者 sinazl 最后一次编辑是 5天前 来自 鸿蒙OS

HarmonyOS 鸿蒙Next 添加折叠屏设备的折叠监听,折叠完成(回调FOLD_STATUS_FOLDED |FOLD_STATUS_EXPANDED)后立即获取屏幕size仍是折叠状态变更前的尺寸,需延迟(如50ms)才能获取当前状态的屏幕尺寸?

如题:添加折叠屏设备的折叠监听,折叠完成(回调FOLD_STATUS_FOLDED |FOLD_STATUS_EXPANDED)后立即获取屏幕size仍是折叠状态变更前的尺寸,需延迟(如50ms)才能获取当前状态的屏幕尺寸?


更多关于HarmonyOS 鸿蒙Next 添加折叠屏设备的折叠监听,折叠完成 需延迟(如50ms)才能获取当前状态的屏幕尺寸的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

刚打开应用时使用display.getDefaultDisplaySync()方法获取屏幕尺寸

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-display-V5#displaygetdefaultdisplaysync9

然后通过窗口的on('windowSizeChange')方法实现对窗口尺寸大小变化的监听:

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-window-V5#onwindowsizechange7

参考:

import { AbilityConstant, UIAbility, Want } from '[@kit](/user/kit).AbilityKit';
import { hilog } from '[@kit](/user/kit).PerformanceAnalysisKit';
import { window } from '[@kit](/user/kit).ArkUI';
import { BusinessError } from '[@kit](/user/kit).BasicServicesKit';

export default class EntryAbility extends UIAbility {
 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
   hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
 }

 onDestroy(): void {
   hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
 }

 onWindowStageCreate(windowStage: window.WindowStage): void {
   // Main window is created, set main page for this ability
   hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
   let windowClass: window.Window | undefined = undefined;
   try {
     window.getLastWindow(this.context, (err: BusinessError, data) => {
       const errCode: number = err.code;
       if (errCode) {
         console.error('Failed to obtain the top window. Cause: ' + JSON.stringify(err));
         return;
       }
       windowClass = data;
       try {
         windowClass.on('windowSizeChange', (data) => {
           AppStorage.setOrCreate('width', data.width);
           AppStorage.setOrCreate('height', data.height);
         });
       } catch (exception) {
         console.error('Failed to enable the listener for window size changes. Cause: ' + JSON.stringify(exception));
       }
       console.info('Succeeded in obtaining the top window. Data: ' + JSON.stringify(data));
     });
   } catch (exception) {
     console.error('Failed to obtain the top window. Cause: ' + JSON.stringify(exception));
   }
   windowStage.loadContent('pages/Index', (err) => {
     if (err.code) {
       hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
       return;
     }
     hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.');
   });
 }

 onWindowStageDestroy(): void {
   // Main window is destroyed, release UI related resources
   hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
 }

 onForeground(): void {
   // Ability has brought to foreground
   hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
 }

 onBackground(): void {
   // Ability has back to background
   hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
 }
}

import display from '[@ohos](/user/ohos).display';
[@Entry](/user/Entry)
[@Component](/user/Component)
struct Index {
 [@StorageLink](/user/StorageLink)('width') width1: number = 0;
 [@StorageLink](/user/StorageLink)('height') height1: number = 0;

 aboutToAppear(): void {
   let displayClass: display.Display | null = null;
   displayClass = display.getDefaultDisplaySync();
   this.width1 = displayClass.width;
   this.height1 = displayClass.height;
 }

 build() {
   Column() {
     Text(this.width1.toString())
       .fontSize(50)
       .fontWeight(FontWeight.Bold)
     Text(this.height1.toString())
       .fontSize(50)
       .fontWeight(FontWeight.Bold)
   }
   .height('100%')
   .width('100%')
 }
}

更多关于HarmonyOS 鸿蒙Next 添加折叠屏设备的折叠监听,折叠完成 需延迟(如50ms)才能获取当前状态的屏幕尺寸的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,添加折叠屏设备的折叠监听时,如果在折叠完成(回调FOLD_STATUS_FOLDEDFOLD_STATUS_EXPANDED)后立即获取屏幕尺寸,可能会遇到获取到的尺寸仍是折叠状态变更前的尺寸。这通常是由于系统状态更新与UI刷新存在一定的异步性。

为了处理这一问题,可以考虑在折叠状态变更的回调中,通过设置一个延时(如50ms)来获取最新的屏幕尺寸。延时可以通过使用定时器或异步任务来实现,确保在UI状态完全更新后再进行尺寸获取。

示例代码(伪代码):

function onFoldStatusChange(status) {
    if (status === FOLD_STATUS_FOLDED || status === FOLD_STATUS_EXPANDED) {
        setTimeout(() => {
            // 获取当前屏幕尺寸
            let screenSize = getCurrentScreenSize();
            console.log(screenSize);
        }, 50);
    }
}

上述方法通过延时确保屏幕尺寸的获取在折叠状态更新后完成。注意,延时时间可能需要根据实际设备性能和应用场景进行调整。

如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html

回到顶部