• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.matrix4 (矩阵变换)
2
3本模块提供矩阵变换功能,可对图形进行平移、旋转和缩放等。
4
5> **说明:**
6>
7> 本模块首批接口从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
8
9
10## 导入模块
11
12```ts
13import matrix4 from '@ohos.matrix4'
14```
15
16
17## matrix4.init
18
19init(options: [number,number,number,number,number,number,number,number,number,number,number,number,number,number,number,number]): Matrix4Transit
20
21
22Matrix的构造函数,可以通过传入的参数创建一个四阶矩阵,矩阵为列优先。
23
24**系统能力:**  SystemCapability.ArkUI.ArkUI.Full
25
26**参数:**
27
28| 参数名 | 类型                                                         | 必填 | 说明                                                         |
29| ------ | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
30| option | [number,number,number,number,<br/>number,number,number,number,<br/>number,number,number,number,<br/>number,number,number,number] | 是   | 参数为长度为16(4\*4)的number数组,&nbsp;详情见四阶矩阵说明。<br/>默认值:<br/>[1,&nbsp;0,&nbsp;0,&nbsp;0,<br/>0,&nbsp;1,&nbsp;0,&nbsp;0,<br/>0,&nbsp;0,&nbsp;1,&nbsp;0,<br/>0,&nbsp;0,&nbsp;0,&nbsp;1] |
31
32**返回值:**
33
34| 类型                              | 说明                         |
35| --------------------------------- | ---------------------------- |
36| [Matrix4Transit](#matrix4transit) | 根据入参创建的四阶矩阵对象。 |
37
38**四阶矩阵说明:**
39
40| 参数名  | 类型     | 必填   | 说明                   |
41| ---- | ------ | ---- | -------------------- |
42| m00  | number | 是    | x轴缩放值,单位矩阵默认为1。      |
43| m01  | number | 是    | 第2个值,xyz轴旋转会影响这个值。   |
44| m02  | number | 是    | 第3个值,xyz轴旋转会影响这个值。   |
45| m03  | number | 是    | 无实际意义。               |
46| m10  | number | 是    | 第5个值,xyz轴旋转会影响这个值。   |
47| m11  | number | 是    | y轴缩放值,单位矩阵默认为1。      |
48| m12  | number | 是    | 第7个值,xyz轴旋转会影响这个值。   |
49| m13  | number | 是    | 无实际意义。               |
50| m20  | number | 是    | 第9个值,xyz轴旋转会影响这个值。   |
51| m21  | number | 是    | 第10个值,xyz轴旋转会影响这个值。  |
52| m22  | number | 是    | z轴缩放值,单位矩阵默认为1。      |
53| m23  | number | 是    | 无实际意义。               |
54| m30  | number | 是    | x轴平移值,单位px,单位矩阵默认为0。 |
55| m31  | number | 是    | y轴平移值,单位px,单位矩阵默认为0。 |
56| m32  | number | 是    | z轴平移值,单位px,单位矩阵默认为0。 |
57| m33  | number | 是    | 齐次坐标下生效,产生透视投影效果。    |
58
59**示例**
60
61```ts
62import matrix4 from '@ohos.matrix4'
63// 创建一个四阶矩阵
64let matrix = matrix4.init([1.0, 0.0, 0.0, 0.0,
65                          0.0, 1.0, 0.0, 0.0,
66                          0.0, 0.0, 1.0, 0.0,
67                          0.0, 0.0, 0.0, 1.0])
68@Entry
69@Component
70struct Tests {
71  build() {
72    Column() {
73      Image($r("app.media.zh"))
74        .width("40%")
75        .height(100)
76        .transform(matrix)
77    }
78  }
79}
80```
81
82
83## matrix4.identity
84
85identity(): Matrix4Transit
86
87
88Matrix的初始化函数,可以返回一个单位矩阵对象。
89
90**系统能力:**  SystemCapability.ArkUI.ArkUI.Full
91
92**返回值:**
93
94| 类型                              | 说明           |
95| --------------------------------- | -------------- |
96| [Matrix4Transit](#matrix4transit) | 单位矩阵对象。 |
97
98**示例:**
99
100```ts
101// matrix1 和 matrix2 效果一致
102import matrix4 from '@ohos.matrix4'
103let matrix1 = matrix4.init([1.0, 0.0, 0.0, 0.0,
104                          0.0, 1.0, 0.0, 0.0,
105                          0.0, 0.0, 1.0, 0.0,
106                          0.0, 0.0, 0.0, 1.0])
107let matrix2 = matrix4.identity()
108@Entry
109@Component
110struct Tests {
111  build() {
112    Column() {
113      Image($r("app.media.zh"))
114        .width("40%")
115        .height(100)
116        .transform(matrix1)
117      Image($r("app.media.zh"))
118        .width("40%")
119        .height(100)
120        .margin({ top: 150 })
121        .transform(matrix2)
122    }
123  }
124}
125```
126
127
128## matrix4.copy
129
130copy(): Matrix4Transit
131
132
133Matrix的拷贝函数,可以拷贝一份当前的矩阵对象。
134
135**系统能力:**  SystemCapability.ArkUI.ArkUI.Full
136
137**返回值:**
138
139| 类型                              | 说明                 |
140| --------------------------------- | -------------------- |
141| [Matrix4Transit](#matrix4transit) | 当前矩阵的拷贝对象。 |
142
143**示例:**
144
145```ts
146// xxx.ets
147import matrix4 from '@ohos.matrix4'
148
149@Entry
150@Component
151struct Test {
152  private matrix1 = matrix4.identity().translate({ x: 100 })
153  // 对matrix1的拷贝矩阵做scale操作,不影响到matrix1
154  private matrix2 = this.matrix1.copy().scale({ x: 2 })
155
156  build() {
157    Column() {
158      Image($r("app.media.bg1"))
159        .width("40%")
160        .height(100)
161        .transform(this.matrix1)
162      Image($r("app.media.bg2"))
163        .width("40%")
164        .height(100)
165        .margin({ top: 50 })
166        .transform(this.matrix2)
167    }
168  }
169}
170```
171
172![zh-cn_image_0000001219744181](figures/zh-cn_image_0000001219744181.png)
173
174## matrix4.invert<sup>(deprecated)</sup>
175
176invert(): Matrix4Transit
177
178Matrix的逆函数,可以返回一个当前矩阵对象的逆矩阵,即效果正好相反。
179
180该接口从Api 10开始废弃。
181
182**系统能力:**  SystemCapability.ArkUI.ArkUI.Full
183
184**返回值:**
185
186| 类型                              | 说明                   |
187| --------------------------------- | ---------------------- |
188| [Matrix4Transit](#matrix4transit) | 当前矩阵的逆矩阵对象。 |
189
190## matrix4.combine<sup>(deprecated)</sup>
191
192combine(options: Matrix4Transit): Matrix4Transit
193
194Matrix的叠加函数,可以将两个矩阵的效果叠加起来生成一个新的矩阵对象。
195
196该接口从Api 10开始废弃。
197
198**系统能力:**  SystemCapability.ArkUI.ArkUI.Full
199
200**参数:**
201
202| 参数名 | 类型                              | 必填 | 说明               |
203| ------ | --------------------------------- | ---- | ------------------ |
204| option | [Matrix4Transit](#matrix4transit) | 是   | 待叠加的矩阵对象。 |
205
206**返回值:**
207
208| 类型                              | 说明                   |
209| --------------------------------- | ---------------------- |
210| [Matrix4Transit](#matrix4transit) | 当前矩阵的逆矩阵对象。 |
211
212## matrix4.translate<sup>(deprecated)</sup>
213
214translate(options: TranslateOption): Matrix4Transit
215
216Matrix的平移函数,可以为当前矩阵增加x轴/y轴/z轴平移效果。
217
218该接口从Api 10开始废弃。
219
220**系统能力:**  SystemCapability.ArkUI.ArkUI.Full
221
222**参数:**
223
224| 参数名 | 类型                                | 必填 | 说明           |
225| ------ | ----------------------------------- | ---- | -------------- |
226| option | [TranslateOption](#translateoption) | 是   | 设置平移参数。 |
227
228**返回值:**
229
230| 类型                              | 说明                   |
231| --------------------------------- | ---------------------- |
232| [Matrix4Transit](#matrix4transit) | 当前矩阵的逆矩阵对象。 |
233
234## matrix4.scale<sup>(deprecated)</sup>
235
236scale(options: ScaleOption): Matrix4Transit
237
238Matrix的缩放函数,可以为当前矩阵增加x轴/y轴/z轴缩放效果。
239
240该接口从Api 10开始废弃。
241
242**系统能力:**  SystemCapability.ArkUI.ArkUI.Full
243
244**参数:**
245
246| 参数名 | 类型                        | 必填 | 说明           |
247| ------ | --------------------------- | ---- | -------------- |
248| option | [ScaleOption](#scaleoption) | 是   | 设置缩放参数。 |
249
250**返回值:**
251
252| 类型                              | 说明                   |
253| --------------------------------- | ---------------------- |
254| [Matrix4Transit](#matrix4transit) | 当前矩阵的逆矩阵对象。 |
255
256## matrix4.rotate<sup>(deprecated)</sup>
257
258rotate(options: RotateOption): Matrix4Transit
259
260Matrix的旋转函数,可以为当前矩阵增加x轴/y轴/z轴旋转效果。
261
262该接口从Api 10开始废弃。
263
264**系统能力:**  SystemCapability.ArkUI.ArkUI.Full
265
266**参数:**
267
268| 参数名 | 类型                          | 必填 | 说明           |
269| ------ | ----------------------------- | ---- | -------------- |
270| option | [RotateOption](#rotateoption) | 是   | 设置旋转参数。 |
271
272**返回值:**
273
274| 类型                              | 说明                   |
275| --------------------------------- | ---------------------- |
276| [Matrix4Transit](#matrix4transit) | 当前矩阵的逆矩阵对象。 |
277
278## matrix4.transformPoint<sup>(deprecated)</sup>
279
280transformPoint(options: [number, number]): [number, number]
281
282Matrix的坐标点转换函数,可以将当前的变换效果作用到一个坐标点上。
283
284该接口从Api 10开始废弃。
285
286**系统能力:**  SystemCapability.ArkUI.ArkUI.Full
287
288**参数:**
289
290| 参数名 | 类型             | 必填 | 说明               |
291| ------ | ---------------- | ---- | ------------------ |
292| option | [number, number] | 是   | 需要转换的坐标点。 |
293
294**返回值:**
295
296| 类型             | 说明                        |
297| ---------------- | --------------------------- |
298| [number, number] | 返回矩阵变换后的Point对象。 |
299
300
301## Matrix4Transit
302
303
304### combine
305
306combine(options: Matrix4Transit): Matrix4Transit
307
308
309Matrix的叠加函数,可以将两个矩阵的效果叠加起来生成一个新的矩阵对象。会改变调用该函数的原始矩阵。
310
311**系统能力:**  SystemCapability.ArkUI.ArkUI.Full
312
313**参数:**
314
315| 参数名 | 类型                              | 必填 | 说明               |
316| ------ | --------------------------------- | ---- | ------------------ |
317| option | [Matrix4Transit](#matrix4transit) | 是   | 待叠加的矩阵对象。 |
318
319**返回值:**
320
321| 类型                              | 说明               |
322| --------------------------------- | ------------------ |
323| [Matrix4Transit](#matrix4transit) | 矩阵叠加后的对象。 |
324
325**示例:**
326
327```ts
328// xxx.ets
329import matrix4 from '@ohos.matrix4'
330
331@Entry
332@Component
333struct Test {
334  private matrix1 = matrix4.identity().translate({ x: 200 })
335  private matrix2 = matrix4.identity().scale({ x: 2 })
336
337  build() {
338    Column() {
339      // 矩阵变换前
340      Image($r("app.media.icon"))
341        .width("40%")
342        .height(100)
343        .margin({ top: 50 })
344      // 先平移x轴200px,再缩放两倍x轴,得到矩阵变换后的效果图
345      Image($r("app.media.icon"))
346        .transform(this.matrix1.copy().combine(this.matrix2))
347        .width("40%")
348        .height(100)
349        .margin({ top: 50 })
350    }
351  }
352}
353```
354
355![zh-cn_image_0000001118642902](figures/zh-cn_image_0000001118642902.png)
356
357
358### invert
359
360invert(): Matrix4Transit
361
362Matrix的逆函数,可以返回一个当前矩阵对象的逆矩阵,即效果正好相反。会改变调用该函数的原始矩阵。
363
364**系统能力:**  SystemCapability.ArkUI.ArkUI.Full
365
366**返回值:**
367
368| 类型                              | 说明                   |
369| --------------------------------- | ---------------------- |
370| [Matrix4Transit](#matrix4transit) | 当前矩阵的逆矩阵对象。 |
371
372**示例:**
373
374```ts
375import matrix4 from '@ohos.matrix4'
376// matrix1(宽放大2倍) 和 matrix2(宽缩小2倍) 效果相反
377let matrix1 = matrix4.identity().scale({ x: 2 })
378let matrix2 = matrix1.copy().invert()
379
380@Entry
381@Component
382struct Tests {
383  build() {
384    Column() {
385      Image($r("app.media.zh"))
386        .width(200)
387        .height(100)
388        .transform(matrix1)
389        .margin({ top: 100 })
390      Image($r("app.media.zh"))
391        .width(200)
392        .height(100)
393        .margin({ top: 150 })
394        .transform(matrix2)
395    }
396  }
397}
398```
399
400
401### translate
402
403translate(options: TranslateOption): Matrix4Transit
404
405Matrix的平移函数,可以为当前矩阵增加x轴/y轴/z轴平移效果。会改变调用该函数的原始矩阵。
406
407**系统能力:**  SystemCapability.ArkUI.ArkUI.Full
408
409**参数:**
410
411| 参数名 | 类型                                | 必填 | 说明           |
412| ------ | ----------------------------------- | ---- | -------------- |
413| option | [TranslateOption](#translateoption) | 是   | 设置平移参数。 |
414
415**返回值:**
416
417| 类型                              | 说明                         |
418| --------------------------------- | ---------------------------- |
419| [Matrix4Transit](#matrix4transit) | 平移效果后的矩阵对象。 |
420
421**示例:**
422
423```ts
424// xxx.ets
425import matrix4 from '@ohos.matrix4'
426
427@Entry
428@Component
429struct Test {
430  private matrix1 = matrix4.identity().translate({ x: 100, y: 200, z: 30 })
431
432  build() {
433    Column() {
434      Image($r("app.media.bg1")).transform(this.matrix1)
435        .width("40%")
436        .height(100)
437    }
438  }
439}
440```
441
442![zh-cn_image_0000001219662645](figures/zh-cn_image_0000001219662645.png)
443
444
445### scale
446
447scale(options: ScaleOption): Matrix4Transit
448
449
450Matrix的缩放函数,可以为当前矩阵增加x轴/y轴/z轴缩放效果。会改变调用该函数的原始矩阵。
451
452**系统能力:**  SystemCapability.ArkUI.ArkUI.Full
453
454**参数:**
455
456| 参数名 | 类型                        | 必填 | 说明           |
457| ------ | --------------------------- | ---- | -------------- |
458| option | [ScaleOption](#scaleoption) | 是   | 设置缩放参数。 |
459
460**返回值:**
461
462| 类型                              | 说明                         |
463| --------------------------------- | ---------------------------- |
464| [Matrix4Transit](#matrix4transit) | 缩放效果后的矩阵对象。 |
465
466**示例:**
467
468```ts
469// xxx.ets
470import matrix4 from '@ohos.matrix4'
471@Entry
472@Component
473struct Test {
474  private matrix1 = matrix4.identity().scale({ x:2, y:3, z:4, centerX:50, centerY:50 })
475
476  build() {
477    Column() {
478      Image($r("app.media.bg1")).transform(this.matrix1)
479        .width("40%")
480        .height(100)
481    }
482  }
483}
484```
485
486![zh-cn_image_0000001219864131](figures/zh-cn_image_0000001219864131.png)
487
488
489### rotate
490
491rotate(options: RotateOption): Matrix4Transit
492
493
494Matrix的旋转函数,可以为当前矩阵增加x轴/y轴/z轴旋转效果。会改变调用该函数的原始矩阵。
495
496**系统能力:**  SystemCapability.ArkUI.ArkUI.Full
497
498**参数:**
499
500| 参数名 | 类型                          | 必填 | 说明           |
501| ------ | ----------------------------- | ---- | -------------- |
502| option | [RotateOption](#rotateoption) | 是   | 设置旋转参数。 |
503
504**返回值:**
505
506| 类型                              | 说明                         |
507| --------------------------------- | ---------------------------- |
508| [Matrix4Transit](#matrix4transit) | 旋转效果后的矩阵对象。 |
509
510**示例:**
511
512```ts
513// xxx.ets
514import matrix4 from '@ohos.matrix4'
515
516@Entry
517@Component
518struct Test {
519  private matrix1 = matrix4.identity().rotate({ x: 1, y: 1, z: 2, angle: 30 })
520
521  build() {
522    Column() {
523      Image($r("app.media.bg1")).transform(this.matrix1)
524        .width("40%")
525        .height(100)
526    }.width("100%").margin({ top: 50 })
527  }
528}
529```
530
531![zh-cn_image_0000001174422898](figures/zh-cn_image_0000001174422898.png)
532
533
534### transformPoint
535
536transformPoint(options: [number, number]): [number, number]
537
538
539Matrix的坐标点转换函数,可以将当前的变换效果作用到一个坐标点上。
540
541**系统能力:**  SystemCapability.ArkUI.ArkUI.Full
542
543**参数:**
544
545| 参数名 | 类型             | 必填 | 说明               |
546| ------ | ---------------- | ---- | ------------------ |
547| option | [number, number] | 是   | 需要转换的坐标点。 |
548
549**返回值:**
550
551| 类型             | 说明                        |
552| ---------------- | --------------------------- |
553| [number, number] | 返回矩阵变换后的Point对象。 |
554
555**示例:**
556
557```ts
558// xxx.ets
559import matrix4 from '@ohos.matrix4'
560
561@Entry
562@Component
563struct Test {
564  private originPoint: number[] = [50, 50]
565  private matrix_1 = matrix4.identity().translate({ x: 150, y: -50 })
566  private transformPoint = this.matrix_1.transformPoint([this.originPoint[0], this.originPoint[1]])
567  private matrix_2 = matrix4.identity().translate({ x: this.transformPoint[0], y: this.transformPoint[1] })
568
569  build() {
570    Column() {
571      Text(`矩阵变换前的坐标:[${this.originPoint}]`)
572        .fontSize(16)
573      Image($r("app.media.image"))
574        .width('600px')
575        .height('300px')
576        .margin({ top: 50 })
577      Text(`矩阵变换后的坐标:[${this.transformPoint}]`)
578        .fontSize(16)
579        .margin({ top: 100 })
580      Image($r("app.media.image"))
581        .width('600px')
582        .height('300px')
583        .margin({ top: 50 })
584        .transform(this.matrix_2)
585    }.width("100%").padding(50)
586  }
587}
588```
589
590![zh-cn_image_0000001219864133](figures/zh-cn_image_0000001219864133.PNG)
591
592## TranslateOption
593
594**系统能力:** SystemCapability.ArkUI.ArkUI.Full
595
596| 名称 | 类型   | 必填 | 说明                                                        |
597| ---- | ------ | ---- | ----------------------------------------------------------- |
598| x    | number | 否   | x轴的平移距离,单位px。<br/>默认值:0<br/>取值范围 (-∞, +∞) |
599| y    | number | 否   | y轴的平移距离,单位px。<br/>默认值:0<br/>取值范围 (-∞, +∞) |
600| z    | number | 否   | z轴的平移距离,单位px。<br/>默认值:0<br/>取值范围 (-∞, +∞) |
601
602## ScaleOption
603
604**系统能力:** SystemCapability.ArkUI.ArkUI.Full
605
606| 名称    | 类型   | 必填 | 说明                                                         |
607| ------- | ------ | ---- | ------------------------------------------------------------ |
608| x       | number | 否   | x轴的缩放倍数。x>1时以x轴方向放大,0&lt;x&lt;1时以x轴方向缩小,x<0时沿x轴反向并缩放。<br/>默认值:1<br/>取值范围 (-∞, +∞) |
609| y       | number | 否   | y轴的缩放倍数。y>1时以y轴方向放大,0&lt;y&lt;1时以y轴方向缩小,y<0时沿y轴反向并缩放。<br/>默认值:1<br/>取值范围 (-∞, +∞) |
610| z       | number | 否   | z轴的缩放倍数。z>1时以z轴方向放大,0&lt;z&lt;1时以z轴方向缩小,z<0时沿z轴反向并缩放。<br/>默认值:1<br/>取值范围 (-∞, +∞) |
611| centerX | number | 否   | 变换中心点x轴坐标。<br/>默认值:0。<br/>取值范围 (-∞, +∞)    |
612| centerY | number | 否   | 变换中心点y轴坐标。<br/>默认值:0。<br/>取值范围 (-∞, +∞)    |
613
614## RotateOption
615
616**系统能力:** SystemCapability.ArkUI.ArkUI.Full
617
618| 名称    | 类型   | 必填 | 说明                                                    |
619| ------- | ------ | ---- | ------------------------------------------------------- |
620| x       | number | 否   | 旋转轴向量x坐标。<br/>默认值:0。<br/>取值范围 (-∞, +∞) |
621| y       | number | 否   | 旋转轴向量y坐标。<br/>默认值:0。<br/>取值范围 (-∞, +∞) |
622| z       | number | 否   | 旋转轴向量z坐标。<br/>默认值:0。<br/>取值范围 (-∞, +∞)。<br/>**说明:** 旋转向量中x、y、z至少有一个不为0才有意义。 |
623| angle   | number | 否   | 旋转角度。<br/>默认值:0                                |
624| centerX | number | 否   | 变换中心点x轴坐标。<br/>默认值:0                       |
625| centerY | number | 否   | 变换中心点y轴坐标。<br/>默认值:0                       |