• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 图像效果
2
3设置组件的模糊,阴影效果以及设置图片的图像效果。
4
5>  **说明:**
6>  从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
7
8
9## 属性
10
11
12| 名称                          | 参数类型                                                     | 默认值 | 描述                                                         |
13| ----------------------------- | ------------------------------------------------------------ | ------ | ------------------------------------------------------------ |
14| blur                          | number                                                       | -      | 为当前组件添加内容模糊效果,入参为模糊半径,模糊半径越大越模糊,为0时不模糊。 |
15| backdropBlur                  | number                                                       | -      | 为当前组件添加背景模糊效果,入参为模糊半径,模糊半径越大越模糊,为0时不模糊。 |
16| shadow                        | {<br/>radius:&nbsp;number \| [Resource](ts-types.md#resource),<br/>color?:&nbsp;[Color](ts-appendix-enums.md#color) \| string \| [Resource](ts-types.md#resource),<br/>offsetX?:&nbsp;number \| [Resource](ts-types.md#resource),<br/>offsetY?:&nbsp;number \| [Resource](ts-types.md#resource)<br/>} | -      | 为当前组件添加阴影效果,入参为模糊半径(必填)、阴影的颜色(可选,默认为灰色)、X轴的偏移量(可选,默认为0),Y轴的偏移量(可选,默认为0),偏移量单位为px。 |
17| grayscale                     | number                                                       | 0.0    | 为当前组件添加灰度效果。值定义为灰度转换的比例,入参1.0则完全转为灰度图像,入参则0.0图像无变化,入参在0.0和1.0之间时,效果呈线性变化。(百分比) |
18| brightness                    | number                                                       | 1.0    | 为当前组件添加高光效果,入参为高光比例,值为1时没有效果,小于1时亮度变暗,0为全黑;大于1时亮度增加,数值越大亮度越大。 |
19| saturate                      | number                                                       | 1.0    | 为当前组件添加饱和度效果,饱和度为颜色中的含色成分和消色成分(灰)的比例,入参为1时,显示原图像,大于1时含色成分越大,饱和度越大;小于1时消色成分越大,饱和度越小。(百分比) |
20| contrast                      | number                                                       | 1.0    | 为当前组件添加对比度效果,入参为对比度的值,值为1时,显示原图;大于1时,值越大对比度越高,图像越清晰醒目;小于1时,值越小对比度越低;当对比度为0时,图像变为全灰。(百分比) |
21| invert                        | number                                                       | 0      | 反转输入的图像。入参为图像反转的比例。值为1时完全反转。值为0则图像无变化。(百分比) |
22| colorBlend&nbsp;<sup>8+</sup> | [Color](ts-appendix-enums.md#color)&nbsp;\|&nbsp;string&nbsp;\|&nbsp;[Resource](ts-types.md#resource) | -      | 为当前组件添加颜色叠加效果,入参为叠加的颜色。               |
23| sepia                         | number                                                       | 0      | 将图像转换为深褐色。入参为图像反转的比例。值为1则完全是深褐色的,值为0图像无变化。&nbsp;(百分比) |
24| hueRotate                     | number&nbsp;\|&nbsp;string                                   | '0deg' | 色相旋转效果,输入参数为旋转角度。                           |
25
26## 示例
27
28### 示例1
29模糊属性的用法,blur内容模糊,backdropBlur背景模糊。
30```ts
31// xxx.ets
32@Entry
33@Component
34struct BlurEffectsExample {
35  build() {
36    Column({ space: 10 }) {
37      //       对字体进行模糊
38      Text('font blur').fontSize(15).fontColor(0xCCCCCC).width('90%')
39      Flex({ alignItems: ItemAlign.Center }) {
40        Text('original text').margin(10)
41        Text('blur text')
42          .blur(1).margin(10)
43        Text('blur text')
44          .blur(2).margin(10)
45        Text('blur text')
46          .blur(3).margin(10)
47      }.width('90%').height(40)
48      .backgroundColor(0xF9CF93)
49
50
51      // 对背景进行模糊
52      Text('backdropBlur').fontSize(15).fontColor(0xCCCCCC).width('90%')
53      Text()
54        .width('90%')
55        .height(40)
56        .fontSize(16)
57        .backdropBlur(3)
58        .backgroundImage('/pages/attrs/image/image.jpg')
59        .backgroundImageSize({ width: 1200, height: 160 })
60    }.width('100%').margin({ top: 5 })
61  }
62}
63```
64
65![textblur](figures/textblur.png)
66
67### 示例2
68设置图片的效果,包括阴影,灰度,高光,饱和度,对比度,图像反转,叠色,色相旋转等。
69```ts
70// xxx.ets
71@Entry
72@Component
73struct ImageEffectsExample {
74  build() {
75    Column({ space: 10 }) {
76      // 添加阴影效果,图片效果不变
77      Text('shadow').fontSize(15).fontColor(0xCCCCCC).width('90%')
78      Image($r('app.media.image'))
79        .width('90%')
80        .height(40)
81        .shadow({ radius: 10, color: Color.Green, offsetX: 20, offsetY: 30 })
82
83      // 灰度效果0~1,越接近1,灰度越明显
84      Text('grayscale').fontSize(15).fontColor(0xCCCCCC).width('90%')
85      Image($r('app.media.image')).width('90%').height(40).grayscale(0.3)
86      Image($r('app.media.image')).width('90%').height(40).grayscale(0.8)
87
88      // 高光效果,1为正常图片,<1变暗,>1亮度增大
89      Text('brightness').fontSize(15).fontColor(0xCCCCCC).width('90%')
90      Image($r('app.media.image')).width('90%').height(40).brightness(1.2)
91
92      // 饱和度,原图为1
93      Text('saturate').fontSize(15).fontColor(0xCCCCCC).width('90%')
94      Image($r('app.media.image')).width('90%').height(40).saturate(2.0)
95      Image($r('app.media.image')).width('90%').height(40).saturate(0.7)
96
97      // 对比度,1为原图,>1值越大越清晰,<1值越小越模糊
98      Text('contrast').fontSize(15).fontColor(0xCCCCCC).width('90%')
99      Image($r('app.media.image')).width('90%').height(40).contrast(2.0)
100      Image($r('app.media.image')).width('90%').height(40).contrast(0.8)
101
102      // 图像反转比例
103      Text('invert').fontSize(15).fontColor(0xCCCCCC).width('90%')
104      Image($r('app.media.image')).width('90%').height(40).invert(0.2)
105      Image($r('app.media.image')).width('90%').height(40).invert(0.8)
106
107      // 叠色添加
108      Text('colorBlend').fontSize(15).fontColor(0xCCCCCC).width('90%')
109      Image($r('app.media.image')).width('90%').height(40).colorBlend(Color.Green)
110      Image($r('app.media.image')).width('90%').height(40).colorBlend(Color.Blue)
111
112      // 深褐色
113      Text('sepia').fontSize(15).fontColor(0xCCCCCC).width('90%')
114      Image($r('app.media.image')).width('90%').height(40).sepia(0.8)
115
116      // 色相旋转
117      Text('hueRotate').fontSize(15).fontColor(0xCCCCCC).width('90%')
118      Image($r('app.media.image')).width('90%').height(40).hueRotate(90)
119    }.width('100%').margin({ top: 5 })
120  }
121}
122```
123
124![imageeffect](figures/imageeffect.png)
125
126