• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16/**
17 * 为图像设置颜色滤镜效果
18 */
19import { drawing, common2D } from '@kit.ArkGraphics2D';
20
21@Entry
22@Component
23struct ImageExample3 {
24  private imageOne: Resource = $r('app.media.1');
25  private imageTwo: Resource = $r('app.media.2');
26  @State src: Resource = this.imageOne;
27  @State src2: Resource = this.imageTwo;
28  private colorFilterMatrix: number[] = [1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0];
29  private color: common2D.Color = {
30    alpha: 255,
31    red: 255,
32    green: 0,
33    blue: 0
34  };
35  @State drawingColorFilterFirst: ColorFilter | undefined = undefined;
36  @State drawingColorFilterSecond: ColorFilter | undefined = undefined;
37  @State drawingColorFilterThird: ColorFilter | undefined = undefined;
38
39  build() {
40    Column() {
41      Image(this.src)
42        .width(100)
43        .height(100)
44        .colorFilter(this.drawingColorFilterFirst)
45        .onClick(()=>{
46          this.drawingColorFilterFirst =
47            drawing.ColorFilter.createBlendModeColorFilter(this.color, drawing.BlendMode.SRC_IN);
48        })
49
50      Image(this.src2)
51        .width(100)
52        .height(100)
53        .colorFilter(this.drawingColorFilterSecond)
54        .onClick(()=>{
55          this.drawingColorFilterSecond = new ColorFilter(this.ColorFilterMatrix);
56        })
57
58      //当加载图片为SVG格式时
59      Image($r('app.media.test_self'))
60        .width(110)
61        .height(110)
62        .margin(15)
63        .colorFilter(this.drawingColorFilterThird)
64        .onClick(()=>{
65          this.drawingColorFilterThird =
66            drawing.ColorFilter.createBlendModeColorFilter(this.color, drawing.BlendMode.SRC_IN);
67        })
68    }
69  }
70}