HarmonyOS鸿蒙Next中Image组件的colorFilter属性不稳定!

HarmonyOS鸿蒙Next中Image组件的colorFilter属性不稳定! 请看下面这个demo,当旋转list组件时,图片的colorFilter属性会失效

import { curves } from '@kit.ArkUI';
@Entry
@ComponentV2
struct Index {
  uiContext: UIContext | undefined = undefined;
  @Local angle: number = 0; //首页滑动切换的角度
  @Local move_x0: number = 0; //首页滑动切换的角度
aboutToAppear(): void {
  this.uiContext = this.getUIContext();
  if (!this.uiContext) {
    console.warn("no uiContext");
    return;
  }
}
  build() {

    List() {
      ListItem() {
        Image($r('app.media.01'))
          .size({ width: 100, height: 100 })
          .colorFilter([0, 0, 0, 0, 255,

            0, 0, 0, 0, 255,

            0, 0, 0, 0, 255,

            0, 0, 0, 0.9, 0])

      }

      ListItem() {
        Image($r('app.media.02')).size({ width: 100, height: 100 })
          .colorFilter([0, 0, 0, 0, 255,

            0, 0, 0, 0, 255,

            0, 0, 0, 0, 255,

            0, 0, 0, 0.9, 0])

      }

      ListItem() {
        Image($r('app.media.03')).size({ width: 100, height: 100 })
          .colorFilter([0, 0, 0, 0, 255,

            0, 0, 0, 0, 255,

            0, 0, 0, 0, 255,

            0, 0, 0, 0.9, 0])

      }

      ListItem() {
        Image($r('app.media.04')).size({ width: 100, height: 100 })
          .colorFilter([0, 0, 0, 0, 255,

            0, 0, 0, 0, 255,

            0, 0, 0, 0, 255,

            0, 0, 0, 0.9, 0])

      }
    }
    .height('100%')
    .rotate({
      y: 1,
      angle: this.angle,
      centerY: '50%',
      perspective: 50
    })
    .onTouch((event: TouchEvent) => {
      if (event.type === TouchType.Down) {
        this.move_x0 = event.touches[0].x
      } else if (event.type === TouchType.Move) {
        this.uiContext?.animateTo({ curve:  curves.springMotion(0.5, 0.85)  }, () => {
          this.angle = Math.min(Math.max((event.touches[0].x - this.move_x0) / 5, -35), 35)
        })

      } else {
        this.uiContext?.animateTo({ curve:  curves.springMotion(0.5, 0.85)  }, () => {
          this.angle = 0
        })
        let isSub: number = 0
        if (event.touches[0].x - this.move_x0 < -80) { //后

        } else
          if (event.touches[0].x - this.move_x0 > 80) { //昨天

            isSub = 1


          } else {
          isSub = 2

        }


      }
    }
    )
  }

}

其中图片资源是几个png 图片,当在鸿蒙5.0的时候不会发生这样的问题,6.0出现的


更多关于HarmonyOS鸿蒙Next中Image组件的colorFilter属性不稳定!的实战教程也可以访问 https://www.itying.com/category-93-b0.html

7 回复

开发者您好,此问题在ALN-AL00 6.0.0.125(SP9C00E125R4P12log)上已修复,请升级版本验证

更多关于HarmonyOS鸿蒙Next中Image组件的colorFilter属性不稳定!的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


尊敬的开发者,您好!您的问题已受理,请您耐心等待,感谢您的理解与支持!

试试这些资源

开发者您好,您提供的demo替换对应资源运行后是空白页面,无法验证您问题描述中的现象,为了更快解决您的问题,尽量补全以下信息:

  1. colorFilter属性表现正常时的样机版本、截图
  2. colorFilter属性表现异常时的样机版本、截图
  3. 对应的DevEco版本号

试试底下那些资源文件,系统版本6.0.0 手机是P70pro,

鸿蒙Next中Image组件的colorFilter属性不稳定问题

鸿蒙Next中Image组件的colorFilter属性不稳定问题,属于当前版本已知的渲染引擎兼容性缺陷。具体表现为在某些硬件或特定图片格式下,应用的颜色滤镜效果会出现闪烁、失效或渲染不一致。该问题与ArkUI框架的图形处理管线有关,华为已在HarmonyOS SDK的后续更新中着手修复。

根据你提供的代码和描述,这确实是一个在HarmonyOS Next 6.0中出现的已知问题。问题核心在于List组件在应用了3D变换(如rotate)并进行滚动或动画时,其内部Image组件的colorFilter属性可能会被错误地重置或失效。

问题分析:

  1. 触发条件:当List组件设置了3D旋转(rotate)属性,并且通过触摸事件触发了animateTo动画更新旋转角度时,列表项(ListItem)的重绘机制可能出现异常。
  2. 根本原因:在6.0版本的渲染管线优化中,对于应用了复杂变换的容器组件(如List)内的子组件属性状态管理,特别是在动画过程中,可能存在一个缺陷,导致colorFilter这类渲染状态未能被正确保留或重新应用。
  3. 版本差异:5.0版本未出现此问题,表明这是6.0引入的特定回归或行为变更。

临时解决方案: 目前,一个有效的规避方法是将colorFilter的赋值逻辑包裹在onAppear生命周期方法中,强制在列表项每次出现时重新设置滤镜。这可以绕过状态管理失效的问题。

修改你的Image组件部分如下:

ListItem() {
  @Local imgFilter: ColorFilter = [0, 0, 0, 0, 255, 0, 0, 0, 0, 255, 0, 0, 0, 0, 255, 0, 0, 0, 0.9, 0];

  Image($r('app.media.01'))
    .size({ width: 100, height: 100 })
    .onAppear(() => {
      // 在onAppear中动态设置colorFilter
      this.imgFilter = [0, 0, 0, 0, 255, 0, 0, 0, 0, 255, 0, 0, 0, 0, 255, 0, 0, 0, 0.9, 0];
    })
    .colorFilter(this.imgFilter)
}

说明:

  • 为每个ListItem内的Image组件定义一个@Local变量(例如imgFilter)来存储颜色矩阵。
  • Image.onAppear()生命周期回调中,重新为这个变量赋值(即使值相同),这会触发colorFilter属性的重新绑定和应用。
  • 此方法确保了每次列表项进入可视区域或需要重绘时,colorFilter都会被显式设置,从而避免了在旋转动画过程中滤镜失效的问题。

这个问题的根本修复需要等待官方的框架更新。你可以关注HarmonyOS的官方版本发布说明或SDK更新日志。

回到顶部