开发指导—利用组件&插值器动画实现 HarmonyOS 动效

这篇具有很好参考价值的文章主要介绍了开发指导—利用组件&插值器动画实现 HarmonyOS 动效。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一. 组件动画

在组件上创建和运行动画的快捷方式。具体用法请参考通用方法。

获取动画对象

通过调用 animate 方法获得 animation 对象,animation 对象支持动画属性、动画方法和动画事件。

<!-- xxx.hml --><div class="container">  <div id="content" class="box" onclick="Show"></div></div>

/* xxx.css */.container {  flex-direction: column;  justify-content: center;  align-items: center;  width: 100%;}.box{  width: 200px;  height: 200px;  background-color: #ff0000;  margin-top: 30px;}

/* xxx.js */export default {  data: {    animation: '',  },  onInit() {  },  onShow() {    var options = {      duration: 1500,    };    var frames = [      {        width:200,height:200,      },      {        width:300,height:300,      }    ];    this.animation = this.$element('content').animate(frames, options);  //获取动画对象  },  Show() {       this.animation.play();  }}

开发指导—利用组件&插值器动画实现 HarmonyOS 动效,javascript,开发语言,HarmonyOS

说明

● 使用 animate 方法时必须传入 Keyframes 和 Options 参数。

● 多次调用 animate 方法时,采用 replace 策略,即最后一次调用时传入的参数生效。

设置动画参数

在获取动画对象后,通过设置参数 Keyframes 设置动画在组件上的样式。

<!-- xxx.hml --><div class="container">   <div id="content" class="box" onclick="Show"></div></div>

/* xxx.css */.container {  flex-direction: column;  justify-content: center;  align-items: center;  width: 100%;  height: 100%;}.box{  width: 200px;  height: 200px;  background-color: #ff0000;  margin-top: 30px;}

/* xxx.js */export default {  data: {    animation: '',    keyframes:{},    options:{}  },  onInit() {    this.options = {      duration: 4000,    }    this.keyframes = [    {      transform: {        translate: '-120px -0px',           scale: 1,                rotate: 0        },           transformOrigin: '100px 100px',          offset: 0.0,         width: 200,          height: 200         },       {        transform: {                translate: '120px 0px',               scale: 1.5,               rotate: 90             },          transformOrigin: '100px 100px',          offset: 1.0,          width: 300,          height: 300         }        ]  },  Show() {    this.animation = this.$element('content').animate(this.keyframes, this.options)    this.animation.play()  }}

开发指导—利用组件&插值器动画实现 HarmonyOS 动效,javascript,开发语言,HarmonyOS

说明

● translate、scale 和 rtotate 的先后顺序会影响动画效果。

● transformOrigin 只对 scale 和 rtotate 起作用。

在获取动画对象后,通过设置参数 Options 来设置动画的属性。

<!-- xxx.hml --><div class="container">   <div id="content" class="box" onclick="Show"></div></div>

/* xxx.css */.container {  flex-direction: column;  justify-content: center;  align-items: center;  width: 100%;}.box{  width: 200px;  height: 200px;  background-color: #ff0000;  margin-top: 30px;}

/* xxx.js */export default {  data: {    animation: '',  },  onInit() {  },  onShow() {    var options = {              duration: 1500,              easing: 'ease-in',              delay: 5,              iterations: 2,              direction: 'normal',        };    var frames = [      {        transform: {          translate: '-150px -0px'        }      },      {        transform: {          translate: '150px 0px'        }      }    ];    this.animation = this.$element('content').animate(frames, options);  },  Show() {    this.animation.play();  }}

开发指导—利用组件&插值器动画实现 HarmonyOS 动效,javascript,开发语言,HarmonyOS

说明

direction:指定动画的播放模式。

normal: 动画正向循环播放。

reverse: 动画反向循环播放。

alternate:动画交替循环播放,奇数次正向播放,偶数次反向播放。

alternate-reverse:动画反向交替循环播放,奇数次反向播放,偶数次正向播放。

二.  插值器动画

动画动效

通过设置插值器来实现动画效果。(从 API Version 6 开始支持。)

创建动画对象

通过 createAnimator 创建一个动画对象,通过设置参数 options 来设置动画的属性。

<!-- xxx.hml --><div class="container">  <div style="width: 300px;height: 300px;margin-top: 100px;background: linear-gradient(pink, purple);transform: translate({{translateVal}});">  </div>  <div class="row">    <button type="capsule" value="play" onclick="playAnimation"></button>  </div></div>

/* xxx.css */.container {  width:100%;  height:100%;  flex-direction: column;  align-items: center;  justify-content: center;}button{  width: 200px;}.row{  width: 65%;  height: 100px;  align-items: center;  justify-content: space-between;  margin-top: 50px;  margin-left: 260px;}
// xxx.jsimport animator from '@ohos.animator';export default {  data: {    translateVal: 0,    animation: null  },  onInit() {},  onShow(){    var options = {      duration: 3000,      easing:"friction",      delay:"1000",      fill: 'forwards',      direction:'alternate',      iterations: 2,      begin: 0,      end: 180    };//设置参数    this.animation = animator.createAnimator(options)//创建动画  },  playAnimation() {    var _this = this;    this.animation.onframe = function(value) {      _this.translateVal= value    };    this.animation.play();  }}

开发指导—利用组件&插值器动画实现 HarmonyOS 动效,javascript,开发语言,HarmonyOS

说明

● 使用 createAnimator 创建动画对象时必须传入 options 参数。

● begin 插值起点,不设置时默认为 0。

● end 插值终点,不设置时默认为 1。

添加动画事件和调用接口

animator 支持事件和接口,可以通过添加 frame、cancel、repeat、finish 事件和调用 update、play、pause、cancel、reverse、finish 接口自定义动画效果。animator 支持的事件和接口具体见动画中的createAnimator。

<!-- xxx.hml --><div style="flex-direction: column;align-items: center;width: 100%;height: 100%;">  <div style="width:200px;height: 200px;margin-top: 100px;background: linear-gradient(#b30d29, #dcac1b);  transform: scale({{scaleVal}});"></div>  <div style="width: {{DivWidth}};height: {{DivHeight}};margin-top: 200px;  background: linear-gradient(pink, purple);margin-top: 200px;transform:translateY({{translateVal}});">  </div>  <div class="row">    <button type="capsule" value="play" onclick="playAnimation"></button>    <button type="capsule" value="update" onclick="updateAnimation"></button>  </div>  <div class="row1">    <button type="capsule" value="pause" onclick="pauseAnimation"></button>    <button type="capsule" value="finish" onclick="finishAnimation"></button>  </div>  <div class="row2">    <button type="capsule" value="cancel" onclick="cancelAnimation"></button>    <button type="capsule" value="reverse" onclick="reverseAnimation"></button>  </div></div>

/* xxx.css */button{  width: 200px;}.row{  width: 65%;  height: 100px;  align-items: center;  justify-content: space-between;  margin-top: 150px;  position: fixed;  top: 52%;  left: 120px;}.row1{  width: 65%;  height: 100px;  align-items: center;  justify-content: space-between;  margin-top: 120px;  position: fixed;  top: 65%;  left: 120px;}.row2{  width: 65%;  height: 100px;  align-items: center;  justify-content: space-between;  margin-top: 100px;  position: fixed;  top: 75%;  left: 120px;}

// xxx.jsimport animator from '@ohos.animator';import promptAction from '@ohos.promptAction';export default {  data: {    scaleVal:1,    DivWidth:200,    DivHeight:200,    translateVal:0,    animation: null  },  onInit() {    var options = {      duration: 3000,      fill: 'forwards',      begin: 200,      end: 270    };    this.animation = animator.createAnimator(options);  },  onShow() {    var _this= this;    //添加动画重放事件    this.animation.onrepeat = function() {      promptAction.showToast({        message: 'repeat'      });      var repeatoptions = {        duration: 2000,        iterations: 1,         direction: 'alternate',         begin: 180,         end: 240       };        _this.animation.update(repeatoptions);        _this.animation.play();      };  },  playAnimation() {    var _this= this;    //添加动画逐帧插值回调事件    this.animation.onframe = function(value) {      _this. scaleVal= value/150,      _this.DivWidth = value,      _this.DivHeight = value,      _this.translateVal = value-180    };    this.animation.play();  },  updateAnimation() {    var newoptions = {      duration: 5000,      iterations: 2,      begin: 120,      end: 180    };    this.animation.update(newoptions);    this.animation.play();//调用动画播放接口  },  pauseAnimation() {    this.animation.pause();//调用动画暂停接口  },  finishAnimation() {    var _this= this;   //添加动画完成事件    this.animation.onfinish = function() {      promptAction.showToast({        message: 'finish'      })    };    this.animation.finish(); //调用动画完成接口  },  cancelAnimation() {    this.animation.cancel(); //调用动画取消接口  },  reverseAnimation() {    this.animation.reverse(); //调用动画倒放接口  }}

开发指导—利用组件&插值器动画实现 HarmonyOS 动效,javascript,开发语言,HarmonyOS

说明

在调用 update 接口的过程中可以使用这个接口更新动画参数,入参与 createAnimator 一致。

动画帧

请求动画帧

请求动画帧时通过 requestAnimationFrame 函数逐帧回调,在调用该函数时传入一个回调函数。

runframe 在调用 requestAnimationFrame 时传入带有 timestamp 参数的回调函数 step,将 step 中的 timestamp 赋予起始的 startTime。当 timestamp 与 startTime 的差值小于规定的时间时将再次调用 requestAnimationFrame,最终动画将会停止。

<!-- xxx.hml --><div class="container">  <tabs onchange="changecontent">    <tab-content>      <div class="container">        <stack style="width: 300px;height: 300px;margin-top: 100px;margin-bottom: 100px;">          <canvas id="mycanvas" style="width: 100%;height: 100%;background-color: coral;">          </canvas>          <div style="width: 50px;height: 50px;border-radius: 25px;background-color: indigo;position: absolute;left: {{left}};top: {{top}};">          </div>        </stack>        <button type="capsule" value="play" onclick="runframe"></button>      </div>    </tab-content>  </tabs></div>

/* xxx.css */.container {  flex-direction: column;  justify-content: center;  align-items: center;  width: 100%;  height: 100%;}button{  width: 300px;}

// xxx.jsexport default {  data: {    timer: null,    left: 0,    top: 0,    flag: true,    animation: null,    startTime: 0,  },  onShow() {    var test = this.$element("mycanvas");    var ctx = test.getContext("2d");    ctx.beginPath();    ctx.moveTo(0, 0);    ctx.lineTo(300, 300);    ctx.lineWidth = 5;    ctx.strokeStyle = "red";    ctx.stroke();  },  runframe() {    this.left = 0;    this.top = 0;    this.flag = true;    this.animation = requestAnimationFrame(this.step);  },  step(timestamp) {    if (this.flag) {      this.left += 5;      this.top += 5;      if (this.startTime == 0) {        this.startTime = timestamp;      }      var elapsed = timestamp - this.startTime;        if (elapsed < 500) {          console.log('callback step timestamp: ' + timestamp);          this.animation = requestAnimationFrame(this.step);        }      } else {        this.left -= 5;        this.top -= 5;        this.animation = requestAnimationFrame(this.step);      }      if (this.left == 250 || this.left == 0) {        this.flag = !this.flag     }    },    onDestroy() {      cancelAnimationFrame(this.animation);    }}

开发指导—利用组件&插值器动画实现 HarmonyOS 动效,javascript,开发语言,HarmonyOS

说明

requestAnimationFrame 函数在调用回调函数时在第一个参数位置传入 timestamp 时间戳,表示 requestAnimationFrame 开始去执行回调函数的时刻。

取消动画帧

通过 cancelAnimationFrame 函数取消逐帧回调,在调用 cancelAnimationFrame 函数时取消 requestAnimationFrame 函数的请求。

<!-- xxx.hml --><div class="container">  <tabs onchange="changecontent">    <tab-content>      <div class="container">        <stack style="width: 300px;height: 300px;margin-top: 100px;margin-bottom: 100px;">          <canvas id="mycanvas" style="width: 100%;height: 100%;background-color: coral;">          </canvas>          <div style="width: 50px;height: 50px;border-radius: 25px;background-color: indigo;position: absolute;left: {{left}};top: {{top}};">          </div>        </stack>        <button type="capsule" value="play" onclick="runframe"></button>      </div>    </tab-content>  </tabs></div>

/* xxx.css */.container {  flex-direction: column;  justify-content: center;  align-items: center;  width: 100%;  height: 100%;}button{  width: 300px;}

// xxx.jsexport default {  data: {    timer: null,    left: 0,    top: 0,    flag: true,    animation: null  },  onShow() {    var test = this.$element("mycanvas");    var ctx = test.getContext("2d");    ctx.beginPath();    ctx.moveTo(0, 0);    ctx.lineTo(300, 300);    ctx.lineWidth = 5;    ctx.strokeStyle = "red";    ctx.stroke();  },  runframe() {    this.left = 0;    this.top = 0;    this.flag = true;    this.animation = requestAnimationFrame(this.step);  },  step(timestamp) {    if (this.flag) {      this.left += 5;      this.top += 5;      this.animation = requestAnimationFrame(this.step);    } else {      this.left -= 5;      this.top -= 5;      this.animation = requestAnimationFrame(this.step);    }    if (this.left == 250 || this.left == 0) {      this.flag = !this.flag    }  },  onDestroy() {    cancelAnimationFrame(this.animation);  }}

开发指导—利用组件&插值器动画实现 HarmonyOS 动效,javascript,开发语言,HarmonyOS

说明

在调用该函数时需传入一个具有标识 id 的参数。文章来源地址https://www.toymoban.com/news/detail-702862.html

到了这里,关于开发指导—利用组件&插值器动画实现 HarmonyOS 动效的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • HarmonyOS:NativeWindow 开发指导

    NativeWindow 是 HarmonyOS 本地平台化窗口 ,表示图形队列的生产者端。开发者可以通过 NativeWindow 接口进行申请和提交 Buffer,配置 Buffer 属性信息。 针对 NativeWindow,常见的开发场景如下: ● 通过 NativeWindow 提供的 Native API 接口申请图形 Buffer,并将生产图形内容写入图形 Buffe

    2024年02月04日
    浏览(33)
  • HarmonyOS 音频通话开发指导

    常用的音频通话模式包括 VOIP 通话和蜂窝通话。 ● VOIP 通话:VOIP(Voice over Internet Protocol)通话是指基于互联网协议(IP)进行通讯的一种语音通话技术。VOIP 通话会将通话信息打包成数据包,通过网络进行传输,因此 VOIP 通话对网络要求较高,通话质量与网络连接速度紧

    2024年02月08日
    浏览(41)
  • 鸿蒙HarmonyOS兼容JS的类Web开发-开发指导

    list开发指导 list是用来显示列表的组件,包含一系列相同宽度的列表项,适合连续、多行地呈现同类数据。具体用法请参考list API。 创建list组件 在pages/index目录下的hml文件中创建一个list组件。 说明 是的子组件,实现列表分组功能,不能再嵌套,可以嵌套。 是的子组件,展

    2024年02月02日
    浏览(37)
  • HarmonyOS音频开发指导:使用AVPlayer开发音频播放功能

    在HarmonyOS系统中,多种API都提供了音频播放开发的支持,不同的API适用于不同音频数据格式、音频资源来源、音频使用场景,甚至是不同开发语言。因此,选择合适的音频播放API,有助于降低开发工作量,实现更佳的音频播放效果。 ● AVPlayer:功能较完善的音频、视频播放

    2024年01月20日
    浏览(26)
  • 【HarmonyOS】ArkTS 组件内转场动画,动画播放时颜色异常问题

    【】 HarmonyOS、ArkTS、组件内转场动画、颜色异常 【问题描述】 根据组件内转场动画文档中示例编写代码,使用动画转场组件button,并给button设置背景色让button透明度为0,实现动画转场时,会先出现默认蓝色button,然后动画再消失,问题代码如下所示: 问题现象请见下

    2024年02月15日
    浏览(35)
  • HarmonyOS鸿蒙开发指南:基于ArkTS开发 音频渲染开发指导

    目录 场景介绍 音频中断 状态检查 异步操作 开发步骤

    2024年01月16日
    浏览(42)
  • HarmonyOS鸿蒙开发指南:基于ArkTS开发 音频录制开发指导

    目录 场景介绍 开发步骤 全流程场景 正常录制场景 音频录制的主要工作是捕获音频信号

    2024年01月19日
    浏览(56)
  • HarmonyOS 音频开发指导:使用 OpenSL ES 开发音频播放功能

    OpenSL ES 全称为 Open Sound Library for Embedded Systems,是一个嵌入式、跨平台、免费的音频处理库。为嵌入式移动多媒体设备上的应用开发者提供标准化、高性能、低延迟的 API。HarmonyOS 的 Native API 基于Khronos Group开发的OpenSL ES 1.0.1 API 规范实现,开发者可以通过OpenSLES.h和O

    2024年02月03日
    浏览(27)
  • HarmonyOS开发:NodeJs脚本实现组件化动态切换

    上篇文章,我们使用NodeJs脚本完成了HarmonyOS项目的组件化运行,但是由于脚本是基于4.0.0.400版本的DevEco Studio开发的,可能在配置文件的修改上有些许差距,那么遇到这种情况怎么办,一种是再写一套针对性的脚本文件或者在原有的脚本中增加配置版本参数,第二种就是自己

    2024年02月07日
    浏览(34)
  • HarmonyOS:Neural Network Runtime 对接 AI 推理框架开发指导

    Neural Network Runtime 作为 AI 推理引擎和加速芯片的桥梁,为 AI 推理引擎提供精简的 Native 接口,满足推理引擎通过加速芯片执行端到端推理的需求。 本文以图 1 展示的 Add 单算子模型为例,介绍 Neural Network Runtime 的开发流程。Add 算子包含两个输入、一个参数和一个输出,其

    2024年02月03日
    浏览(27)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包