📝往期推文全新看点(文中附带最新·鸿蒙全栈学习笔记)

1️⃣ 市场巨变,移动开发行业即将迎来“第二春”?

2️⃣ 鸿蒙应用开发与鸿蒙系统开发哪个更有前景?

3️⃣ 嵌入式开发适不适合做鸿蒙南向开发?看完这篇你就了解了~

4️⃣ 对于大前端开发来说,转鸿蒙开发究竟是福还是祸?

5️⃣ 鸿蒙岗位需求突增!移动端、PC端、IoT到底该怎么选?

6️⃣ 记录一场鸿蒙开发岗位面试经历~

7️⃣ 持续更新中……


介绍

本示例介绍使用image库以及effectKit库中的colorPicker对目标图片进行取色,将获取的颜色作为背景渐变色,通过swiper组件对图片进行轮播,

效果图预览

使用说明

直接进入页面,对图片进行左右滑动,或者等待几秒,图片会自动轮播,图片切换后即可改变背景颜色。

实现思路

  1. 在事件onAnimationStart切换动画过程中通过Image模块相关能力,获取图片颜色平均值,使用effectKit库中的ColorPicker智能取色器进行颜色取值。
    const context = getContext(this);
    //获取resourceManager资源管理器
    const resourceMgr: resourceManager.ResourceManager = context.resourceManager;
    const fileData: Uint8Array = await resourceMgr.getMediaContent(this.imgData[targetIndex]);
    //获取图片的ArrayBuffer
    const buffer = fileData.buffer;
    //创建imageSource
    const imageSource: image.ImageSource = image.createImageSource(buffer);
    //创建pixelMap
    const pixelMap: image.PixelMap = await imageSource.createPixelMap();

    effectKit.createColorPicker(pixelMap, (err, colorPicker) => {
      //读取图像主色的颜色值,结果写入Color
      let color = colorPicker.getMainColorSync();
    })
  1. 同时通过接口animateTo开启背景颜色渲染的属性动画。全局界面开启沉浸式状态栏。
   animateTo({ duration: 500, curve: Curve.Linear, iterations: 1 }, () => {
      //将取色器选取的color示例转换为十六进制颜色代码
      this.bgColor = "#" + color.alpha.toString(16) + color.red.toString(16) + color.green.toString(16) + color.blue.toString(16);
   })
  1. 通过属性linearGradient设置背景色渲染方向以及渲染氛围。
    linearGradient({
      //渐变方向
      direction: GradientDirection.Bottom,
      //数组末尾元素占比小于1时,满足重复着色的效果
      colors: [[this.bgColor, 0.0], [Color.White, 0.5]]
    })

工程结构&模块类型

   effectKit                                  // har类型
   |---pages
   |---|---MainPage.ets                       // 视图层-场景列表页面

Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