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

相关推荐
陈大头铃儿响叮当19 分钟前
Android Studio升级后,Flutter运行android设备报错
android·flutter·android studio
勤劳打代码34 分钟前
isar_flutter_libs 引发 Namespace not specified
android·flutter·groovy
m0_685535081 小时前
手机镜头设计
华为·光学·光学设计·光学工程·镜头设计
爱笑的眼睛112 小时前
HarmonyOS Marquee组件深度解析:构建高性能滚动视觉效果
华为·harmonyos
奔跑吧 android2 小时前
【android bluetooth 协议分析 18】【PBAP详解 2】【车机为何不显示电话号码为空的联系人信息】
android·蓝牙电话·hfp·pbap·电话簿
深盾科技2 小时前
安卓二次打包技术深度拆解:从逆向篡改到防护逻辑
android
4Forsee2 小时前
【Android】消息机制
android·java·前端
不爱说话郭德纲2 小时前
UniappX不会运行到鸿蒙?超超超保姆级鸿蒙开发生成证书以及配置证书步骤
前端·uni-app·harmonyos
m0_685535082 小时前
大疆无人机镜头
华为·光学·光学设计·光学工程·镜头设计
m0_685535082 小时前
无人机镜头设计实例
华为·光学·光学设计·光学工程·镜头设计