【HarmonyOS NEXT】在Component中通过px2vp转换的数据前后不一致

文章讨论了在使用Ohos框架的UIAbility组件时,状态栏高度通过px2vp转换出现不一致的问题。解决方法是在windowStage.loadContent接口的回调中进行初始化,确保在初始化ArkUI后获得设备特定的转换比率。
摘要由CSDN通过智能技术生成

【关键字】

px2vp像素单位转换 / @ohos.app.ability.UIAbility组件 / @ohos.window (窗口)模块 / windowStage.loadContent

【问题描述】

通过getWindowAvoidArea.topRect.heigth获取到状态栏的高度为126,这个高度设置为组件的margintop,在工具类里面通过px2vp 126 = 126, 而在component中转换后得到的值却不一样,是什么原因?

screenutils(hsp): 在entryablity中的onWindowCreate中初始化:

import window from '@ohos.window';
import { LogUtils } from './LogUtils';
import { DS_COMPONENTS_TAG } from '../DSComponentsContants';

export class ScreenUtils {
private static TAG: string = `${DS_COMPONENTS_TAG}-${ScreenUtils.name}`
private static _statusBarHeight: number = 0
private static _navigationBarHeight: number = 0
private static _screenWidth: number = 0
private static _screenHeight: number = 0
private static _contentHeight: number = 0
private static _contentWidth: number = 0

static get statusBarHeight(): number {
return ScreenUtils._statusBarHeight
}

static get navigationBarHeight(): number {
return ScreenUtils._navigationBarHeight
}

static get screenWidth(): number {
return ScreenUtils._screenWidth
}

static get screenHeight(): number {
return ScreenUtils._screenHeight
}

static get contentHeight(): number {
return ScreenUtils._contentHeight
}

static get contentWidth(): number {
return ScreenUtils._contentWidth
}

static async init(windowStage: window.WindowStage): Promise<boolean> {
let windowClass: window.Window | null = await windowStage.getMainWindowSync()
let type = window.AvoidAreaType.TYPE_SYSTEM;
try {
let area = windowClass.getWindowAvoidArea(type);
ScreenUtils._statusBarHeight = px2vp(area.topRect.height)
ScreenUtils._navigationBarHeight = px2vp(area.bottomRect.height)
ScreenUtils._screenWidth = px2vp(area.topRect.width)
ScreenUtils._contentWidth = px2vp(area.topRect.width)
ScreenUtils._screenHeight = px2vp(area.bottomRect.height + area.bottomRect.top)
ScreenUtils._contentHeight = px2vp(area.bottomRect.top - ScreenUtils._statusBarHeight)
if (area.bottomRect.height == 0) {
ScreenUtils._screenHeight = px2vp(windowClass.getWindowProperties().windowRect.height)
ScreenUtils._contentHeight = px2vp(windowClass.getWindowProperties().windowRect.height)
}
}

【解决方案】

px2vp是ArkUI的接口。初始化ArkUI之前,转化比例是1,只有当初始化完成后,这个值才会根据设备得到具体的值。初始化ArkUI可以使用windowStage.loadContent接口,在onWindowStageCreate的windowStage.loadContent的回调中使用px2vp即可。

@ohos.app.ability.UIAbility接口文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-app-ability-uiability-0000001774120870#ZH-CN_TOPIC_0000001774120870__uiabilityonwindowstagecreate

windowStage.loadContent接口文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-window-0000001820880785#ZH-CN_TOPIC_0000001820880785__loadcontent9-2

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值