• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 色彩
2<!--Kit: ArkUI-->
3<!--Subsystem: ArkUI-->
4<!--Owner: @CCFFWW-->
5<!--Designer: @yangfan229-->
6<!--Tester: @lxl007-->
7<!--Adviser: @HelloCrease-->
8
9
10## 色彩
11
12通过颜色渐变接口,可以设置组件的背景颜色渐变效果,实现在两个或多个指定的颜色之间进行平稳的过渡。
13
14| 接口 | 说明 |
15| -------- | -------- |
16| [linearGradient](../reference/apis-arkui/arkui-ts/ts-universal-attributes-gradient-color.md#lineargradient) | 为当前组件添加线性渐变的颜色渐变效果。 |
17| [sweepGradient](../reference/apis-arkui/arkui-ts/ts-universal-attributes-gradient-color.md#sweepgradient) | 为当前组件添加角度渐变的颜色渐变效果。 |
18| [radialGradient](../reference/apis-arkui/arkui-ts/ts-universal-attributes-gradient-color.md#radialgradient) | 为当前组件添加径向渐变的颜色渐变效果。 |
19
20
21## 为组件添加线性渐变效果
22
23
24```ts
25@Entry
26@Component
27struct LinearGradientDemo {
28  build() {
29    Grid() {
30      GridItem() {
31        Column() {
32          Text('angle: 180')
33            .fontSize(15)
34        }
35        .width(100)
36        .height(100)
37        .justifyContent(FlexAlign.Center)
38        .borderRadius(10)
39        .linearGradient({
40          // 0点方向顺时针旋转为正向角度,线性渐变起始角度的默认值为180°
41          colors: [
42            [0xf56c6c, 0.0], // 颜色断点1的颜色和比重,对应组件在180°方向上的起始位置
43            [0xffffff, 1.0],// 颜色断点2的颜色和比重,对应组件在180°方向上的终点位置
44          ]
45        })
46      }
47
48      GridItem() {
49        Column() {
50          Text('angle: 45')
51            .fontSize(15)
52        }
53        .width(100)
54        .height(100)
55        .justifyContent(FlexAlign.Center)
56        .borderRadius(10)
57        .linearGradient({
58          angle: 45, // 设置颜色渐变起始角度为顺时针方向45°
59          colors: [
60            [0xf56c6c, 0.0],
61            [0xffffff, 1.0],
62          ]
63        })
64      }
65
66      GridItem() {
67        Column() {
68          Text('repeat: true')
69            .fontSize(15)
70        }
71        .width(100)
72        .height(100)
73        .justifyContent(FlexAlign.Center)
74        .borderRadius(10)
75        .linearGradient({
76          repeating: true, // 在当前组件内0.3到1.0区域内重复0到0.3区域的颜色渐变效果
77          colors: [
78            [0xf56c6c, 0.0],
79            [0xE6A23C, 0.3],
80          ]
81        })
82      }
83
84      GridItem() {
85        Column() {
86          Text('repeat: false')
87            .fontSize(15)
88        }
89        .width(100)
90        .height(100)
91        .justifyContent(FlexAlign.Center)
92        .borderRadius(10)
93        .linearGradient({
94          colors: [
95            [0xf56c6c, 0.0], // repeating默认为false,此时组件内只有0到0.3区域内存在颜色渐变效果
96            [0xE6A23C, 0.3],
97          ]
98        })
99      }
100    }
101    .columnsGap(10)
102    .rowsGap(10)
103    .columnsTemplate('1fr 1fr')
104    .rowsTemplate('1fr 1fr 1fr')
105    .width('100%')
106    .height('100%')
107  }
108}
109```
110
111![zh-cn_image_0000001641176829](figures/zh-cn_image_0000001641176829.png)
112
113
114## 为组件添加角度渐变效果
115
116
117```ts
118@Entry
119@Component
120struct SweepGradientDemo {
121  build() {
122    Grid() {
123      GridItem() {
124        Column() {
125          Text('center: 50')
126            .fontSize(15)
127        }
128        .width(100)
129        .height(100)
130        .justifyContent(FlexAlign.Center)
131        .borderRadius(10)
132        .sweepGradient({
133          center: [50, 50], // 角度渐变中心点
134          start: 0, // 角度渐变的起点
135          end: 360, // 角度渐变的终点。
136          repeating: true, // 渐变效果在重复
137          colors: [
138          // 当前组件中,按照中心点和渐变的起点和终点值,
139          // 角度区域为0-0.125的范围,从颜色断点1的颜色渐变到颜色断点2的颜色,
140          // 角度区域0.125到0.25的范围,从颜色断点2的颜色渐变到颜色断点3的颜色,
141          // 因为repeating设置为true,角度区域0.25到1的范围,重复区域0到0.25的颜色渐变效果
142            [0xf56c6c, 0], // 颜色断点1的颜色和比重,对应角度为0*360°=0°,角点为中心点
143            [0xffffff, 0.125], // 颜色断点2的颜色和比重
144            [0x409EFF, 0.25]// 颜色断点3的颜色和比重
145          ]
146        })
147      }
148
149      GridItem() {
150        Column() {
151          Text('center: 0')
152            .fontSize(15)
153        }
154        .width(100)
155        .height(100)
156        .justifyContent(FlexAlign.Center)
157        .borderRadius(10)
158        .sweepGradient({
159          center: [0, 0], // 角度渐变中心点,当前为组件的左上角坐标
160          start: 0,
161          end: 360,
162          repeating: true,
163          colors: [
164          // 当前组件中,因为角度渐变中心是组件的左上角,所以从颜色断点1到颜色断点3的角度范围,恰好可以覆盖整个组件
165            [0xf56c6c, 0], // 颜色断点1的颜色和比重,对应角度为0*360°=0°
166            [0xffffff, 0.125], // 颜色断点2的颜色和比重,对应角度为0.125*360°=45°
167            [0x409EFF, 0.25]// 颜色断点3的颜色和比重,对应角度为0.25*360°=90°
168          ]
169        })
170      }
171
172      GridItem() {
173        Column() {
174          Text('repeat: true')
175            .fontSize(15)
176        }
177        .width(100)
178        .height(100)
179        .justifyContent(FlexAlign.Center)
180        .borderRadius(10)
181        .sweepGradient({
182          center: [50, 50],
183          start: 0,
184          end: 360,
185          repeating: true,
186          colors: [
187            [0xf56c6c, 0],
188            [0xffffff, 0.125],
189            [0x409EFF, 0.25]
190          ]
191        })
192      }
193
194      GridItem() {
195        Column() {
196          Text('repeat: false')
197            .fontSize(15)
198        }
199        .width(100)
200        .height(100)
201        .justifyContent(FlexAlign.Center)
202        .borderRadius(10)
203        .sweepGradient({
204          center: [50, 50],
205          start: 0,
206          end: 360,
207          repeating: false, //只在颜色断点角度覆盖范围内产生颜色渐变效果,其余范围内不重复
208          colors: [
209            [0xf56c6c, 0],
210            [0xffffff, 0.125],
211            [0x409EFF, 0.25]
212          ]
213        })
214      }
215    }
216    .columnsGap(10)
217    .rowsGap(10)
218    .columnsTemplate('1fr 1fr')
219    .rowsTemplate('1fr 1fr 1fr')
220    .width('100%')
221    .height(437)
222  }
223}
224```
225
226![zh-cn_image_0000001641177073](figures/zh-cn_image_0000001641177073.png)
227
228
229## 为组件添加径向渐变效果
230
231
232```ts
233@Entry
234@Component
235struct radialGradientDemo {
236  build() {
237    Grid() {
238      GridItem() {
239        Column() {
240          Text('center: 50')
241            .fontSize(15)
242        }
243        .width(100)
244        .height(100)
245        .justifyContent(FlexAlign.Center)
246        .borderRadius(10)
247        .radialGradient({
248          center: [50, 50], // 径向渐变中心点
249          radius: 100, // 径向渐变半径
250          repeating: true, // 允许在组件内渐变范围外重复按照渐变范围内效果着色
251          colors: [
252          // 组件内以[50,50]为中心点,在半径为0到12.5的范围内从颜色断点1的颜色渐变到颜色断点2的颜色,
253          // 在半径为12.5到25的范围内从颜色断点2的颜色渐变到颜色断点3的颜色,
254          // 组件外其他半径范围内按照半径为0到25的渐变效果重复着色
255            [0xf56c6c, 0], // 颜色断点1的颜色和比重,对应半径为0*100=0
256            [0xffffff, 0.125], // 颜色断点2的颜色和比重,对应半径为0.125*100=12.5
257            [0x409EFF, 0.25]// 颜色断点3的颜色和比重,对应半径为0.25*100=25
258          ]
259        })
260      }
261
262      GridItem() {
263        Column() {
264          Text('center: 0')
265            .fontSize(15)
266        }
267        .width(100)
268        .height(100)
269        .justifyContent(FlexAlign.Center)
270        .borderRadius(10)
271        .radialGradient({
272          center: [0, 0], // 径向渐变中心点,当前为组件左上角坐标
273          radius: 100,
274          repeating: true,
275          colors: [
276            [0xf56c6c, 0],
277            [0xffffff, 0.125],
278            [0x409EFF, 0.25]
279          ]
280        })
281      }
282
283      GridItem() {
284        Column() {
285          Text('repeat: true')
286            .fontSize(15)
287        }
288        .width(100)
289        .height(100)
290        .justifyContent(FlexAlign.Center)
291        .borderRadius(10)
292        .radialGradient({
293          center: [50, 50],
294          radius: 100,
295          repeating: true,
296          colors: [
297            [0xf56c6c, 0],
298            [0xffffff, 0.125],
299            [0x409EFF, 0.25]
300          ]
301        })
302      }
303
304      GridItem() {
305        Column() {
306          Text('repeat: false')
307            .fontSize(15)
308        }
309        .width(100)
310        .height(100)
311        .justifyContent(FlexAlign.Center)
312        .borderRadius(10)
313        .radialGradient({
314          center: [50, 50],
315          radius: 100,
316          repeating: false, // 在组件内渐变范围外不重复按照渐变范围内效果着色
317          colors: [
318            [0xf56c6c, 0],
319            [0xffffff, 0.125],
320            [0x409EFF, 0.25]
321          ]
322        })
323      }
324    }
325    .columnsGap(10)
326    .rowsGap(10)
327    .columnsTemplate('1fr 1fr')
328    .rowsTemplate('1fr 1fr 1fr')
329    .width('100%')
330    .height('100%')
331  }
332}
333```
334
335![zh-cn_image_0000001592904050](figures/zh-cn_image_0000001592904050.png)
336