HarmonyOS 怎么获取某一个视图的起始位置?

HarmonyOS 怎么获取某一个视图的起始位置?

HarmonyOS
2024-10-15 12:43:22
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

通过给组件设置id,根据id获取位置信息: componentUtils.getRectangleById(对应Id)。

import componentUtils from '@ohos.arkui.componentUtils';  
@Entry  
@Component  
struct Page1 {  
  build() {  
    Row() {  
      Column() {  
        Text('Hello World')  
          .fontSize(50)  
          .fontWeight(FontWeight.Bold)  
          .id('myText')  
          .backgroundColor(Color.Yellow)  
          .onClick(()=>{  
            let modePosition: componentUtils.ComponentInfo = componentUtils.getRectangleById("myText");  
            console.log('modePosition' + JSON.stringify(modePosition));  
          })  
      }  
      .width('100%')  
    }  
    .height('100%')  
  }  
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.

输出日志:

modePosition{"size":{"width":910,"height":197},"localOffset":{"x":175,"y":0},"windowOffset":{"x":175,"y":1279},"screenOffset":{"x":175,"y":1279},"translate":{"x":0,"y":0,"z":0},"scale":{"x":1,"y":1,"z":1,"centerX":14000,"centerY":3030.769230769231},"rotate":{"x":0,"y":0,"z":0,"angle":0,"centerX":14000,"centerY":3030.769230769231},"transform":[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1]}
  • 1.

对应值为px,可用px2vp()转成需要的值。

分享
微博
QQ
微信
回复
2024-10-15 15:59:15
相关问题
Scroll中点击某一个层图片移动到顶端
1405浏览 • 1回复 待解决
HarmonyOS 实现一个全局浮动视图
616浏览 • 1回复 待解决
HarmonyOS 如何返回路由一个视图
1094浏览 • 1回复 待解决