往期鸿蒙5.0全套实战文章必看:(文中附带全栈鸿蒙5.0学习资料)
Graphics
自定义节点相关属性定义的详细信息。
说明
本模块首批接口从API version 11开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
导入模块
import { DrawContext, Size, Offset, Position, Pivot, Scale, Translation, Matrix4, Rotation, Frame, LengthMetricsUnit } from "@kit.ArkUI";
Size
用于返回组件布局大小的宽和高。默认单位为vp,不同的接口使用Size类型时会再定义单位,以接口定义的单位为准。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
| 名称 | 类型 | 可读 | 可写 | 说明 |
|---|---|---|---|---|
| width | number | 是 | 是 | 组件大小的宽度。 单位:vp |
| height | number | 是 | 是 | 组件大小的高度。 单位:vp |
Position
type Position = Vector2
用于设置或返回组件的位置。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
| 类型 | 说明 |
|---|---|
| Vector2 | 包含x和y两个值的向量。 单位:vp |
PositionT12+
type PositionT<T> = Vector2T<T>
用于设置或返回组件的位置。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
| 类型 | 说明 |
|---|---|
| Vector2T<T> | 包含x和y两个值的向量。 单位:vp |
Frame
用于设置或返回组件的布局大小和位置。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
| 名称 | 类型 | 只读 | 可选 | 说明 |
|---|---|---|---|---|
| x | number | 是 | 是 | 水平方向位置。 单位:vp |
| y | number | 是 | 是 | 垂直方向位置。 单位:vp |
| width | number | 是 | 是 | 组件的宽度。 单位:vp |
| height | number | 是 | 是 | 组件的高度。 单位:vp |
Pivot
type Pivot = Vector2
用于设置组件的轴心坐标,轴心会作为组件的旋转/缩放中心点,影响旋转和缩放效果。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
| 类型 | 说明 |
|---|---|
| Vector2 | 轴心的x和y轴坐标。该参数为浮点数,默认值为0.5, 取值范围为[0.0, 1.0]。 |
Scale
type Scale = Vector2
用于设置组件的缩放比例。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
| 类型 | 说明 |
|---|---|
| Vector2 | x和y轴的缩放参数。该参数为浮点数,默认值为1.0。 |
Translation
type Translation = Vector2
用于设置组件的平移量。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
| 类型 | 说明 |
|---|---|
| Vector2 | x和y轴的平移量。 单位:px |
Rotation
type Rotation = Vector3
用于设置组件的旋转角度。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
| 类型 | 说明 |
|---|---|
| Vector3 | x、y、z轴方向的旋转角度。 单位:vp |
Offset
type Offset = Vector2
用于设置组件或效果的偏移。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
| 类型 | 说明 |
|---|---|
| Vector2 | x和y轴方向的偏移量。 单位:vp |
Matrix4
type Matrix4 = [number,number,number,number,number,number,number,number,number,number,number,number,number,number,number,number]
设置四阶矩阵。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
| 类型 | 说明 |
|---|---|
| [number,number,number,number, number,number,number,number, number,number,number,number, number,number,number,number] |
参数为长度为16(4*4)的number数组。 |
用于设置组件的变换信息,该类型为一个 4x4 矩阵,使用一个长度为16的number[]进行表示,例如:
const transform: Matrix4 = [
1, 0, 45, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
]
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
Vector2
用于表示包含x和y两个值的向量。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
| 名称 | 类型 | 只读 | 可选 | 说明 |
|---|---|---|---|---|
| x | number | 否 | 否 | 向量x轴方向的值。 |
| y | number | 否 | 否 | 向量y轴方向的值。 |
Vector3
用于表示包含x、y、z三个值的向量。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
| 名称 | 类型 | 只读 | 可选 | 说明 |
|---|---|---|---|---|
| x | number | 否 | 否 | x轴方向的旋转角度。 |
| y | number | 否 | 否 | y轴方向的旋转角度。 |
| z | number | 否 | 否 | z轴方向的旋转角度。 |
Vector2T<T>12+
用于表示T类型的包含x和y两个值的向量。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
| 名称 | 类型 | 只读 | 可选 | 说明 |
|---|---|---|---|---|
| x | T | 否 | 否 | 向量x轴方向的值。 |
| y | T | 否 | 否 | 向量y轴方向的值。 |
DrawContext
图形绘制上下文,提供绘制所需的画布宽度和高度。
size
get size(): Size
获取画布的宽度和高度。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
返回值:
| 类型 | 说明 |
|---|---|
| Size | 画布的宽度和高度。 |
sizeInPixel12+
get sizeInPixel(): Size
获取以px为单位的画布的宽度和高度。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
返回值:
| 类型 | 说明 |
|---|---|
| Size | 画布的宽度和高度,以px为单位。 |
canvas
get canvas(): drawing.Canvas
获取用于绘制的画布。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
返回值:
| 类型 | 说明 |
|---|---|
| drawing.Canvas | 用于绘制的画布。 |
示例:
import { RenderNode, FrameNode, NodeController, DrawContext } from "@kit.ArkUI";
class MyRenderNode extends RenderNode {
flag: boolean = false;
draw(context: DrawContext) {
const size = context.size;
const canvas = context.canvas;
const sizeInPixel = context.sizeInPixel;
}
}
const renderNode = new MyRenderNode();
renderNode.frame = { x: 0, y: 0, width: 100, height: 100 };
renderNode.backgroundColor = 0xffff0000;
class MyNodeController extends NodeController {
private rootNode: FrameNode | null = null;
makeNode(uiContext: UIContext): FrameNode | null {
this.rootNode = new FrameNode(uiContext);
const rootRenderNode = this.rootNode.getRenderNode();
if (rootRenderNode !== null) {
rootRenderNode.appendChild(renderNode);
}
return this.rootNode;
}
}
@Entry
@Component
struct Index {
private myNodeController: MyNodeController = new MyNodeController();
build() {
Row() {
NodeContainer(this.myNodeController)
}
}
}
Edges<T>12+
用于设置边框的属性。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
| 名称 | 类型 | 可读 | 可写 | 说明 |
|---|---|---|---|---|
| left | T | 是 | 是 | 左侧边框的属性。 |
| top | T | 是 | 是 | 顶部边框的属性。 |
| right | T | 是 | 是 | 右侧边框的属性。 |
| bottom | T | 是 | 是 | 底部边框的属性。 |
LengthUnit12+
长度属性单位枚举。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
| 名称 | 值 | 说明 |
|---|---|---|
| PX | 0 | 长度类型,用于描述以px像素单位为单位的长度。 |
| VP | 1 | 长度类型,用于描述以vp像素单位为单位的长度。 |
| FP | 2 | 长度类型,用于描述以fp像素单位为单位的长 |
2031




被折叠的 条评论
为什么被折叠?



