107 Star 880 Fork 419

OpenHarmony/codelabs

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

动画样式(JS)

介绍

本篇Codelab使用动画样式,实现几种常见动画效果:平移、旋转、缩放以及透明度变化。

相关概念

  • 自定义组件:自定义组件是用户根据业务需求,将已有的组件组合,封装成的新组件,可以在工程中多次调用,从而提高代码的可读性。
  • 动画样式:组件支持动态的旋转、平移、缩放效果,可在style或css中设置。

环境搭建

软件要求

  • DevEco Studio版本:DevEco Studio 3.1 Release及以上版本。
  • OpenHarmony SDK版本:API version 9及以上版本。

硬件要求

环境搭建

完成本篇Codelab我们首先要完成开发环境的搭建,本示例以RK3568开发板为例,参照以下步骤进行:

  1. 获取OpenHarmony系统版本:标准系统解决方案(二进制)。以3.2 Release版本为例:

  2. 搭建烧录环境。

    1. 完成DevEco Device Tool的安装
    2. 完成RK3568开发板的烧录
  3. 搭建开发环境。

    1. 开始前请参考工具准备,完成DevEco Studio的安装和开发环境配置。
    2. 开发环境配置完成后,请参考使用工程向导创建工程(模板选择“Empty Ability”)。
    3. 工程创建完成后,选择使用真机进行调测

代码结构解读

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

├──entry/src/main/js	             // 代码区
│  └──MainAbility
│     ├──common
│     │  └──images
│     │     └──ic_windmill.png       // 风车图标
│     ├──component
│     │  └──animatedCards
│     │     ├──animatedCards.css     // 自定义动画组件样式
│     │     ├──animatedCards.hml     // 自定义动画组件页面
│     │     └──animatedCards.js      // 自定义动画组件逻辑
│     ├──i18n
│     │  ├──en-US.json               // 英文国际化
│     │  └──zh-CN.json               // 中文国际化
│     ├──pages
│     │  └──animation
│     │     ├──animation.css   	     // 动画页面样式
│     │     ├──animation.hml         // 动画页面
│     │     └──animation.js          // 动画页面逻辑
│     └──app.js                      // 程序入口
└──entry/src/main/resources          // 应用资源目录

页面构建

页面展示几种常见动画效果:平移、旋转、缩放以及透明度变化,界面主要由image组件和text组件组成,效果如图所示:

<!-- animation.hml -->
<element name='animated-cards' src="../../component/animatedCards/animatedCards.hml"></element>
<div class="container">
    <div class="animation-box" for="{{ value in animationList }}">
        <animated-cards icon="{{ windmillIcon }}" animation-list="{{ value }}"></animated-cards>
    </div>
</div>

animation.js文件中,animationList是展示动画效果的列表数据,windmillIcon是页面动画的图片。

// animation.js
export default {
  data: {
    // 动画列表
    animationList: [
      {
        animationName: 'Translate',
        animationStyle: 'img-translate'
      },
      {
        animationName: 'Rotate',
        animationStyle: 'img-rotate'
      },
      {
        animationName: 'RotateY',
        animationStyle: 'img-rotateY'
      },
      {
        animationName: 'Scale',
        animationStyle: 'img-scale'
      },
      {
        animationName: 'Opacity',
        animationStyle: 'img-opacity'
      }
    ],
    // 动画图片
    windmillIcon: '/common/images/ic_windmill.png'
  }
}

动画实现

图片的平移、旋转、缩放以及透明度变化都是在animatedCards自定义组件中进行实现,界面主要由image组件和text组件组成。

<!--animatedCards.hml-->
<div class="container">
    <div class="box">
        <text class="text">{{ animationList.animationName }}</text>
        <div class="windmill-box">
            <image class="img {{ animationList.animationStyle }}" src="{{ icon }}"></image>
        </div>
    </div>
</div>

声明类型为Array的props,父组件可以通过设置props属性向子组件传递参数。

// animatedCards.js
export default {
  props: ['icon', 'animationList']
}

通过css样式,实现风车的平移、旋转、缩放以及透明度的变化。

/* animatedCards.css */
/* 平移动画 */
.img-translate {
    animation-name: translateAnim;
}

/* 顺时针旋转 */
.img-rotate {
    animation-name: rotateAnim;
}

/* Y轴方向旋转 */
.img-rotateY {
    animation-name: rotateYAnim;
}

/* 缩放动画 */
.img-scale {
    animation-name: scaleAnim;
}
/* 透明度变化 */
.img-opacity {
    animation-name: opacityAnim;
}

/* 从-100vp平移到100vp */
@keyframes translateAnim {
    from {
        transform: translate(-100vp);
    }
    to {
        transform: translate(100vp);
    }
}
/* 从0°旋转到360° */
@keyframes rotateAnim {
    from {
        transform: rotate(0deg);
    }
    to {
        transform: rotate(360deg);
    }
}

/* 沿Y轴旋转,从0°旋转到360° */
@keyframes rotateYAnim {
    from {
        transform: rotateY(0deg);
    }
    to {
        transform: rotateY(360deg);
    }
}

/* 从0倍缩放到1.2倍大小 */
@keyframes scaleAnim {
    from {
        transform: scale(0);
    }
    to {
        transform: scale(1.2);
    }
}

/* 不透明度值从0变化到1 */
@keyframes opacityAnim {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}

总结

您已经完成了本次Codelab的学习,并了解到以下知识点:

  1. 自定义组件的使用。
  2. 动画样式的使用。

马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/openharmony/codelabs.git
git@gitee.com:openharmony/codelabs.git
openharmony
codelabs
codelabs
master

搜索帮助