成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

如何使用彈簧動畫曲線

系統 OpenHarmony
OpenHarmony提供了三種彈簧動畫曲線用來實現彈性效果,本例將為大家介紹這三種曲線的用法。

想了解更多關于開源的內容,請訪問:

51CTO 開源基礎軟件社區

https://ost.51cto.com

場景說明

在動畫開發場景中,經常用到彈性效果,尤其在拖拽某個對象時經常伴隨彈性動效。OpenHarmony提供了三種彈簧動畫曲線用來實現彈性效果,本例將為大家介紹這三種曲線的用法。

效果呈現

本例最終效果如下:

如何使用彈簧動畫曲線-開源基礎軟件社區

運行環境

本例基于以下環境開發,開發者也可以基于其他適配的版本進行開發:

  • IDE: DevEco Studio 3.1 Beta2
  • SDK: Ohos_sdk_public 3.2.11.9(API Version 9 Release)

實現思路

本例主要用到以下三種彈簧動畫曲線:

  • curves.springCurve:通過設置彈簧的初始速度、質量、剛度和阻尼來控制彈簧動畫的效果。對應本例中springCurve按鈕觸發的動畫。
  • curves.springMotion:通過設置彈簧震動時間和阻尼來控制彈簧動畫的效果。對應本例中springMotion按鈕觸發的動畫。
  • curves.responsiveSpringMotion:構造彈性跟手動畫曲線對象,是springMotion的一種特例,僅默認參數不同,可與springMotion混合使用。用來實現拖拽動畫。

開發步驟

搭建UI框架。
樣例中有兩個按鈕,一個圖片。內容整體縱向分布,兩個按鈕橫向分布。縱向布局可以采用Column組件,橫向布局可以采用Row組件。代碼如下:

@Entry
@Component
struct ImageComponent {
  build() {
    Column() {
      Row() {
        Button('springCurve')
          .margin({right:10})
          .fontSize(20)
          .backgroundColor('#18183C')
        Button('springMotion')
          .fontSize(20)
          .backgroundColor('#18183C')
      }
      .margin({top:30})

      Image($r("app.media.contact2"))
        .width(100)
        .height(100)
    }.width("100%").height("100%").backgroundColor('#A4AE77')
  }
}

為springCurve按鈕添加curves.springCurve的曲線動畫。

...
// 定義狀態變量translateY,用來控制笑臉圖像的位移
@State translateY: number = 0
	...
    Button('springCurve')
      .margin({right:10})
      .fontSize(20)
      .backgroundColor('#18183C')
      // 綁定點擊事件
      .onClick(() => {
        // 在點擊事件中添加顯示動畫
        animateTo({
          duration: 2000,
          // 設定curves.springCurve為動畫曲線
          curve: curves.springCurve(100, 10, 80, 10)
        },
        () => {
          // 改變translateY的值,使笑臉圖像發生位移
          this.translateY = -20
        })
        this.translateY = 0
      })
	...
    Image($r("app.media.contact2"))
      .width(100)
      .height(100)
      // 為笑臉圖像添加位移屬性,以translateY為參數
      .translate({ y: this.translateY })
	...

效果如下:

如何使用彈簧動畫曲線-開源基礎軟件社區

為springMotion按鈕添加curves.springMotion曲線動畫。
這里通過position屬性控制springMotion按鈕的移動,當然開發者也可以繼續選擇使用translate屬性。

