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

相关推荐
不吃凉粉20 小时前
Android Studio USB串口通信
android·ide·android studio
zhangphil20 小时前
android studio设置大内存,提升编译速度
android·android studio
编程乐学21 小时前
安卓非原创--基于Android Studio 实现的天气预报App
android·ide·android studio·课程设计·大作业·天气预报·安卓大作业
大熊的瓜地1 天前
Android automotive 框架
android·android car
前端世界1 天前
HarmonyOS应用开发指南:Toast无法显示的完整排查流程与实战案例
华为·harmonyos
私人珍藏库1 天前
[Android] Alarm Clock Pro 11.1.0一款经典简约个性的时钟
android·时钟
消失的旧时光-19431 天前
ScheduledExecutorService
android·java·开发语言
安卓开发者1 天前
鸿蒙NEXT Wear Engine穿戴侧应用开发完全指南
ubuntu·华为·harmonyos
安卓开发者1 天前
鸿蒙Next振动开发指南:打造沉浸式触觉反馈体验
华为·harmonyos
Devil枫1 天前
HarmonyOS屏幕方向适配指南
华为·harmonyos