• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Rect
2
3矩形绘制组件。
4
5>  **说明:**
6>
7>  该组件从API Version 9开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
8
9
10## 子组件
11
1213
14
15## 接口
16
17Rect(value?: {width?: string | number,height?: string | number,radius?: string | number | Array<string | number>} |
18  {width?: string | number,height?: string | number,radiusWidth?: string | number,radiusHeight?: string | number})
19
20从API version 9开始,该接口支持在ArkTS卡片中使用。
21
22**参数:**
23
24| 参数名 | 参数类型 | 必填 | 默认值 | 参数描述 |
25| -------- | -------- | -------- | -------- | -------- |
26| width | string&nbsp;\|&nbsp;number | 否 | 0 | 宽度。<br/>**说明:**  <br/>异常值按照默认值处理。 |
27| height | string&nbsp;\|&nbsp;number | 否 | 0 | 高度。<br/>**说明:**  <br/>异常值按照默认值处理。 |
28| radius | string&nbsp;\|&nbsp;number&nbsp;\|&nbsp;Array&lt;string&nbsp;\|&nbsp;number&gt; | 否 | 0 | 圆角半径,支持分别设置四个角的圆角度数。<br/>该属性和radiusWidth/radiusHeight属性效果类似,在组合使用时优先于radiusWidth/radiusHeight生效<br/>**说明:**  <br/>异常值按照默认值处理。 |
29| radiusWidth | string&nbsp;\|&nbsp;number | 否 | 0 | 圆角宽度。<br/>**说明:**  <br/>异常值按照默认值处理。 |
30| radiusHeight | string&nbsp;\|&nbsp;number | 否 | 0 | 圆角高度。<br/>**说明:**  <br/>异常值按照默认值处理。 |
31
32## 属性
33
34除支持[通用属性](ts-universal-attributes-size.md)外,还支持以下属性:
35
36| 名称 | 类型 | 默认值 | 描述 |
37| -------- | -------- | -------- | -------- |
38| radiusWidth | string&nbsp;\|&nbsp;number | 0 | 圆角的宽度,仅设置宽时宽高一致。 <br/>从API version 9开始,该接口支持在ArkTS卡片中使用。<br/>**说明:**  <br/>异常值按照默认值处理。 |
39| radiusHeight | string&nbsp;\|&nbsp;number | 0 | 圆角的高度,仅设置高时宽高一致。 <br/>从API version 9开始,该接口支持在ArkTS卡片中使用。<br/>**说明:**  <br/>异常值按照默认值处理。 |
40| radius | string&nbsp;\|&nbsp;number&nbsp;\|&nbsp;Array&lt;string&nbsp;\|&nbsp;number&gt; | 0 | 圆角半径大小。 <br/>从API version 9开始,该接口支持在ArkTS卡片中使用。<br/>**说明:**  <br/>异常值按照默认值处理。 |
41| fill | [ResourceColor](ts-types.md#resourcecolor) | Color.Black | 设置填充区域颜色。 <br/>从API version 9开始,该接口支持在ArkTS卡片中使用。<br/>**说明:**  <br/>异常值按照默认值处理。 |
42| fillOpacity | [Length](ts-types.md#length) | 1 | 设置填充区域透明度。 <br/>取值范围是[0.0, 1.0],若给定值小于0.0,则取值为0.0;若给定值大于1.0,则取值为1.0,其余异常值按1.0处理。<br/>从API version 9开始,该接口支持在ArkTS卡片中使用。 |
43| stroke | [ResourceColor](ts-types.md#resourcecolor) | - | 设置边框颜色,不设置时,默认没有边框。 <br/>从API version 9开始,该接口支持在ArkTS卡片中使用。<br/>**说明:**  <br/>异常值不会绘制边框。 |
44| strokeDashArray | Array&lt;[Length](ts-types.md#length)&gt; | [] | 设置边框间隙。 <br/>从API version 9开始,该接口支持在ArkTS卡片中使用。<br/>**说明:**  <br/>线段相交时可能会出现重叠现象。异常值按照默认值处理。 |
45| strokeDashOffset | number&nbsp;\|&nbsp;string | 0 | 边框绘制起点的偏移量。 <br/>从API version 9开始,该接口支持在ArkTS卡片中使用。<br/>**说明:**  <br/>异常值按照默认值处理。 |
46| strokeLineCap | [LineCapStyle](ts-appendix-enums.md#linecapstyle) | LineCapStyle.Butt | 设置边框端点绘制样式。 <br/>从API version 9开始,该接口支持在ArkTS卡片中使用。|
47| strokeLineJoin | [LineJoinStyle](ts-appendix-enums.md#linejoinstyle) | LineJoinStyle.Miter | 设置边框拐角绘制样式。 <br/>从API version 9开始,该接口支持在ArkTS卡片中使用。|
48| strokeMiterLimit | number&nbsp;\|&nbsp;string | 4 | 设置斜接长度与边框宽度比值的极限值。斜接长度表示外边框外边交点到内边交点的距离,边框宽度即strokeWidth属性的值。<br/>从API version 9开始,该接口支持在ArkTS卡片中使用。<br/>**说明:**<br/>该属性取值需在strokeLineJoin属性取值LineJoinStyle.Miter时生效。 <br/>该属性的合法值范围应当大于等于1.0,当取值范围在[0,1)时按1.0处理,其余异常值按默认值处理。 |
49| strokeOpacity | [Length](ts-types.md#length) | 1 | 设置边框透明度。<br/>从API version 9开始,该接口支持在ArkTS卡片中使用。<br/>**说明:**<br/>该属性的取值范围是[0.0, 1.0],若给定值小于0.0,则取值为0.0;若给定值大于1.0,则取值为1.0,其余异常值按1.0处理 。 |
50| strokeWidth | [Length](ts-types.md#length) | 1 | 设置边框宽度。 <br/>从API version 9开始,该接口支持在ArkTS卡片中使用。<br/>**说明:**<br/>该属性若为string类型, 暂不支持百分比,百分比按照1px处理。 |
51| antiAlias | boolean | true | 是否开启抗锯齿效果。 <br/>从API version 9开始,该接口支持在ArkTS卡片中使用。<br/>**说明:**  <br/>异常值按照默认值处理。 |
52
53## 示例
54### 示例1
55
56```ts
57// xxx.ets
58@Entry
59@Component
60struct RectExample {
61  build() {
62    Column({ space: 10 }) {
63      Text('normal').fontSize(11).fontColor(0xCCCCCC).width('90%')
64      // 绘制90% * 50的矩形
65      Column({ space: 5 }) {
66        Text('normal').fontSize(9).fontColor(0xCCCCCC).width('90%')
67        // 绘制90% * 50矩形
68        Rect({ width: '90%', height: 50 })
69          .fill(Color.Pink)
70        // 绘制90% * 50的矩形框
71        Rect()
72          .width('90%')
73          .height(50)
74          .fillOpacity(0)
75          .stroke(Color.Red)
76          .strokeWidth(3)
77
78        Text('with rounded corners').fontSize(11).fontColor(0xCCCCCC).width('90%')
79        // 绘制90% * 80的矩形, 圆角宽高分别为40、20
80        Rect({ width: '90%', height: 80 })
81          .radiusHeight(20)
82          .radiusWidth(40)
83          .fill(Color.Pink)
84        // 绘制90% * 80的矩形, 圆角宽高为20
85        Rect({ width: '90%', height: 80 })
86          .radius(20)
87          .fill(Color.Pink)
88          .stroke(Color.Transparent)
89      }.width('100%').margin({ top: 10 })
90      // 绘制90% * 50矩形, 左上圆角宽高40,右上圆角宽高20,右下圆角宽高40,左下圆角宽高20
91      Rect({ width: '90%', height: 80 })
92        .radius([[40, 40], [20, 20], [40, 40], [20, 20]])
93        .fill(Color.Pink)
94    }.width('100%').margin({ top: 5 })
95  }
96}
97```
98
99![zh-cn_image_0000001174264386](figures/zh-cn_image_0000001174264386.png)
100
101### 示例2
102
103```ts
104// xxx.ets
105@Entry
106@Component
107struct RectExample {
108  build() {
109    Column({ space: 10 }) {
110      Column()
111        .width(100)
112        .height(100)
113        .linearGradient({
114          direction: GradientDirection.Right,
115          colors: [[0xff0000, 0.0], [0x0000ff, 0.3], [0xffff00, 1.0]]
116        })
117        .clip(new Rect({ width: 100, height: 100, radius: 40 }))
118      Rect()
119        .width(100)
120        .height(100)
121        // 设置矩形填充,如果需要显示背景的渐变色,请设置区域透明度.fillOpacity(0.0)
122        .fill(Color.Pink)
123        // 设置倒角为40
124        .radius(40)
125        .stroke(Color.Black)
126        // 设置渐变色,仅100*100的矩形区域生效,渐变色的边界不包含倒角
127        .linearGradient({
128          direction: GradientDirection.Right,
129          colors: [[0xff0000, 0.0], [0x0000ff, 0.3], [0xffff00, 1.0]]
130        })
131    }
132  }
133}
134```
135
136![zh-cn_image_0000001174264386](figures/zh-cn_image_0000001174264387.jpeg)
137