...
  // 定義狀態變量translateY,用來控制笑臉圖像的位置變化
  @State imgPos: {
    x: number,
    y: number
  } = { x: 125, y: 400 }
        ...
        Button('springMotion')
          .fontSize(20)
          .backgroundColor('#18183C')
          // 綁定點擊事件
          .onClick(() => {
          // 在點擊事件中添加顯示動畫
          animateTo({
            duration: 15,
            //設定curves.springMotion為動畫曲線
            curve: curves.springMotion(0.5, 0.5),
            onFinish: () => {
              animateTo({ duration: 500,
                curve: curves.springMotion(0.5, 0.5), }, () => {
                // 動畫結束時笑臉圖像位置還原
                this.imgPos = { x: 125, y: 400 }
              })
            }
          }, () => {
            // 改變笑臉圖像位置,y軸位置由400,變為150
            this.imgPos = { x: 125, y: 150 }
          })
        })
      ...
      Image($r("app.media.contact2"))
        .width(100)
        .height(100)
        .translate({ y: this.translateY })
        // 為笑臉圖像添加位置屬性,以imgPos為參數
        .position(this.imgPos)
     ...

效果如下:

如何使用彈簧動畫曲線-開源基礎軟件社區

使用curves.responsiveSpringMotion為笑臉圖像添加拖拽動畫。

...
      Image($r("app.media.contact2"))
        .width(100)
        .height(100)
        .translate({ y: this.translateY })
        .position(this.imgPos)
        // 綁定觸摸事件
        .onTouch((event: TouchEvent) => {
          // 當觸摸放開時,笑臉圖像位置還原
          if (event.type == TouchType.Up) {
            animateTo({
              duration: 50,
              delay: 0,
              curve: curves.springMotion(),
              onFinish: () => {
              }
            }, () => {
              this.imgPos = { x: 125, y: 400 }
            })
          } else {
            // 觸摸過程中觸發跟手動畫
            animateTo({
              duration: 50,
              delay: 0,
              //設定跟手動畫曲線
              curve: curves.responsiveSpringMotion(),
              onFinish: () => {
              }
            }, () => {
              // 根據觸點位置改變笑臉圖像位置,從而實現跟手動畫
              this.imgPos = {
                x: event.touches[0].screenX - 100 / 2,
                y: event.touches[0].screenY - 100 / 2
              }
            })
          }
        })
...

效果如下:

如何使用彈簧動畫曲線-開源基礎軟件社區

完整代碼

本例完整代碼如下:

import curves from '@ohos.curves';

@Entry
@Component
struct ImageComponent {
  // 定義狀態變量translateY,用來控制笑臉圖像的位移
  @State translateY: number = 0
  // 定義狀態變量translateY,用來控制笑臉圖像的位置變化
  @State imgPos: {
    x: number,
    y: number
  } = { x: 125, y: 400 }

  build() {
    Column() {
      Row() {
        Button('springCurve')
          .margin({right:10})
          .fontSize(20)
          .backgroundColor('#18183C')
          // 綁定點擊事件
          .onClick(() => {
            // 在點擊事件中添加顯示動畫
            animateTo({
              duration: 2000,
              // 設定curves.springCurve為動畫曲線
              curve: curves.springCurve(100, 10, 80, 10)
            },
            () => {
              // 改變translateY的值,使笑臉圖像發生位移
              this.translateY = -20
            })
            this.translateY = 0
          })
        Button('springMotion')
          .fontSize(20)
          .backgroundColor('#18183C')
          // 綁定點擊事件
          .onClick(() => {
            // 在點擊事件中添加顯示動畫
            animateTo({
              duration: 15,
              //設定curves.springMotion為動畫曲線
              curve: curves.springMotion(0.5, 0.5),
              onFinish: () => {
                animateTo({ duration: 500,
                  curve: curves.springMotion(0.5, 0.5), }, () => {
                  // 動畫結束時笑臉圖像位置還原
                  this.imgPos = { x: 125, y: 400 }
                })
              }
            }, () => {
              // 改變笑臉圖像位置,y軸位置由400,變為150
              this.imgPos = { x: 125, y: 150 }
            })
        })
      }
      .margin({top:30})

      Image($r("app.media.contact2"))
        .width(100)
        .height(100)
        // 為笑臉圖像添加位移屬性,以translateY為參數
        .translate({ y: this.translateY })
        // 為笑臉圖像添加位置屬性,以imgPos為參數
        .position(this.imgPos)
        // 綁定觸摸事件
        .onTouch((event: TouchEvent) => {
          // 當觸摸放開時,笑臉圖像位置還原
          if (event.type == TouchType.Up) {
            animateTo({
              duration: 50,
              delay: 0,
              curve: curves.springMotion(),
              onFinish: () => {
              }
            }, () => {
              this.imgPos = { x: 125, y: 400 }
            })
          } else {
            // 觸摸過程中觸發跟手動畫,同樣通過animateTo實現動畫效果
            animateTo({
              duration: 50,
              delay: 0,
              //設定跟手動畫曲線
              curve: curves.responsiveSpringMotion(),
              onFinish: () => {
              }
            }, () => {
              // 根據觸點位置改變笑臉圖像位置,從而實現跟手動畫
              this.imgPos = {
                x: event.touches[0].screenX - 100 / 2,
                y: event.touches[0].screenY - 100 / 2
              }
            })
          }
        })
    }.width("100%").height("100%").backgroundColor('#A4AE77')
  }
}

