• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 图像效果 (系统接口)
2
3设置组件的模糊、阴影、球面效果以及设置图片的图像效果。
4
5>  **说明:**
6>
7>  从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
8>
9>  当前页面仅包含本模块的系统接口,其他公开接口参见[图像效果](ts-universal-attributes-image-effect.md)。
10
11
12## sphericalEffect<sup>10+</sup>
13
14sphericalEffect(value: number)
15
16设置组件的图像球面化程度。
17
18**系统能力:** SystemCapability.ArkUI.ArkUI.Full
19
20**系统接口:** 此接口为系统接口
21
22**参数:**
23
24| 参数名 | 类型   | 必填 | 说明                                                         |
25| ------ | ------ | ---- | ------------------------------------------------------------ |
26| value  | number | 是   | 设置组件的图像球面化程度。<br/>取值范围:[0,1]。<br/>**说明:**<br/>1. 如果value等于0则图像保持原样,如果value等于1则图像为完全球面化效果。在0和1之间,数值越大,则球面化程度越高。<br/>`value < 0 `或者` value > 1`为异常情况,`value < 0`按0处理,`value > 1`按1处理。<br/>2. 如果组件的图像使用异步加载,则不支持球面效果。例如Image组件默认使用异步加载,如果要使用球面效果,就要设置`syncLoad`为`true`,但是这种做法不推荐。`backgroundImage`也是使用异步加载,所以如果设置了`backgroundImage`,不支持球面效果。<br/>3. 如果组件设置了阴影,不支持球面效果。<br>4. 设置value大于0时,组件冻屏不更新并且把组件内容绘制到透明离屏buffer上,如果要更新组件属性则需要把value设置为0。 |
27
28## lightUpEffect<sup>10+</sup>
29
30lightUpEffect(value: number)
31
32设置组件图像亮起程度。
33
34**系统能力:** SystemCapability.ArkUI.ArkUI.Full
35
36**系统接口:** 此接口为系统接口
37
38**参数:**
39
40| 参数名 | 类型   | 必填 | 说明                                                         |
41| ------ | ------ | ---- | ------------------------------------------------------------ |
42| value  | number | 是   | 设置组件图像亮起程度。<br/>取值范围:[0,1]。<br/>如果value等于0则图像为全黑,如果value等于1则图像为全亮效果。0到1之间数值越大,表示图像亮度越高。`value < 0` 或者 `value > 1`为异常情况,`value < 0`按0处理,`value > 1`按1处理。 |
43
44## pixelStretchEffect<sup>10+</sup>
45
46pixelStretchEffect(options: PixelStretchEffectOptions)
47
48设置组件的图像边缘像素扩展距离。
49
50**系统能力:** SystemCapability.ArkUI.ArkUI.Full
51
52**系统接口:** 此接口为系统接口
53
54**参数:**
55
56| 参数名  | 类型                                                         | 必填 | 说明                                                         |
57| ------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
58| options | [PixelStretchEffectOptions](ts-types.md#pixelstretcheffectoptions10) | 是   | 设置组件的图像边缘像素扩展距离。<br/>参数`options`包括上下左右四个方向的边缘像素扩展距离。<br/>**说明:**<br/>1. 如果距离为正值,表示向外扩展,放大原来图像大小。上下左右四个方向分别用边缘像素填充,填充的距离即为设置的边缘扩展的距离。<br/>2. 如果距离为负值,表示内缩,但是最终图像大小不变。<br/>内缩方式:<br/>图像根据`options`的设置缩小,缩小大小为四个方向边缘扩展距离的绝对值。<br/>图像用边缘像素扩展到原来大小。<br/>3. 对`options`的输入约束:<br/>上下左右四个方向的扩展统一为非正值或者非负值。即四个边同时向外扩或者内缩,方向一致。<br/>所有方向的输入均为百分比或者具体值,不支持百分比和具体值混用。<br/>所有异常情况下,显示为{0,0,0,0}效果,即跟原图保持一致。 |
59
60
61## 示例
62
63### 示例1
64
65设置组件的图像球面效果。
66
67```ts
68// xxx.ets
69@Entry
70@Component
71struct SphericalEffectExample {
72  build() {
73    Stack() {
74      TextInput({ placeholder: "请输入变化范围百分比([0%,100%])"})
75        .width('50%')
76        .height(35)
77        .type(InputType.Number)
78        .enterKeyType(EnterKeyType.Done)
79        .caretColor(Color.Red)
80        .placeholderColor(Color.Blue)
81        .placeholderFont({
82          size: 20,
83          style: FontStyle.Italic,
84          weight: FontWeight.Bold
85        })
86        .sphericalEffect(0.5)
87    }.alignContent(Alignment.Center).width("100%").height("100%")
88  }
89}
90
91```
92
93效果图如下:
94
95![textInputSpherical1](figures/textInputSpherical1.png)
96
97去掉sphericalEffect的设置,效果如下:
98
99![textInputSpherical2](figures/textInputSpherical2.png)
100
101### 示例2
102
103设置组件的图像渐亮效果。
104
105```ts
106// xxx.ets
107@Entry
108@Component
109struct LightUpExample {
110  build() {
111    Stack() {
112      Text('This is the text content with letterSpacing 0.')
113        .letterSpacing(0)
114        .fontSize(12)
115        .border({ width: 1 })
116        .padding(10)
117        .width('50%')
118        .lightUpEffect(0.6)
119    }.alignContent(Alignment.Center).width("100%").height("100%")
120  }
121}
122
123```
124
125效果图如下:
126
127![textLightUp3](figures/textLightUp3.png)
128
129修改lightUpEffect参数值为0.2:
130
131![textLightUp2](figures/textLightUp2.png)
132
133去掉lightUpEffect的设置,效果如下:
134
135![textLightUp1](figures/textLightUp1.png)
136
137### 示例3
138
139```ts
140// xxx.ets
141@Entry
142@Component
143struct LightUpExample {
144  @State isLunar: boolean = false
145  private selectedDate: Date = new Date('2028-08-08')
146  build() {
147    Stack() {
148      DatePicker({
149        start: new Date('1970-1-1'),
150        end: new Date('2100-1-1'),
151        selected: this.selectedDate
152      })
153        .lunar(this.isLunar)
154        .onDateChange((value: Date) => {
155          this.selectedDate = value
156          console.info('select current date is: ' + value.toString())
157        })
158        .lightUpEffect(0.6)
159
160    }.alignContent(Alignment.Center).width("100%").height("100%")
161  }
162}
163```
164
165![datePickerLightUp2](figures/datePickerLightUp2.png)
166
167去掉lightUpEffect的设置,效果如下:
168
169![datePickerLightUp1](figures/datePickerLightUp1.png)
170
171### 示例4
172
173设置组件的图像边缘像素扩展效果。
174
175```ts
176// xxx.ets
177@Entry
178@Component
179struct PixelStretchExample {
180  build() {
181    Stack() {
182      Text('This is the text content with letterSpacing 0.')
183        .letterSpacing(0)
184        .fontSize(12)
185        .border({ width: 1 })
186        .padding(10)
187        .clip(false)
188        .width('50%')
189        .pixelStretchEffect({top:10,left:10,right:10,bottom:10 })
190    }.alignContent(Alignment.Center).width("100%").height("100%")
191  }
192}
193
194```
195
196效果图如下:
197
198![textPixelStretch1](figures/textPixelStretch1.png)
199
200去掉pixelStretchEffect的设置,原图效果如下:
201
202![textPixelStretch2](figures/textPixelStretch2.png)
203
204### 示例5
205
206基于示例4,现在把边缘扩展距离改为非正值。
207
208```ts
209// xxx.ets
210@Entry
211@Component
212struct PixelStretchExample {
213  build() {
214    Stack() {
215      Text('This is the text content with letterSpacing 0.')
216        .letterSpacing(0)
217        .fontSize(12)
218        .border({ width: 1 })
219        .padding(10)
220        .width('50%')
221        .pixelStretchEffect({top:-10,left:-10,right:-10,bottom:-10 })
222    }.alignContent(Alignment.Center).width("100%").height("100%")
223  }
224}
225```
226
227效果图如下:
228
229![textPixelStretch3](figures/textPixelStretch3.png)
230
231跟原图对比发现,效果图分两步实现:<br>1、原图大小缩小,缩小后的大小为原图大小减去像素
232收缩的距离。例如,原图大小为`100*100`,设置了`pixelStretchEffect({top:-10,left:-10,
233right:-10,bottom:-10 })`,则缩小后的大小为`(100-10-10)*(100-10-10)`,即`80*80`。<br>2、使用边缘像素扩展,将图像扩展为原图大小。
234