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