鸿蒙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.动画效果如下:

相关推荐
r***11337 小时前
【MySQL】MySQL库的操作
android·数据库·mysql
在下历飞雨7 小时前
Kuikly基础之动画实战:让孤寡青蛙“活”过来
前端·ios·harmonyos
ljt27249606617 小时前
Compose笔记(五十九)--BadgedBox
android·笔记·android jetpack
用户41659673693557 小时前
ExoPlayer 播放花屏与跳跃?我们如何像 QuickTime 一样优雅处理音频时间戳错误
android
Y***h1877 小时前
MySQL不使用子查询的原因
android·数据库·mysql
p***93037 小时前
Java进阶之泛型
android·前端·后端
Yeats_Liao8 小时前
华为开源自研AI框架昇思MindSpore Lite初探: 端侧推理快速入门
人工智能·华为
马剑威(威哥爱编程)8 小时前
【鸿蒙开发实战篇】HarmonyOS 6.0 蓝牙实现服务端和客户端通讯案例详解
华为·蓝牙·harmonyos
3***16108 小时前
MySQL中ON DUPLICATE KEY UPDATE的介绍与使用、批量更新、存在即更新不存在则插入
android·数据库·mysql
大侠课堂8 小时前
嵌入式系统经典面试题100道-华为中兴篇
网络·华为·面试