HarmonyOS 怎样获取FrameNode含expandSafeArea的位置大小

使用getMeasuredSize和getPositionToWindow可以获取到FrameNode的大小和位置,但是不包含expandSafeArea扩展出来的区域,怎样才能获取到expandSafeArea扩展后的背景大小。

HarmonyOS
2024-12-26 14:32:35
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

可以通过getRectangleById获取组件大小。

参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-arkui-uicontext-V5#getrectanglebyid

参考demo:

// xxx.ets
import { window } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';
import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
@Entry
@Component
struct SafeAreaExample1 {
  @State text: string = ''
  controller: TextInputController = new TextInputController()
  getSafeArea(){
    console.info("进入点击事件")
    window.getLastWindow(getContext(this), (err: BusinessError, data) => {
      let windowClass: window.Window  = data;
      let topAvoidArea = windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM)
      let bottomAvoidArea = windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR)
      let bottomRectHeight = px2vp(bottomAvoidArea.bottomRect.height)
      let topRectHeight = px2vp(topAvoidArea.topRect.height)
      console.info("topAvoidArea:",bottomRectHeight)
      console.info("bottomAvoidArea:",topRectHeight)
    })
  }
  getAreaById(){
    let context = this.getUIContext()
    let componentUtils:ComponentUtils = context.getComponentUtils();
    let modePosition = componentUtils.getRectangleById("out");
    let localOffsetWidth = modePosition.size.width;
    let localOffsetHeight = modePosition.size.height;
    console.info("localOffsetWidth:", px2vp(localOffsetWidth))
    console.info("localOffsetHeight:",px2vp(localOffsetHeight))
  }
  build() {
    Row() {
      Column()
        .id('out')
        .height('100%').width('100%')
        .backgroundImage($r('app.media.background')).backgroundImageSize(ImageSize.Cover)
        .expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP,SafeAreaEdge.BOTTOM])
        .onAreaChange((old:Area,newArea:Area)=>{
          console.info("新的Area:",JSON.stringify(newArea))
        })
        .onClick(()=>{
          this.getAreaById()
        });
    }.height('100%')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
分享
微博
QQ
微信
回复
2024-12-26 17:25:29
相关问题
如何获取元素位置大小
2940浏览 • 1回复 待解决
ArkTS获取组件位置大小接口
4542浏览 • 1回复 待解决
HarmonyOS Progress 怎样自定义圆角大小
781浏览 • 1回复 待解决
HarmonyOS NavDestinationexpandSafeArea问题
759浏览 • 1回复 待解决
HarmonyOS 有没有FrameNode示例demo
792浏览 • 1回复 待解决
postgresql 如何插入 ‘ 数据?
4424浏览 • 2回复 待解决
HarmonyOS 如何获取组件位置
864浏览 • 1回复 待解决
HarmonyOS FrameNode手势检测crash
626浏览 • 1回复 待解决
HarmonyOS 如何获取设备显示大小
716浏览 • 1回复 待解决
HarmonyOS ArkUI获取元素坐标位置
759浏览 • 1回复 待解决
HarmonyOS 怎样获取系统音频延时
841浏览 • 1回复 待解决
HarmonyOS expandSafeArea不生效
1455浏览 • 1回复 待解决
HarmonyOS 获取设备存储空间大小
1417浏览 • 1回复 待解决
如何获取List实际大小
2696浏览 • 1回复 待解决