• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 图形变换
2
3用于对组件进行旋转、平移、缩放、矩阵变换等操作。
4
5> **说明:**
6>
7> 从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
8
9## rotate
10
11rotate(value: RotateOptions)
12
13设置组件旋转。
14
15**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。
16
17**系统能力:** SystemCapability.ArkUI.ArkUI.Full
18
19**参数:**
20
21| 参数名 | 类型                                    | 必填 | 说明                                                         |
22| ------ | --------------------------------------- | ---- | ------------------------------------------------------------ |
23| value  | [RotateOptions](#rotateoptions对象说明) | 是   | 可使组件在以组件左上角为坐标原点的坐标系中进行旋转(坐标系如下图所示)。其中,(x,&nbsp;y,&nbsp;z)指定一个矢量,作为旋转轴。<br/>旋转轴和旋转中心点都基于坐标系设定,组件发生位移时,坐标系不会随之移动。<br/>默认值:<br/>{<br/>x:&nbsp;0,<br/>y:&nbsp;0,<br/>z:&nbsp;0,<br/>centerX:&nbsp;'50%',<br/>centerY:&nbsp;'50%'<br/>centerZ:&nbsp;0,<br/>perspective:&nbsp;0<br/>}<br/>![coordinates](figures/coordinates.png)<br/>centerZ、perspective从API version 10开始支持在ArkTS卡片中使用。 |
24
25## translate
26
27translate(value: TranslateOptions)
28
29设置组件平移。
30
31**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。
32
33**系统能力:** SystemCapability.ArkUI.ArkUI.Full
34
35**参数:**
36
37| 参数名 | 类型                                          | 必填 | 说明                                                         |
38| ------ | --------------------------------------------- | ---- | ------------------------------------------------------------ |
39| value  | [TranslateOptions](#translateoptions对象说明) | 是   | 可使组件在以组件左上角为坐标原点的坐标系中进行移动(坐标系如下图所示)。其中,x,y,z的值分别表示在对应轴移动的距离,值为正时表示向对应轴的正向移动,值为负时表示向对应轴的反向移动。移动距离支持数字和字符串(比如'10px',‘10%’)两种类型。<br/>默认值:<br/>{<br/>x:&nbsp;0,<br/>y:&nbsp;0,<br/>z:&nbsp;0<br/>}<br/>![coordinates](figures/coordinates.png)<br/>**说明:**<br/>z轴方向移动时由于观察点位置不变,z的值接近观察点组件会有放大效果,远离则缩小。<br/>![coordinateNode](figures/coordinateNote.png) |
40
41## scale
42
43scale(value: ScaleOptions)
44
45设置组件缩放。
46
47**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。
48
49**系统能力:** SystemCapability.ArkUI.ArkUI.Full
50
51**参数:**
52
53| 参数名 | 类型                                  | 必填 | 说明                                                         |
54| ------ | ------------------------------------- | ---- | ------------------------------------------------------------ |
55| value  | [ScaleOptions](#scaleoptions对象说明) | 是   | 可以分别设置X轴、Y轴、Z轴的缩放比例,默认值为1,同时可以通过centerX和centerY设置缩放的中心点。<br/>默认值:<br/>{<br/>x:&nbsp;1,<br/>y:&nbsp;1,<br/>z:&nbsp;1,<br/>centerX:'50%',<br/>centerY:'50%'<br/>} |
56
57## transform
58
59transform(value: Matrix4Transit)
60
61设置组件的变换矩阵。
62
63**系统能力:** SystemCapability.ArkUI.ArkUI.Full
64
65**参数:**
66
67| 参数名 | 类型                                    | 必填 | 说明                     |
68| ------ | --------------------------------------- | ---- | ------------------------ |
69| value  | [Matrix4Transit](../js-apis-matrix4.md) | 是   | 设置当前组件的变换矩阵。 |
70
71## RotateOptions对象说明
72
73从API version 9开始,该接口支持在ArkTS卡片中使用。
74
75| 名称                      | 类型                       | 必填 | 说明                                                         |
76| ------------------------- | -------------------------- | ---- | ------------------------------------------------------------ |
77| x                         | number                     | 否   | 旋转轴向量x坐标。                                            |
78| y                         | number                     | 否   | 旋转轴向量y坐标。                                            |
79| z                         | number                     | 否   | 旋转轴向量z坐标。                                            |
80| angle                     | number&nbsp;\|&nbsp;string | 是   | 旋转角度。取值为正时相对于旋转轴方向顺时针转动,取值为负时相对于旋转轴方向逆时针转动。取值可为string类型,如'90deg'。 |
81| centerX                   | number&nbsp;\|&nbsp;string | 否   | 变换中心点x轴坐标。                                          |
82| centerY                   | number&nbsp;\|&nbsp;string | 否   | 变换中心点y轴坐标。                                          |
83| centerZ<sup>10+</sup>     | number                     | 否   | z轴锚点,即3D旋转中心点的z轴分量。                           |
84| perspective<sup>10+</sup> | number                     | 否   | 视距,即视点到z=0平面的距离。<br/>旋转轴和旋转中心点都基于坐标系设定,组件发生位移时,坐标系不会随之移动。 |
85
86## TranslateOptions对象说明
87
88从API version 9开始,该接口支持在ArkTS卡片中使用。
89
90| 名称 | 类型                       | 必填 | 说明            |
91| ---- | -------------------------- | ---- | --------------- |
92| x    | number&nbsp;\|&nbsp;string | 否   | x轴的平移距离。 |
93| y    | number&nbsp;\|&nbsp;string | 否   | y轴的平移距离。 |
94| z    | number&nbsp;\|&nbsp;string | 否   | z轴的平移距离。 |
95
96## ScaleOptions对象说明
97
98从API version 9开始,该接口支持在ArkTS卡片中使用。
99
100| 名称    | 类型                       | 必填 | 说明                                                         |
101| ------- | -------------------------- | ---- | ------------------------------------------------------------ |
102| x       | number                     | 否   | x轴的缩放倍数。x>1时以x轴方向放大,0<x<1时以x轴方向缩小,x<0时沿x轴反向并缩放。 |
103| y       | number                     | 否   | y轴的缩放倍数。y>1时以y轴方向放大,0<y<1时以y轴方向缩小,y<0时沿y轴反向并缩放。 |
104| z       | number                     | 否   | z轴的缩放倍数。z>1时以z轴方向放大,0<z<1时以z轴方向缩小,z<0时沿z轴反向并缩放。 |
105| centerX | number&nbsp;\|&nbsp;string | 否   | 变换中心点x轴坐标。                                          |
106| centerY | number&nbsp;\|&nbsp;string | 否   | 变换中心点y轴坐标。                                          |
107
108> **说明:**
109>
110> 当组件同时设置了rotate和scale属性时,centerX和centerY的取值会发生冲突,此时centerX和centerY的值以最后设定的属性的值为准。
111
112## 示例
113
114```ts
115// xxx.ets
116import matrix4 from '@ohos.matrix4'
117
118@Entry
119@Component
120struct TransformExample {
121  build() {
122    Column() {
123      Text('rotate').width('90%').fontColor(0xCCCCCC).padding(15).fontSize(14)
124      Row()
125        .rotate({
126          x: 0,
127          y: 0,
128          z: 1,
129          centerX: '50%',
130          centerY: '50%',
131          angle: 300
132        }) // 组件以矢量(0,0,1)为旋转轴,绕中心点顺时针旋转300度
133        .width(100).height(100).backgroundColor(0xAFEEEE)
134
135      Text('translate').width('90%').fontColor(0xCCCCCC).padding(10).fontSize(14)
136      Row()
137        .translate({ x: 100, y: 10 }) // x轴方向平移100,y轴方向平移10
138        .width(100).height(100).backgroundColor(0xAFEEEE).margin({ bottom: 10 })
139
140      Text('scale').width('90%').fontColor(0xCCCCCC).padding(15).fontSize(14)
141      Row()
142        .scale({ x: 2, y: 0.5 }) // 高度缩小一倍,宽度放大一倍,z轴在2D下无效果
143        .width(100).height(100).backgroundColor(0xAFEEEE)
144
145      Text('Matrix4').width('90%').fontColor(0xCCCCCC).padding(15).fontSize(14)
146      Row()
147        .width(100).height(100).backgroundColor(0xAFEEEE)
148        .transform(matrix4.identity().translate({ x: 50, y: 50 }).scale({ x: 1.5, y: 1 }).rotate({
149          x: 0,
150          y: 0,
151          z: 1,
152          angle: 60
153        }))
154    }.width('100%').margin({ top: 5 })
155  }
156}
157```
158
159![transform](figures/transform.PNG)