鸿蒙OS 动画

2020-09-18 13:55 更新

动画分为静态动画和连续动画。

静态动画

静态动画的核心是 transform 样式,主要可以实现以下三种变换类型,一次样式设置只能实现一种类型变换。

  • translate:沿水平或垂直方向将指定组件移动所需距离。
  • scale:横向或纵向将指定组件缩小或放大到所需比例。
  • rotate:将指定组件沿横轴或纵轴或中心点旋转指定的角度。

静态动画只有开始状态和结束状态,没有中间状态,如果需要设置中间的过渡状态和转换效果,需要使用连续动画实现。具体的使用示例如下,更多信息请参考组件。

  1. <!-- xxx.hml -->
  2. <div class="container">
  3. <text class="translate">hello</text>
  4. <text class="rotate">hello</text>
  5. <text class="scale">hello</text>
  6. </div>

  1. /* xxx.css */
  2. .container {
  3. flex-direction: column;
  4. align-items: center;
  5. }
  6. .translate {
  7. height: 300px;
  8. width: 400px;
  9. font-size: 100px;
  10. background-color: #008000;
  11. transform: translate(300px);
  12. }
  13. .rotate {
  14. height: 300px;
  15. width: 400px;
  16. font-size: 100px;
  17. background-color: #008000;
  18. transform-origin: 200px 100px;
  19. transform: rotateX(45deg);
  20. }
  21. .scale {
  22. height: 300px;
  23. width: 400px;
  24. font-size: 100px;
  25. background-color: #008000;
  26. transform: scaleX(1.5);
  27. }

图1 静态动画效果图 img

连续动画

连续动画的核心是 animation 样式,它定义了动画的开始状态、结束状态以及时间和速度的变化曲线。通过 animation 样式可以实现的效果有:

  • animation-name:设置动画执行后应用到组件上的背景颜色、透明度、宽高和变换类型。
  • animation-delayanimation-duration:分别设置动画执行后元素延迟和持续的时间。
  • animation-timing-function:描述动画执行的速度曲线,使动画更加平滑。
  • animation-iteration-count:定义动画播放的次数。
  • animation-fill-mode:指定动画执行结束后是否恢复初始状态。

animation 样式需要在 css 文件中先定义 keyframe,在 keyframe 中设置动画的过渡效果,并通过一个样式类型在 hml 文件中调用。animation-name 的使用示例如下:

  1. <!-- xxx.hml -->
  2. <div class="item-container">
  3. <div class="group">
  4. <text class="header">animation-name</text>
  5. <div class="item {{colorParam}}">
  6. <text class="txt">color</text>
  7. </div>
  8. <div class="item {{opacityParam}}">
  9. <text class="txt">opacity</text>
  10. </div>
  11. <input class="button" type="button" name="" value="show" onclick="showAnimation"/>
  12. </div>
  13. </div>

  1. /* xxx.css */
  2. .item-container {
  3. margin-bottom: 50px;
  4. margin-right: 60px;
  5. margin-left: 60px;
  6. flex-direction: column;
  7. align-items: flex-start;
  8. }
  9. .group {
  10. margin-bottom: 150px;
  11. flex-direction: column;
  12. align-items: flex-start;
  13. }
  14. .header {
  15. margin-bottom: 20px;
  16. }
  17. .item {
  18. background-color: #f76160;
  19. }
  20. .txt {
  21. text-align: center;
  22. width: 200px;
  23. height: 100px;
  24. }
  25. .button {
  26. width: 200px;
  27. font-size: 30px;
  28. color: #ffffff;
  29. background-color: #09ba07;
  30. }
  31. .color {
  32. animation-name: Color;
  33. animation-duration: 8000ms;
  34. }
  35. .opacity {
  36. animation-name: Opacity;
  37. animation-duration: 8000ms;
  38. }
  39. @keyframes Color {
  40. from {
  41. background-color: #f76160;
  42. }
  43. to {
  44. background-color: #09ba07;
  45. }
  46. }
  47. @keyframes Opacity {
  48. from {
  49. opacity: 0.9;
  50. }
  51. to {
  52. opacity: 0.1;
  53. }
  54. }

  1. // xxx.js
  2. export default {
  3. data: {
  4. colorParam: '',
  5. opacityParam: '',
  6. },
  7. showAnimation: function () {
  8. this.colorParam = '';
  9. this.opacityParam = '';
  10. this.colorParam = 'color';
  11. this.opacityParam = 'opacity';
  12. },
  13. }

图2 连续动画效果图 img

以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号