openharmony 鸿蒙 js-apis-arkui-componentUtils

2025-06-12 浏览 (1)

@ohos.arkui.componentUtils (componentUtils)

The componentUtils module provides API for obtaining the coordinates and size of the drawing area of a component.

NOTE

The APIs of this module are supported since API version 10. Updates will be marked with a superscript to indicate their earliest API version.

The functionality of this module depends on UI context. This means that the APIs of this module cannot be used where the UI context is unclear. For details, see UIContext.

Modules to Import

import { componentUtils } from '@kit.ArkUI';

componentUtils.getRectangleById(deprecated)

getRectangleById(id: string): ComponentInfo

Obtains a ComponentInfo object based on the component ID and synchronously returns the geometric properties of the component.

NOTE

This API is deprecated since API version 18. You are advised to use getRectangleById instead on the obtained ComponentUtils object.

Since API version 10, you can use the getComponentUtils API in UIContext to obtain the ComponentUtils object associated with the current UI context. For this API to work correctly, call it after the notification indicating completion of component layout is received through @ohos.arkui.inspector (layout callback).

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.ArkUI.ArkUI.Full

Parameters

NameTypeMandatoryDescription
idstringYesComponent ID.

Return value

TypeDescription
ComponentInfoComponentInfo object, which provides the size, position, translation, scaling, rotation, and affine matrix information of the component.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
100001UI execution context not found.

Example

import { componentUtils } from '@kit.ArkUI';
let modePosition:componentUtils.ComponentInfo = componentUtils.getRectangleById("onClick");

ComponentInfo

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeMandatoryDescription
sizeSizeYesComponent size.
localOffsetOffsetYesOffset of the component relative to the parent component.
windowOffsetOffsetYesOffset of the component relative to the window.
screenOffsetOffsetYesOffset of the component relative to the screen.
translateTranslateResultYesTranslation of the component.
scaleScaleResultYesScaling of the component.
rotateRotateResultYesRotation of the component.
transformMatrix4ResultYesAffine matrix of the component, which is a 4x4 matrix object created based on the input parameter.

Size

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeMandatoryDescription
widthnumberYesComponent width.
Unit: px
heightnumberYesComponent height.
Unit: px

Offset

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeMandatoryDescription
xnumberYesX coordinate.
Unit: px
ynumberYesY coordinate.
Unit: px

TranslateResult

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeMandatoryDescription
xnumberYesTranslation distance along the x-axis.
Unit: px
ynumberYesTranslation distance along the y-axis.
Unit: px
znumberYesTranslation distance along the z-axis.
Unit: px

ScaleResult

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeMandatoryDescription
xnumberYesScale factor along the x-axis.
Unit: px
ynumberYesScale factor along the y-axis.
Unit: px
znumberYesScale factor along the z-axis.
Unit: px
centerXnumberYesX coordinate of the center point.
Unit: px
centerYnumberYesY coordinate of the center point.
Unit: px

RotateResult

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeMandatoryDescription
xnumberYesX coordinate of the rotation vector.
Unit: px
ynumberYesY coordinate of the rotation vector.
Unit: px
znumberYesZ coordinate of the rotation vector.
Unit: px
anglenumberYesRotation angle.
Unit: px
centerXnumberYesX coordinate of the center point.
Unit: px
centerYnumberYesY coordinate of the center point.
Unit: px

Matrix4Result

type Matrix4Result = [number,number,number,number,number,number,number,number,number,number,number,number,number,number,number,number]

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.ArkUI.ArkUI.Full

TypeDescription
[number,number,number,number,
number,number,number,number,
number,number,number,number,
number,number,number,number]
A number array whose length is 16 (4 x 4). For details, see 4 x 4 matrix description.
Unit: px

4 x 4 matrix description

NameTypeMandatoryDescription
m00numberYesScale factor along the x-axis. Defaults to 1 for the identity matrix.
m01numberYesThe second value, which is affected by the rotation of the x, y, and z axes.
m02numberYesThe third value, which is affected by the rotation of the x, y, and z axes.
m03numberYesMeaningless value.
m10numberYesThe fifth value, which is affected by the rotation of the x, y, and z axes.
m11numberYesScale factor along the y-axis. Defaults to 1 for the identity matrix.
m12numberYesThe seventh value, which is affected by the rotation of the x, y, and z axes.
m13numberYesMeaningless value.
m20numberYesThe ninth value, which is affected by the rotation of the x, y, and z axes.
m21numberYesThe tenth value, which is affected by the rotation of the x, y, and z axes.
m22numberYesScale factor along the z-axis. Defaults to 1 for the identity matrix.
m23numberYesMeaningless value.
m30numberYesTranslation value of the x-axis, in px. Defaults to 0 for the unit matrix.
m31numberYesTranslation value of the y-axis, in px. The default value is 0 for the identity matrix.
m32numberYesTranslation value of the z-axis, in px. The default value is 0 for the identity matrix.
m33numberYesValid in homogeneous coordinates, presenting the perspective projection effect.

Example

NOTE

You are advised to use the getComponentUtils API in UIContext to obtain the ComponentUtils object associated with the current UI context.

import { matrix4, componentUtils } from '@kit.ArkUI';

@Entry
@Component
struct Utils {
@State x: number = 120;
@State y: number = 10;
@State z: number = 100;
@State value: string = '';
private matrix1 = matrix4.identity().translate({ x: this.x, y: this.y, z: this.z });

build() {
  Column() {
    Image($r("app.media.img"))
      .transform(this.matrix1)
      .translate({ x: 20, y: 20, z: 20 })
      .scale({ x: 0.5, y: 0.5, z: 1 })
      .rotate({
        x: 1,
        y: 1,
        z: 1,
        centerX: '50%',
        centerY: '50%',
        angle: 300
      })
      .width(300)
      .height(100)
      .key("image_01")
    Button('getRectangleById')
    .onClick(() => {
      this.value = JSON.stringify(this.getUIContext().getComponentUtils().getRectangleById("image_01")) // You are advised to use this.getUIContext().getComponentUtils().
    }).margin(10).id('onClick')
    Text(this.value)
      .margin(20)
      .width(300)
      .height(300)
      .borderWidth(2)
  }.margin({left: 50})
}
}

componentget

你可能感兴趣的鸿蒙文章

harmony 鸿蒙ArkUI

harmony 鸿蒙ARKUI_TextPickerCascadeRangeContent

harmony 鸿蒙ARKUI_TextPickerRangeContent

harmony 鸿蒙ArkUI_AnimateCompleteCallback

harmony 鸿蒙ArkUI_AttributeItem

harmony 鸿蒙ArkUI_ColorStop

harmony 鸿蒙ArkUI_ContextCallback

harmony 鸿蒙ArkUI_EventModule

harmony 鸿蒙ArkUI_ExpectedFrameRateRange

harmony 鸿蒙ArkUI_IntOffset

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/HlruNL