• HarmonyOS实现几种常见图片点击效果


    一. 样例介绍

    HarmonyOS提供了常用的图片、图片帧动画播放器组件,开发者可以根据实际场景和开发需求,实现不同的界面交互效果,包括:点击阴影效果、点击切换状态、点击动画效果、点击切换动效。

    相关概念

    • image组件:图片组件,用于图片资源的展示。
    • image-animator组件:帧动画播放器,用以播放一组图片,可以设置播放时间、次数等参数。
    • 通用事件:事件绑定在组件上,当组件达到事件触发条件时,会执行JS中对应的事件回调函数,实现页面UI视图和页面JS逻辑层的交互。

    完整示例

    gitee源码地址

    二.环境搭建

    我们首先需要完成HarmonyOS开发环境搭建,可参照如下步骤进行。

    软件要求

    硬件要求

    • 设备类型:华为手机或运行在DevEco Studio上的华为手机设备模拟器。
    • HarmonyOS系统:3.1.0 Developer Release及以上版本。

    环境搭建

    1. 安装DevEco Studio,详情请参考下载和安装软件
    2. 设置DevEco Studio开发环境,DevEco Studio开发环境需要依赖于网络环境,需要连接上网络才能确保工具的正常使用,可以根据如下两种情况来配置开发环境:如果可以直接访问Internet,只需进行下载HarmonyOS SDK操作。
      1. 如果网络不能直接访问Internet,需要通过代理服务器才可以访问,请参考配置开发环境
    3. 开发者可以参考以下链接,完成设备调试的相关配置:

    三.代码结构解读

    本篇Codelab只对核心代码进行讲解,对于完整代码,我们会在源码下载或gitee中提供。

    1. ├──entry/src/main/js                // 代码区
    2. │  └──MainAbility
    3. │     ├──common
    4. │     │  ├──constants
    5. │     │  │  └──commonConstants.js     // 帧动画数据常量
    6. │     │  └──images
    7. │     ├──i18n               // 中英文
    8. │     │  ├──en-US.json
    9. │     │  └──zh-CN.json
    10. │     └──pages
    11. │        └──index
    12. │           ├──index.css              // 首页样式文件
    13. │           ├──index.hml              // 首页布局文件
    14. │           └──index.js               // 首页脚本文件
    15. └──entry/src/main/resources           // 应用资源目录

    四.界面布局

    本示例使用卡片布局,将四种实现以四张卡片的形式呈现在主界面。每张卡片都使用图文结合的方式直观地向开发者展示所实现效果。

    每张卡片对应一个div容器组件,以水平形式分为左侧文本和右侧图片两部分。左侧文本同样是一个div容器组件,以垂直形式分为操作文本与效果描述文本。右侧图片则根据需要使用image组件或image-animator组件。当前示例中,前两张卡片右侧使用的是image组件,剩余两张卡片使用的是image-animator组件。

    1. <div class="container">
    2. <div class="card-container" for="item in imageCards">
    3. <div class="text-container">
    4. <text class="text-operation">{{ contentTitle }}text>
    5. <text class="text-description">{{ item.description }}text>
    6. div>
    7. <image class="{{ item.classType }}" src="{{ item.src }}" onclick="changeHookState({{ item.eventType }})"
    8.                ontouchstart="changeShadow({{ item.eventType }}, true)"
    9.                ontouchend="changeShadow({{ item.eventType }}, false)"/>
    10. div>
    11. <div class="card-container" for="item in animationCards">
    12. <div class="text-container">
    13. <text class="text-operation">{{ contentTitle }}text>
    14. <text class="text-description">{{ item.description }}text>
    15. div>
    16. <image-animator id="{{ item.id }}" class="animator" images="{{ item.frames }}" iteration="1"
    17.                         duration="{{ item.durationTime }}" onclick="handleStartFrame({{ item.type }})"/>
    18. div>
    19. div>

    五.事件交互

    为image组件添加touchstart和touchend事件,实现点击图片改变边框阴影的效果,点击触碰结束时,恢复初始效果。

    1. // index.js
    2. // 点击阴影效果
    3. changeShadow(eventType, shadowFlag) {
    4.   if (eventType === 'click') {
    5.     return;
    6.   }
    7.   if (shadowFlag) {
    8.     this.imageCards[0].classType = 'main-img-touch';
    9.   } else {
    10.     this.imageCards[0].classType = 'img-normal';
    11.   }
    12. }

    为image组件添加click事件,实现点击切换状态并变换显示图片的效果。

    1. // index.js
    2. // 点击切换状态
    3. changeHookState(eventType) {
    4.   if (eventType === 'touch') {
    5.     return;
    6.   }
    7. if (this.hook) {
    8.     this.imageCards[1].src = '/common/images/ic_fork.png';
    9.     this.hook = false;
    10.   } else {
    11.     this.imageCards[1].src = '/common/images/ic_hook.png';
    12.     this.hook = true;
    13.   }
    14. }

    为image-animator组件添加click事件,实现点击播放帧动画效果。

    1. // index.js
    2. // 点击动画效果方法
    3. handleStartFrame(type) {
    4.   if (type === 'dial') {
    5.     this.animationCards[0].durationTime = CommonConstants.DURATION_TIME;
    6.     this.$element('dialAnimation').start();
    7.   } else {
    8.     if (this.animationCards[1].flag) {
    9.       this.animationCards[1].frames = this.collapse;
    10.       this.animationCards[1].durationTime = this.durationTimeArray[0];
    11.       this.$element('toggleAnimation').start();
    12.       this.animationCards[1].flag = false;
    13.       this.$element('toggleAnimation').stop();
    14.     } else {
    15.       this.animationCards[1].frames = this.arrow;
    16.       this.animationCards[1].durationTime = this.durationTimeArray[1];
    17.       this.$element('toggleAnimation').start();
    18.       this.animationCards[1].flag = true;
    19.       this.$element('toggleAnimation').stop();
    20.     }
    21.   }
    22. }
  • 相关阅读:
    如何使用Jekyll在GitHub Pages上搭建网站(个人博客)
    ARMv7/ARMv8/ARMv9架构你不知道的那些事
    【云原生之Docker实战】部署docker管理平台shipyard
    【Spring Boot 使用记录】kafka自动配置和自定义配置及消费者
    Python 自动化(十八)admin后台管理
    A. XOR Mixup
    AI诈骗的防范与应对:维护数字安全的责任
    python 断点续传下载
    Java 多态
    【pytorch09】数学运算
  • 原文地址:https://blog.csdn.net/HarmonyOSDev/article/details/132731737