鸿蒙获取组件的位置信息

       在鸿蒙应用开发中,经常需要获取组件的位置信息。下面我将分步骤介绍如何实现这一过程。

步骤一:设置组件ID

        首先,给需要获取位置信息的组件设置一个唯一的ID值。以Text组件为例,可以这样设置:

Text().id("text")

        这样,Text组件就有了一个独一无二的标识符“text”。

步骤二:获取组件的详细信息

        接下来,使用鸿蒙系统提供的getInspectorByKey(key)方法来获取组件的详细信息:

let strJson = getInspectorByKey("text");

        此方法返回的JSON字符串中,包含了组件的很多详细信息,比如组件的类型、大小、位置等。我们可以根据自己的需求,从这个JSON字符串中提取出所需要的内容。

步骤三:解析JSON字符串

        为了方便处理,我们将JSON字符串转换成JavaScript对象:

let obj = JSON.parse(strJson); // 将返回的JSON字符串解析为对象

let rectInfo = JSON.parse('[' + obj.$rect + ']');

步骤四:提取位置信息

        最后,我们就可以从转换后的对象中,提取出组件的位置信息了。具体的位置信息有left、top、right、bottom属性,分别表示组件的左边缘位置、上边缘位置、右边缘位置和下边缘位置。我们可以通过如下方式获取这些位置信息:

// 解析组件的四个边缘位置
rect.left = JSON.parse('[' + rectInfo[0] + ']')[0];
rect.top = JSON.parse('[' + rectInfo[0] + ']')[1];
rect.right = JSON.parse('[' + rectInfo[1] + ']')[0];
rect.bottom = JSON.parse('[' + rectInfo[1] + ']')[1];

总结        

        通过以上步骤,我们就可以在鸿蒙应用中精确地获取组件的位置信息了。这些信息对于后续的界面布局和交互设计都非常有用。希望这篇文章能对你的鸿蒙应用开发工作有所帮助!

完整代码如下:

页面类:        

import { ComponentRect } from '../common/ComponentRect';
import { Utils } from '../utils/Utils';

@Entry
@Component
struct PositionPage {
  @State message: string = 'Hello World'
  //Text控件的矩形位置
  textComponentRect: ComponentRect = new ComponentRect();
  @State left: number = 0;
  @State top: number = 0;
  @State right: number = 0;
  @State bottom: number = 0;

  build() {
    Row() {
      Column() {
        Text(this.message)
          .id("text")
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(()=>{
            this.textComponentRect = Utils.getComponentRect("text");
            this.left = this.textComponentRect.left;
            this.top = this.textComponentRect.top;
            this.right = this.textComponentRect.right;
            this.bottom = this.textComponentRect.bottom;
          })
        Text(`this.left = ${this.left}` )
        Text(`this.top = ${this.top}` )
        Text(`this.right = ${this.right}` )
        Text(`this.bottom = ${this.bottom}` )
      }
      .width('100%')
    }
    .height('100%')
  }
}

Utils类:

import { ComponentRect } from '../common/ComponentRect'

export class Utils {
  //获取组件所占矩形区域坐标
  static getComponentRect(key): ComponentRect {
    let rect: ComponentRect = new ComponentRect();
    let strJson = getInspectorByKey(key)
    let obj = JSON.parse(strJson)
    Utils.log("[getInspectorByKey] current component obj is: " + JSON.stringify(obj))
    let rectInfo = JSON.parse('[' + obj.$rect + ']')
    Utils.log("[getInspectorByKey] rectInfo is: " + rectInfo)
    rect.left = JSON.parse('[' + rectInfo[0] + ']')[0]
    rect.top = JSON.parse('[' + rectInfo[0] + ']')[1]
    rect.right = JSON.parse('[' + rectInfo[1] + ']')[0]
    rect.bottom = JSON.parse('[' + rectInfo[1] + ']')[1]
    return rect;
  }


  static log(content: string) {
    console.log("长度验证:", content.length)
    const maxSize = 1000
    if (content.length <= maxSize) {
      //直接打印
      console.log(content)
    } else {
      while (content.length > maxSize) {
        let logContent = content.substring(0, maxSize)
        //内容超长,分段打印
        console.log(logContent)
        content = content.replace(logContent, "")
      }
      console.log(content)
    }
  }
}

ComponentRect 类: 

export class ComponentRect {
  left: number = 0;
  top: number = 0;
  right: number = 0;
  bottom: number = 0;
}

  • 13
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值