HarmonyOS 自定义设置图片颜色(前景色)的能力

在可变样式的页面场景,需要根据不同的页面风格,动态改变图片的颜色,以符合整体的风格样式。

图片颜色适配不同的页面风格,不用预埋所有颜色的相同图片。

HarmonyOS
2024-10-29 11:23:00
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
put_get

​该问题可以参考开发指南(代码)实现:

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-graphics-drawing-V5#ZH-CN_TOPIC_0000001847209028__createblendmodecolorfilter

例子如下:​

import drawing from '@ohos.graphics.drawing';  
import common2D from '@ohos.graphics.common2D';  
  
@Entry  
@Component  
struct ImageExample {  
  @State imageSrc: Resource = $r('app.media.all_channel_enter');  
  private color: common2D.Color = {  
    alpha: 120,  
    red: 34,  
    green: 155,  
    blue: 100  
  };  
  private color1: common2D.Color = {  
    alpha: 190,  
    red: 255,  
    green: 30,  
    blue: 0  
  };  
  @State DrawingColorFilter: ColorFilter | undefined =  
    drawing.ColorFilter.createBlendModeColorFilter(this.color, drawing.BlendMode.MODULATE);  
  @State DrawingColorFilter1: ColorFilter | undefined =  
    drawing.ColorFilter.createBlendModeColorFilter(this.color1, drawing.BlendMode.MODULATE);  
  
  build() {  
    Column() {  
      Image(this.imageSrc)  
        .width(100)  
        .height(100)  
        .colorFilter(this.DrawingColorFilter)  
  
      Image(this.imageSrc)  
        .width(100)  
        .height(100)  
        .colorFilter(this.DrawingColorFilter1)  
    }  
  }  
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
分享
微博
QQ
微信
回复
2024-10-29 17:53:33
相关问题
HarmonyOS 弹框背景色如何自定义图片
677浏览 • 1回复 待解决
HarmonyOS 如何设置自定义弹框颜色
767浏览 • 1回复 待解决
HarmonyOS 自定义Popup arrow颜色怎么设置
631浏览 • 1回复 待解决
自定义弹框,遮罩背景颜色无法设置
1052浏览 • 1回复 待解决
HarmonyOS 自定义Dialog背景色透明问题
1957浏览 • 1回复 待解决
HarmonyOS 图片增加自定义水印
579浏览 • 1回复 待解决
HarmonyOS 自定义图片选择界面
944浏览 • 1回复 待解决
自定义颜色透明度如何实现?
1016浏览 • 1回复 待解决
HarmonyOS WebView设置自定义UserAgent
1106浏览 • 1回复 待解决
HarmonyOS 如何设置自定义字体
1623浏览 • 1回复 待解决
HarmonyOS 自定义设置动态壁纸
1015浏览 • 1回复 待解决
HarmonyOS 全局设置自定义字体方法
828浏览 • 1回复 待解决