拉起相机功能,拍摄后返回,并使用windowClass.on('avoidAreaChange')监听安全避让区域的变化,此时监听到的底部导航栏安全避让区域高度变大,导致整体页面布局上抬。
问题代码示例参考如下:
import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; import { window } from '@kit.ArkUI'; const DOMAIN = 0x0000; export default class EntryAbility extends UIAbility { onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { try { this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); } catch (err) { hilog.error(DOMAIN, 'testTag', 'Failed to set colorMode. Cause: %{public}s', JSON.stringify(err)); } hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onCreate'); } onDestroy(): void { hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy'); } onWindowStageCreate(windowStage: window.WindowStage): void { let windowClass = windowStage.getMainWindowSync() // 1.初始化获取布局避让遮挡的区域 let type = window.AvoidAreaType.TYPE_SYSTEM let avoidArea = windowClass.getWindowAvoidArea(type) let topHeight = avoidArea.topRect.height let vpTopHeight = windowClass.getUIContext().px2vp(topHeight) console.info('avoidAreaChange init topHeight:', `${topHeight}`) AppStorage.setOrCreate('topRectHeight', vpTopHeight) let bottomHeight = avoidArea.bottomRect.height let vpBotHeight = windowClass.getUIContext().px2vp(bottomHeight) console.info('avoidAreaChange init bottomHeight:', `${bottomHeight}`) AppStorage.setOrCreate('bottomRectHeight', vpBotHeight) // 2.注册监听函数,动态获取避让区域数据 windowClass.on('avoidAreaChange', (data) => { if (data.type === window.AvoidAreaType.TYPE_SYSTEM) { let topRectHeight = data.area.topRect.height; console.info('avoidAreaChange topRectHeight:', `${topRectHeight}`) AppStorage.setOrCreate('topRectHeight', topRectHeight); } else if (data.type == window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR) { let bottomRectHeight = data.area.bottomRect.height; console.info('avoidAreaChange bottomRectHeight:', `${bottomRectHeight}`) AppStorage.setOrCreate('bottomRectHeight', bottomRectHeight); } else { console.info('avoidAreaChange other:') } }); } onWindowStageDestroy(): void { // Main window is destroyed,release UI related resources hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); } onForeground(): void { // Ability has brought to foreground hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground'); } onBackground(): void { // Ability has back to background hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground'); } }
I avoidAreaChange init topHeight:123 I avoidAreaChange init bottomHeight:0
I avoidAreaChange bottomRectHeight:0 I avoidAreaChange bottomRectHeight:91
初始化页面布局 | 拉起相机后返回的页面布局 |
|---|---|
|
|
ArkUI为开发者提供4种像素单位,采用vp为基准数据单位。因此为了UI组件布局统一,最好统一使用vp作为数据单位,而getWindowAvoidArea()和on('avoidAreaChange')获取到的窗口矩形区域Rect是以px为单位的,需要进行px2vp的单位转换。
获取避让区示例代码如下:
import { hilog } from '@kit.PerformanceAnalysisKit'; import { window } from '@kit.ArkUI'; import { UIAbility } from '@kit.AbilityKit'; const DOMAIN = 0x0000; export default class EntryAbility extends UIAbility { onWindowStageCreate(windowStage: window.WindowStage): void { windowStage.loadContent('pages/Index', (err) => { if (err.code) { hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err)); return; } let windowClass = windowStage.getMainWindowSync(); // 1.初始化获取布局避让遮挡的区域 let type = window.AvoidAreaType.TYPE_SYSTEM; let avoidArea = windowClass.getWindowAvoidArea(type); let topHeight = avoidArea.topRect.height; let vpTopHeight = windowClass.getUIContext().px2vp(topHeight); console.info('avoidAreaChange init topHeight:', `${topHeight}`); AppStorage.setOrCreate('topRectHeight', vpTopHeight); let bottomHeight = avoidArea.bottomRect.height; let vpBotHeight = windowClass.getUIContext().px2vp(bottomHeight); console.info('avoidAreaChange init bottomHeight:', `${bottomHeight}`); AppStorage.setOrCreate('bottomRectHeight', vpBotHeight); // 2.注册监听函数,动态获取避让区域数据 windowClass.on('avoidAreaChange', (data) => { if (data.type === window.AvoidAreaType.TYPE_SYSTEM) { let topRectHeight = windowClass.getUIContext().px2vp(data.area.topRect.height); // UI全局单位统一,添加px2vp的单位转换操作 console.info('avoidAreaChange topRectHeight:', `${topRectHeight}`); AppStorage.setOrCreate('topRectHeight', topRectHeight); } else if (data.type == window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR) { let bottomRectHeight = windowClass.getUIContext().px2vp(data.area.bottomRect.height); // UI全局单位统一,添加px2vp的单位转换操作 console.info('avoidAreaChange bottomRectHeight:', `${bottomRectHeight}`); AppStorage.setOrCreate('bottomRectHeight', bottomRectHeight); } else { console.info('avoidAreaChange other:'); } }); hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.'); }); } onWindowStageDestroy(): void { hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); } onForeground(): void { hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground'); } onBackground(): void { hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground'); } };
import { camera, cameraPicker as picker } from '@kit.CameraKit'; import { BusinessError } from '@kit.BasicServicesKit'; import { common } from '@kit.AbilityKit'; async function openCamera(context: Context) { try { let pickerProfile: picker.PickerProfile = { cameraPosition: camera.CameraPosition.CAMERA_POSITION_BACK }; await picker.pick(context, [picker.PickerMediaType.PHOTO, picker.PickerMediaType.VIDEO], pickerProfile); } catch (error) { let err = error as BusinessError; console.error(`the pick call failed. error code: ${err.code}`); } } @Entry @Component struct HomePage { message: string = 'Hello World'; @State topHeight: number = 0; @State bottomHeight: number = 0; aboutToAppear(): void { this.topHeight = AppStorage.get('topRectHeight') as number; this.bottomHeight = AppStorage.get('bottomRectHeight') as number; } build() { Column() { Text(this.message) .id('HomePageHelloWorld') .fontSize($r('app.float.page_text_font_size')) .fontWeight(FontWeight.Bold) .alignRules({ center: { anchor: '__container__', align: VerticalAlign.Center }, middle: { anchor: '__container__', align: HorizontalAlign.Center } }) .onClick(() => { let context = this.getUIContext().getHostContext() as Context as common.UIAbilityContext; // 调用系统相机 openCamera(context); }); Image($r('app.media.background')) .height('100%') .width('100%'); } .padding({ bottom: this.bottomHeight, top: this.topHeight }) .height('100%') .width('100%'); } }