HarmonyOS 怎么实现Image动态修改颜色?

如题:HarmonyOS 怎么实现Image动态修改颜色?

阅读 553
1 个回答

可以通过设置Image组件的colorFilter值达到更改图片颜色的效果。

参考此链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-basic-components-image-V5\#colorfilter9

@Entry
@Component
struct Index {
  @State color: number[] = [0.78, 0, 0, 0, 0,
    0, 0.93, 0, 0, 0,
    0, 0, 0.80, 0, 0,
    0, 0, 0, 1, 0]

  build() {
    Row() {
      Column() {
        Image($r("app.media.white"))
          .width("50%")
          .colorFilter(this.color)
        Button("切换").onClick(() => {
          // #FAF9DE
          this.color = [0.98, 0, 0, 0, 0,
            0, 0.976, 0, 0, 0,
            0, 0, 0.817, 0, 0,
            0, 0, 0, 1, 0]
        })
      }
      .width('100%')
    }
    .height('100%')
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进