鸿蒙NEXT开发动画案例2

1.创建空白项目

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

复制代码
// ===== 接口定义(必须放在使用前)=====
/**
 * 关键帧动画整体配置参数
 */
interface KeyframeAnimationConfig {
  iterations: number;
  delay: number;
}

/**
 * 单个关键帧动画项
 */
interface KeyframeItem {
  duration: number;
  curve: Curve;
  event: () => void;
}

/**
 * 动画状态更新参数
 */
interface AnimationUpdateParams {
  scale1?: number;
  scale2?: number;
}
// ===== 接口定义结束 =====

/**
 * SpinKit 风格的弹性缩放加载动画组件。
 *
 * @component
 * @param spinSize - 动画容器大小(必须为正数)
 * @param spinColor - 动画颜色(支持资源引用)
 *
 * 示例:
 * ```ets
 * SpinTwo({
 *   spinSize: 60,
 *   spinColor: '#FF0000'
 * })
 * ```
 */
@ComponentV2
export struct SpinTwo {
  // 参数定义(父组件必须传入)
  @Require @Param spinSize: number = 48;
  @Require @Param spinColor: ResourceColor = '#209ED8';

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

  // 常量定义
  private readonly ANIMATION_DURATION: number = 1000;

  build() {
    Stack() {
      Canvas()
        .scale({ x: this.scale1, y: this.scale1 })
        .bounceStyle()

      Canvas()
        .scale({ x: this.scale2, y: this.scale2 })
        .bounceStyle()
    }
    .width(this.spinSize)
    .height(this.spinSize)
    .onAppear(() => {
      this.startAnimation();
    });
  }

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

    const animationConfig: KeyframeAnimationConfig = {
      iterations: -1,
      delay: 0
    };

    uiContext.keyframeAnimateTo(animationConfig, [
      this.createKeyframe(this.ANIMATION_DURATION, { scale1: 1, scale2: 0 }),
      this.createKeyframe(this.ANIMATION_DURATION, { scale1: 0, scale2: 1 })
    ]);
  }

  /**
   * 创建关键帧动画配置项
   * @param duration - 动画持续时间
   * @param update - 更新的状态对象
   */
  private createKeyframe(
    duration: number,
    update: AnimationUpdateParams
  ): KeyframeItem {
    return {
      duration,
      curve: Curve.EaseInOut,
      event: () => {
        if (update.scale1 !== undefined) this.scale1 = update.scale1;
        if (update.scale2 !== undefined) this.scale2 = update.scale2;
      }
    };
  }

  /**
   * 公共样式封装
   */
  @Styles
  bounceStyle() {
    .width('100%')
    .height('100%')
    .opacity(0.6)
    .borderRadius(this.spinSize / 2) // 圆形效果
    .backgroundColor(this.spinColor)
  }
}
复制代码
代码如下:
TypeScript 复制代码
// ===== 接口定义(必须放在使用前)=====
/**
 * 关键帧动画整体配置参数
 */
interface KeyframeAnimationConfig {
  iterations: number;
  delay: number;
}

/**
 * 单个关键帧动画项
 */
interface KeyframeItem {
  duration: number;
  curve: Curve;
  event: () => void;
}

/**
 * 动画状态更新参数
 */
interface AnimationUpdateParams {
  scale1?: number;
  scale2?: number;
}
// ===== 接口定义结束 =====

/**
 * SpinKit 风格的弹性缩放加载动画组件。
 *
 * @component
 * @param spinSize - 动画容器大小(必须为正数)
 * @param spinColor - 动画颜色(支持资源引用)
 *
 * 示例:
 * ```ets
 * SpinTwo({
 *   spinSize: 60,
 *   spinColor: '#FF0000'
 * })
 * ```
 */
@ComponentV2
export struct SpinTwo {
  // 参数定义(父组件必须传入)
  @Require @Param spinSize: number = 48;
  @Require @Param spinColor: ResourceColor = '#209ED8';

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

  // 常量定义
  private readonly ANIMATION_DURATION: number = 1000;

  build() {
    Stack() {
      Canvas()
        .scale({ x: this.scale1, y: this.scale1 })
        .bounceStyle()

      Canvas()
        .scale({ x: this.scale2, y: this.scale2 })
        .bounceStyle()
    }
    .width(this.spinSize)
    .height(this.spinSize)
    .onAppear(() => {
      this.startAnimation();
    });
  }

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

    const animationConfig: KeyframeAnimationConfig = {
      iterations: -1,
      delay: 0
    };

    uiContext.keyframeAnimateTo(animationConfig, [
      this.createKeyframe(this.ANIMATION_DURATION, { scale1: 1, scale2: 0 }),
      this.createKeyframe(this.ANIMATION_DURATION, { scale1: 0, scale2: 1 })
    ]);
  }

  /**
   * 创建关键帧动画配置项
   * @param duration - 动画持续时间
   * @param update - 更新的状态对象
   */
  private createKeyframe(
    duration: number,
    update: AnimationUpdateParams
  ): KeyframeItem {
    return {
      duration,
      curve: Curve.EaseInOut,
      event: () => {
        if (update.scale1 !== undefined) this.scale1 = update.scale1;
        if (update.scale2 !== undefined) this.scale2 = update.scale2;
      }
    };
  }

  /**
   * 公共样式封装
   */
  @Styles
  bounceStyle() {
    .width('100%')
    .height('100%')
    .opacity(0.6)
    .borderRadius(this.spinSize / 2) // 圆形效果
    .backgroundColor(this.spinColor)
  }
}

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

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

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

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

代码如下:

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

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

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

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

5.动画效果如下:

相关推荐
用户20187928316739 分钟前
演员的智能衣橱系统之Selector选择器
android
CYRUS_STUDIO1 小时前
OLLVM 混淆 + VMP 壳照样破!绕过加壳 SDK 的核心检测逻辑
android·逆向·汇编语言
Kapaseker1 小时前
憋了一周了,12000字深入浅出Android的Context机制
android
betazhou1 小时前
MySQL ROUTER安装部署
android·数据库·mysql·adb·mgr·mysql router
用户2018792831671 小时前
🧙‍♂️ 故事:像素王国的“贴纸工厂”与“圣殿徽章工坊”
android
用户2018792831671 小时前
浅谈Android资源国王的故事
android
Harry技术1 小时前
这款 Android 智能柜系统绝了!多重验证 + 硬件联动,仪器管理超省心
android·kotlin
顾林海1 小时前
Android 性能优化:提升应用启动速度(GC抑制)
android·面试·性能优化
隼尘1 小时前
android cpp
android
安卓开发者2 小时前
Android CameraX 使用指南:简化相机开发
android·数码相机