想了解更多關于開源的內容,請訪問:

51CTO 開源基礎軟件社區

https://ost.51cto.com

責任編輯:jianghua 來源: 51CTO 開源基礎軟件社區
相關推薦

2021-06-01 09:27:52

視頻動畫Remotion

2010-12-01 11:03:20

職場

2009-09-22 12:59:58

ibmdwDojo

2022-11-23 08:17:18

CSS動畫貝塞爾

2021-07-27 05:53:00

Chrome瀏覽器KPI

2017-04-27 20:30:33

Android動畫技巧

2017-03-28 13:00:10

LinuxGifineGIF動畫

2014-04-08 17:35:24

iOS 7彈簧式列表

2023-06-05 09:28:32

CSS漸變

2021-12-15 19:22:38

原理View動畫

2021-12-20 20:30:48

鴻蒙HarmonyOS應用

2020-07-09 13:10:42

GIMP曲線文本應用

2023-09-13 14:45:14

性能測試開發

2014-05-30 09:44:08

Android折紙動畫

2021-05-11 08:19:00

CSS 文字動畫技巧

2021-02-21 08:12:24

SVG線條動畫Web動畫

2012-12-24 13:38:01

iOSUIView

2018-10-10 09:00:00

前端框架Angular

2021-01-04 11:10:14

鴻蒙HarmonyOSCanvas

2023-04-27 09:55:09

分類器ROC曲線混淆矩陣
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 成人国产精品免费观看视频 | www.99久久.com | 一区二区三区免费 | 久久新视频 | 在线视频亚洲 | 91精品国产自产精品男人的天堂 | 国产午夜精品理论片a大结局 | 国产精品久久久久久亚洲调教 | 黄色一级片视频 | 国产精品区一区二区三区 | 欧美精品a∨在线观看不卡 欧美日韩中文字幕在线播放 | 三级视频久久 | 日本视频免费 | 精品日韩一区二区 | 中文字幕国产 | 三级在线免费观看 | 在线视频日韩 | www.97国产| 日韩欧美一区二区三区免费观看 | 成人毛片网站 | 欧美久久精品一级c片 | 911精品美国片911久久久 | 国产一区二区欧美 | www在线视频 | 亚洲视屏 | 亚洲国产91 | 欧美黄在线观看 | 中国大陆高清aⅴ毛片 | av免费在线播放 | 亚洲欧美日韩精品久久亚洲区 | 日韩超碰| 国产精品久久一区 | 羞羞网站在线观看 | 免费黄色大片 | 99国产精品久久久 | 性高湖久久久久久久久3小时 | 亚洲va中文字幕 | 国产亚洲精品久久久久动 | 成人h动漫精品一区二区器材 | 欧美日韩中文在线 | 欧美日韩视频 |