• 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**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
18
19**系统能力:** SystemCapability.ArkUI.ArkUI.Full
20
21**参数:**
22
23| 参数名 | 类型                                    | 必填 | 说明                                                         |
24| ------ | --------------------------------------- | ---- | ------------------------------------------------------------ |
25| value  | [RotateOptions](#rotateoptions对象说明) | 是   | 可使组件在以组件左上角为坐标原点的坐标系中进行旋转(坐标系如下图所示)。其中,(x,&nbsp;y,&nbsp;z)指定一个矢量,作为旋转轴。<br/>旋转轴和旋转中心点都基于坐标系设定,组件发生位移时,坐标系不会随之移动。<br/>默认值: 在x、y、z都不指定时,x、y、z的默认值分别为0、0、1。指定了x、y、z任何一个值时,x、y、z中未指定的值默认为0。<br/>{<br/>centerX:&nbsp;'50%',<br/>centerY:&nbsp;'50%'<br/>centerZ:&nbsp;0,<br/>perspective:&nbsp;0<br/>}<br/>单位:vp<br/>![coordinates](figures/coordinates.png)<br/>centerZ、perspective从API version 10开始支持在ArkTS卡片中使用。 |
26
27## translate
28
29translate(value: TranslateOptions)
30
31设置组件平移。
32
33**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。
34
35**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
36
37**系统能力:** SystemCapability.ArkUI.ArkUI.Full
38
39**参数:**
40
41| 参数名 | 类型                                          | 必填 | 说明                                                         |
42| ------ | --------------------------------------------- | ---- | ------------------------------------------------------------ |
43| value  | [TranslateOptions](#translateoptions对象说明) | 是   | 可使组件在以组件左上角为坐标原点的坐标系中进行移动(坐标系如下图所示)。其中,x,y,z的值分别表示在对应轴移动的距离,值为正时表示向对应轴的正向移动,值为负时表示向对应轴的反向移动。移动距离支持数字和字符串(比如'10px',‘10%’)两种类型。<br/>默认值:<br/>{<br/>x:&nbsp;0,<br/>y:&nbsp;0,<br/>z:&nbsp;0<br/>}<br/>单位:vp<br/>![coordinates](figures/coordinates.png)<br/>**说明:**<br/>z轴方向移动时由于观察点位置不变,z的值接近观察点组件会有放大效果,远离则缩小。<br/>![coordinateNode](figures/coordinateNote.png) |
44
45## scale
46
47scale(value: ScaleOptions)
48
49设置组件缩放。
50
51**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。
52
53**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
54
55**系统能力:** SystemCapability.ArkUI.ArkUI.Full
56
57**参数:**
58
59| 参数名 | 类型                                  | 必填 | 说明                                                         |
60| ------ | ------------------------------------- | ---- | ------------------------------------------------------------ |
61| 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/>} |
62
63## transform
64
65transform(value: object)
66
67设置组件的变换矩阵。
68
69**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
70
71**系统能力:** SystemCapability.ArkUI.ArkUI.Full
72
73**参数:**
74
75| 参数名 | 类型                                    | 必填 | 说明                     |
76| ------ | --------------------------------------- | ---- | ------------------------ |
77| value  | object | 是   | 设置当前组件的变换矩阵。object当前仅支持[Matrix4Transit](../js-apis-matrix4.md)矩阵对象类型。 |
78
79## RotateOptions对象说明
80
81**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。
82
83**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
84
85**系统能力:** SystemCapability.ArkUI.ArkUI.Full
86
87| 名称                      | 类型                       | 必填 | 说明                                                         |
88| ------------------------- | -------------------------- | ---- | ------------------------------------------------------------ |
89| x                         | number                     | 否   | 旋转轴向量x坐标。                                            |
90| y                         | number                     | 否   | 旋转轴向量y坐标。                                            |
91| z                         | number                     | 否   | 旋转轴向量z坐标。                                            |
92| angle                     | number&nbsp;\|&nbsp;string | 是   | 旋转角度。取值为正时相对于旋转轴方向顺时针转动,取值为负时相对于旋转轴方向逆时针转动。取值可为string类型,如'90deg'。 |
93| centerX                   | number&nbsp;\|&nbsp;string | 否   | 变换中心点x轴坐标。                                          |
94| centerY                   | number&nbsp;\|&nbsp;string | 否   | 变换中心点y轴坐标。                                          |
95| centerZ<sup>10+</sup>     | number                     | 否   | z轴锚点,即3D旋转中心点的z轴分量。                           |
96| perspective<sup>10+</sup> | number                     | 否   | 视距,即视点到z=0平面的距离。<br/>旋转轴和旋转中心点都基于坐标系设定,组件发生位移时,坐标系不会随之移动。 |
97
98## TranslateOptions对象说明
99
100**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。
101
102**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
103
104**系统能力:** SystemCapability.ArkUI.ArkUI.Full
105
106| 名称 | 类型                       | 必填 | 说明            |
107| ---- | -------------------------- | ---- | --------------- |
108| x    | number&nbsp;\|&nbsp;string | 否   | x轴的平移距离。 |
109| y    | number&nbsp;\|&nbsp;string | 否   | y轴的平移距离。 |
110| z    | number&nbsp;\|&nbsp;string | 否   | z轴的平移距离。 |
111
112## ScaleOptions对象说明
113
114**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。
115
116**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
117
118**系统能力:** SystemCapability.ArkUI.ArkUI.Full
119
120| 名称    | 类型                       | 必填 | 说明                                                         |
121| ------- | -------------------------- | ---- | ------------------------------------------------------------ |
122| x       | number                     | 否   | x轴的缩放倍数。x>1时以x轴方向放大,0<x<1时以x轴方向缩小,x<0时沿x轴反向并缩放。 |
123| y       | number                     | 否   | y轴的缩放倍数。y>1时以y轴方向放大,0<y<1时以y轴方向缩小,y<0时沿y轴反向并缩放。 |
124| z       | number                     | 否   | z轴的缩放倍数。z>1时以z轴方向放大,0<z<1时以z轴方向缩小,z<0时沿z轴反向并缩放。 |
125| centerX | number&nbsp;\|&nbsp;string | 否   | 变换中心点x轴坐标。<br/>单位:vp|
126| centerY | number&nbsp;\|&nbsp;string | 否   | 变换中心点y轴坐标。<br/>单位:vp|
127
128> **说明:**
129>
130> 当组件同时设置了rotate和scale属性时,centerX和centerY的取值会发生冲突,此时centerX和centerY的值以最后设定的属性的值为准。
131
132## 示例
133
134### 示例1(为组件添加图形变换效果)
135
136该示例通过rotate、translate、scale、transform为组件添加旋转、平移、缩放、变换矩阵效果。
137
138```ts
139// xxx.ets
140import { matrix4 } from '@kit.ArkUI';
141
142@Entry
143@Component
144struct TransformExample {
145  build() {
146    Column() {
147      Text('rotate').width('90%').fontColor(0xCCCCCC).padding(15).fontSize(14)
148      Row()
149        .rotate({
150          x: 0,
151          y: 0,
152          z: 1,
153          centerX: '50%',
154          centerY: '50%',
155          angle: 300
156        }) // 组件以矢量(0,0,1)为旋转轴,绕中心点顺时针旋转300度
157        .width(100).height(100).backgroundColor(0xAFEEEE)
158
159      Text('translate').width('90%').fontColor(0xCCCCCC).padding(10).fontSize(14)
160      Row()
161        .translate({ x: 100, y: 10 }) // x轴方向平移100,y轴方向平移10
162        .width(100).height(100).backgroundColor(0xAFEEEE).margin({ bottom: 10 })
163
164      Text('scale').width('90%').fontColor(0xCCCCCC).padding(15).fontSize(14)
165      Row()
166        .scale({ x: 2, y: 0.5 }) // 高度缩小一倍,宽度放大一倍,z轴在2D下无效果
167        .width(100).height(100).backgroundColor(0xAFEEEE)
168
169      Text('Matrix4').width('90%').fontColor(0xCCCCCC).padding(15).fontSize(14)
170      Row()
171        .width(100).height(100).backgroundColor(0xAFEEEE)
172        .transform(matrix4.identity().translate({ x: 50, y: 50 }).scale({ x: 1.5, y: 1 }).rotate({
173          x: 0,
174          y: 0,
175          z: 1,
176          angle: 60
177        }))
178    }.width('100%').margin({ top: 5 })
179  }
180}
181```
182
183![transform](figures/transform.PNG)
184
185### 示例2(设置旋转视距)
186
187该示例通过perspective为组件添加视距效果。
188
189```ts
190// xxx.ets
191@Entry
192@Component
193struct Index {
194  @State prep: number = 10;
195
196  build() {
197    Row() {
198      Column() {
199        Stack()
200          .width(100)
201          .height(100)
202          .backgroundColor(Color.Red)
203          .rotate({ y: 1, angle: 45, perspective: this.prep })
204        Button('change prep')
205          .margin({ top: 100 })
206          .onClick(() => {
207            animateTo({
208              duration: 2000,
209              curve: Curve.EaseIn,
210              iterations: 1,
211              playMode: PlayMode.Normal,
212              onFinish: () => {
213                console.info('play end')
214              }
215            }, () => {
216              this.prep = 500 // 组件视距从10变换到500
217            })
218          })
219      }
220      .width('100%')
221    }
222    .height('100%')
223  }
224}
225```
226
227![perspective](figures/perspective.gif)