HarmonyOS 鸿蒙Next 添加折叠屏设备的折叠监听,折叠完成 需延迟(如50ms)才能获取当前状态的屏幕尺寸
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()方法获取屏幕尺寸
然后通过窗口的on('windowSizeChange')方法实现对窗口尺寸大小变化的监听:
参考:
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