鴻蒙OS 動畫

2020-09-18 13:55 更新

動畫分為靜態(tài)動畫和連續(xù)動畫。

靜態(tài)動畫

靜態(tài)動畫的核心是 transform 樣式,主要可以實現(xiàn)以下三種變換類型,一次樣式設(shè)置只能實現(xiàn)一種類型變換。

  • translate:沿水平或垂直方向?qū)⒅付ńM件移動所需距離。
  • scale:橫向或縱向?qū)⒅付ńM件縮小或放大到所需比例。
  • rotate:將指定組件沿橫軸或縱軸或中心點旋轉(zhuǎn)指定的角度。

靜態(tài)動畫只有開始狀態(tài)和結(jié)束狀態(tài),沒有中間狀態(tài),如果需要設(shè)置中間的過渡狀態(tài)和轉(zhuǎn)換效果,需要使用連續(xù)動畫實現(xiàn)。具體的使用示例如下,更多信息請參考組件。

  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 靜態(tài)動畫效果圖 img

連續(xù)動畫

連續(xù)動畫的核心是 animation 樣式,它定義了動畫的開始狀態(tài)、結(jié)束狀態(tài)以及時間和速度的變化曲線。通過 animation 樣式可以實現(xiàn)的效果有:

  • animation-name:設(shè)置動畫執(zhí)行后應(yīng)用到組件上的背景顏色、透明度、寬高和變換類型。
  • animation-delayanimation-duration:分別設(shè)置動畫執(zhí)行后元素延遲和持續(xù)的時間。
  • animation-timing-function:描述動畫執(zhí)行的速度曲線,使動畫更加平滑。
  • animation-iteration-count:定義動畫播放的次數(shù)。
  • animation-fill-mode:指定動畫執(zhí)行結(jié)束后是否恢復(fù)初始狀態(tài)。

animation 樣式需要在 css 文件中先定義 keyframe,在 keyframe 中設(shè)置動畫的過渡效果,并通過一個樣式類型在 hml 文件中調(diào)用。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 連續(xù)動畫效果圖 img

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號