鸿蒙NEXT开发动画案例5

1.创建空白项目

2.Page文件夹下面新建Spin.ets文件,代码如下:

复制代码
/**
 * TODO SpinKit动画组件 - Pulse 脉冲动画
 * author: CSDN—鸿蒙布道师
 * since: 2024/05/09
 */
@ComponentV2
export struct SpinFive {
  // 参数定义
  @Require @Param spinSize: number = 48;
  @Require @Param spinColor: ResourceColor = '#209ED8';

  // 局部状态
  @Local scale1: number = 1;
  @Local opacity1: number = 1;

  build() {
    Canvas()
      .width(this.spinSize)
      .height(this.spinSize)
      .borderRadius(this.spinSize)
      .backgroundColor(this.spinColor)
      .renderFit(RenderFit.CENTER)
      .scale({ x: this.scale1, y: this.scale1 })
      .opacity(this.opacity1)
      .onAppear(() => {
        this.startAnimation();
      })
  }

  /**
   * 启动无限循环的关键帧动画
   */
  private startAnimation(): void {
    const uiContext = this.getUIContext();
    if (!uiContext) return;

    const keyframes: Array<KeyframeState> = [
      {
        duration: 0,
        curve: Curve.EaseInOut,
        event: (): void => {
          this.scale1 = 0;
          this.opacity1 = 1;
        },
      },
      {
        duration: 1000,
        curve: Curve.EaseInOut,
        event: (): void => {
          this.scale1 = 1.0;
          this.opacity1 = 0.01;
        },
      }
    ];

    uiContext.keyframeAnimateTo(
      { iterations: -1, delay: 0 },
      keyframes
    );
  }
}
复制代码
代码如下:
TypeScript 复制代码
/**
 * TODO SpinKit动画组件 - Pulse 脉冲动画
 * author: CSDN---鸿蒙布道师
 * since: 2024/05/09
 */
@ComponentV2
export struct SpinFive {
  // 参数定义
  @Require @Param spinSize: number = 48;
  @Require @Param spinColor: ResourceColor = '#209ED8';

  // 局部状态
  @Local scale1: number = 1;
  @Local opacity1: number = 1;

  build() {
    Canvas()
      .width(this.spinSize)
      .height(this.spinSize)
      .borderRadius(this.spinSize)
      .backgroundColor(this.spinColor)
      .renderFit(RenderFit.CENTER)
      .scale({ x: this.scale1, y: this.scale1 })
      .opacity(this.opacity1)
      .onAppear(() => {
        this.startAnimation();
      })
  }

  /**
   * 启动无限循环的关键帧动画
   */
  private startAnimation(): void {
    const uiContext = this.getUIContext();
    if (!uiContext) return;

    const keyframes: Array<KeyframeState> = [
      {
        duration: 0,
        curve: Curve.EaseInOut,
        event: (): void => {
          this.scale1 = 0;
          this.opacity1 = 1;
        },
      },
      {
        duration: 1000,
        curve: Curve.EaseInOut,
        event: (): void => {
          this.scale1 = 1.0;
          this.opacity1 = 0.01;
        },
      }
    ];

    uiContext.keyframeAnimateTo(
      { iterations: -1, delay: 0 },
      keyframes
    );
  }
}

3.修改Index.ets文件,代码如下:

复制代码
import { SpinFive } from './Spin';

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';

  build() {
    Column() {
      SpinFive({
        spinSize: 60,
        spinColor: '#FF0000'
      })
    }
    .alignItems(HorizontalAlign.Center)
    .justifyContent(FlexAlign.Center)
    .height('100%')
    .width('100%')
  }
}

代码如下:
TypeScript 复制代码
import { SpinFive } from './Spin';

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';

  build() {
    Column() {
      SpinFive({
        spinSize: 60,
        spinColor: '#FF0000'
      })
    }
    .alignItems(HorizontalAlign.Center)
    .justifyContent(FlexAlign.Center)
    .height('100%')
    .width('100%')
  }
}

4.运行项目,登录华为账号,需进行签名

5.动画效果如下:

相关推荐
程序员码歌1 小时前
【零代码AI编程实战】AI灯塔导航-总结篇
android·前端·后端
zhanshuo2 小时前
鸿蒙 ArkTS 自定义组件全攻略:从按钮到商品卡片一步步搞定
harmonyos
zhanshuo2 小时前
HarmonyOS 推送通知开发实战:从权限申请到多场景应用的完整指南
harmonyos
书弋江山2 小时前
flutter 跨平台编码库 protobuf 工具使用
android·flutter
来来走走5 小时前
Flutter开发 webview_flutter的基本使用
android·flutter
Jerry说前后端5 小时前
Android 组件封装实践:从解耦到架构演进
android·前端·架构
louisgeek6 小时前
Android OkHttp Interceptor
android
大王派来巡山的小旋风6 小时前
Kotlin基本用法三
android·kotlin
Jerry说前后端7 小时前
Android 移动端 UI 设计:前端常用设计原则总结
android·前端·ui
bytebeats7 小时前
Jetpack Compose 1.9: 核心新特性简介
android·android jetpack