鴻蒙OS 動(dòng)畫(huà)

2020-09-18 13:55 更新

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

靜態(tài)動(dòng)畫(huà)

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

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

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

<!-- xxx.hml -->
<div class="container">
  <text class="translate">hello</text>
  <text class="rotate">hello</text>
  <text class="scale">hello</text>
</div>

/* xxx.css */
.container {
  flex-direction: column;
  align-items: center;
}
.translate {
  height: 300px;
  width: 400px;
  font-size: 100px;
  background-color: #008000;
  transform: translate(300px);
}
.rotate {
  height: 300px;
  width: 400px;
  font-size: 100px;
  background-color: #008000;
  transform-origin: 200px 100px;
  transform: rotateX(45deg);
}
.scale {
  height: 300px;
  width: 400px;
  font-size: 100px;
  background-color: #008000;
  transform: scaleX(1.5);
}

圖1 靜態(tài)動(dòng)畫(huà)效果圖 img

連續(xù)動(dòng)畫(huà)

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

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

animation 樣式需要在 css 文件中先定義 keyframe,在 keyframe 中設(shè)置動(dòng)畫(huà)的過(guò)渡效果,并通過(guò)一個(gè)樣式類(lèi)型在 hml 文件中調(diào)用。animation-name 的使用示例如下:

<!-- xxx.hml -->
<div class="item-container">
  <div class="group">
    <text class="header">animation-name</text>
    <div class="item {{colorParam}}">
      <text class="txt">color</text>
    </div>
    <div class="item {{opacityParam}}">
      <text class="txt">opacity</text>
    </div>
    <input class="button" type="button" name="" value="show" onclick="showAnimation"/>
  </div>
</div>

/* xxx.css */
.item-container {
  margin-bottom: 50px;
  margin-right: 60px;
  margin-left: 60px;
  flex-direction: column;
  align-items: flex-start;
}
.group {
  margin-bottom: 150px;
  flex-direction: column;
  align-items: flex-start;
}
.header {
  margin-bottom: 20px;
}
.item {
  background-color: #f76160;
}
.txt {
  text-align: center;
  width: 200px;
  height: 100px;
}
.button {
  width: 200px;
  font-size: 30px;
  color: #ffffff;
  background-color: #09ba07;
}
.color {
  animation-name: Color;
  animation-duration: 8000ms;
}
.opacity {
  animation-name: Opacity;
  animation-duration: 8000ms;
}
@keyframes Color {
  from {
    background-color: #f76160;
  }
  to {
    background-color: #09ba07;
  }
}
@keyframes Opacity {
  from {
    opacity: 0.9;
  }
  to {
    opacity: 0.1;
  }
}

// xxx.js
export default {
  data: {
    colorParam: '',
    opacityParam: '',
  },
  showAnimation: function () {
    this.colorParam = '';
    this.opacityParam = '';
    this.colorParam = 'color';
    this.opacityParam = 'opacity';
  },
}

圖2 連續(xù)動(dòng)畫(huà)效果圖 img

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

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)