鸿蒙NEXT开发动画案例2

打印 上一主题 下一主题

主题 1729|帖子 1729|积分 5187

1.创建空缺项目


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


  1. // ===== 接口定义(必须放在使用前)=====
  2. /**
  3. * 关键帧动画整体配置参数
  4. */
  5. interface KeyframeAnimationConfig {
  6.   iterations: number;
  7.   delay: number;
  8. }
  9. /**
  10. * 单个关键帧动画项
  11. */
  12. interface KeyframeItem {
  13.   duration: number;
  14.   curve: Curve;
  15.   event: () => void;
  16. }
  17. /**
  18. * 动画状态更新参数
  19. */
  20. interface AnimationUpdateParams {
  21.   scale1?: number;
  22.   scale2?: number;
  23. }
  24. // ===== 接口定义结束 =====
  25. /**
  26. * SpinKit 风格的弹性缩放加载动画组件。
  27. *
  28. * @component
  29. * @param spinSize - 动画容器大小(必须为正数)
  30. * @param spinColor - 动画颜色(支持资源引用)
  31. *
  32. * 示例:
  33. * ```ets
  34. * SpinTwo({
  35. *   spinSize: 60,
  36. *   spinColor: '#FF0000'
  37. * })
  38. * ```
  39. */
  40. @ComponentV2
  41. export struct SpinTwo {
  42.   // 参数定义(父组件必须传入)
  43.   @Require @Param spinSize: number = 48;
  44.   @Require @Param spinColor: ResourceColor = '#209ED8';
  45.   // 局部状态
  46.   @Local scale1: number = 0;
  47.   @Local scale2: number = 1;
  48.   // 常量定义
  49.   private readonly ANIMATION_DURATION: number = 1000;
  50.   build() {
  51.     Stack() {
  52.       Canvas()
  53.         .scale({ x: this.scale1, y: this.scale1 })
  54.         .bounceStyle()
  55.       Canvas()
  56.         .scale({ x: this.scale2, y: this.scale2 })
  57.         .bounceStyle()
  58.     }
  59.     .width(this.spinSize)
  60.     .height(this.spinSize)
  61.     .onAppear(() => {
  62.       this.startAnimation();
  63.     });
  64.   }
  65.   /**
  66.    * 启动无限循环的关键帧动画
  67.    */
  68.   private startAnimation(): void {
  69.     const uiContext = this.getUIContext();
  70.     if (!uiContext) return;
  71.     const animationConfig: KeyframeAnimationConfig = {
  72.       iterations: -1,
  73.       delay: 0
  74.     };
  75.     uiContext.keyframeAnimateTo(animationConfig, [
  76.       this.createKeyframe(this.ANIMATION_DURATION, { scale1: 1, scale2: 0 }),
  77.       this.createKeyframe(this.ANIMATION_DURATION, { scale1: 0, scale2: 1 })
  78.     ]);
  79.   }
  80.   /**
  81.    * 创建关键帧动画配置项
  82.    * @param duration - 动画持续时间
  83.    * @param update - 更新的状态对象
  84.    */
  85.   private createKeyframe(
  86.     duration: number,
  87.     update: AnimationUpdateParams
  88.   ): KeyframeItem {
  89.     return {
  90.       duration,
  91.       curve: Curve.EaseInOut,
  92.       event: () => {
  93.         if (update.scale1 !== undefined) this.scale1 = update.scale1;
  94.         if (update.scale2 !== undefined) this.scale2 = update.scale2;
  95.       }
  96.     };
  97.   }
  98.   /**
  99.    * 公共样式封装
  100.    */
  101.   @Styles
  102.   bounceStyle() {
  103.     .width('100%')
  104.     .height('100%')
  105.     .opacity(0.6)
  106.     .borderRadius(this.spinSize / 2) // 圆形效果
  107.     .backgroundColor(this.spinColor)
  108.   }
  109. }
复制代码
  1. <strong>代码如下:</strong>
复制代码
  1. // ===== 接口定义(必须放在使用前)=====
  2. /**
  3. * 关键帧动画整体配置参数
  4. */
  5. interface KeyframeAnimationConfig {
  6.   iterations: number;
  7.   delay: number;
  8. }
  9. /**
  10. * 单个关键帧动画项
  11. */
  12. interface KeyframeItem {
  13.   duration: number;
  14.   curve: Curve;
  15.   event: () => void;
  16. }
  17. /**
  18. * 动画状态更新参数
  19. */
  20. interface AnimationUpdateParams {
  21.   scale1?: number;
  22.   scale2?: number;
  23. }
  24. // ===== 接口定义结束 =====
  25. /**
  26. * SpinKit 风格的弹性缩放加载动画组件。
  27. *
  28. * @component
  29. * @param spinSize - 动画容器大小(必须为正数)
  30. * @param spinColor - 动画颜色(支持资源引用)
  31. *
  32. * 示例:
  33. * ```ets
  34. * SpinTwo({
  35. *   spinSize: 60,
  36. *   spinColor: '#FF0000'
  37. * })
  38. * ```
  39. */
  40. @ComponentV2
  41. export struct SpinTwo {
  42.   // 参数定义(父组件必须传入)
  43.   @Require @Param spinSize: number = 48;
  44.   @Require @Param spinColor: ResourceColor = '#209ED8';
  45.   // 局部状态
  46.   @Local scale1: number = 0;
  47.   @Local scale2: number = 1;
  48.   // 常量定义
  49.   private readonly ANIMATION_DURATION: number = 1000;
  50.   build() {
  51.     Stack() {
  52.       Canvas()
  53.         .scale({ x: this.scale1, y: this.scale1 })
  54.         .bounceStyle()
  55.       Canvas()
  56.         .scale({ x: this.scale2, y: this.scale2 })
  57.         .bounceStyle()
  58.     }
  59.     .width(this.spinSize)
  60.     .height(this.spinSize)
  61.     .onAppear(() => {
  62.       this.startAnimation();
  63.     });
  64.   }
  65.   /**
  66.    * 启动无限循环的关键帧动画
  67.    */
  68.   private startAnimation(): void {
  69.     const uiContext = this.getUIContext();
  70.     if (!uiContext) return;
  71.     const animationConfig: KeyframeAnimationConfig = {
  72.       iterations: -1,
  73.       delay: 0
  74.     };
  75.     uiContext.keyframeAnimateTo(animationConfig, [
  76.       this.createKeyframe(this.ANIMATION_DURATION, { scale1: 1, scale2: 0 }),
  77.       this.createKeyframe(this.ANIMATION_DURATION, { scale1: 0, scale2: 1 })
  78.     ]);
  79.   }
  80.   /**
  81.    * 创建关键帧动画配置项
  82.    * @param duration - 动画持续时间
  83.    * @param update - 更新的状态对象
  84.    */
  85.   private createKeyframe(
  86.     duration: number,
  87.     update: AnimationUpdateParams
  88.   ): KeyframeItem {
  89.     return {
  90.       duration,
  91.       curve: Curve.EaseInOut,
  92.       event: () => {
  93.         if (update.scale1 !== undefined) this.scale1 = update.scale1;
  94.         if (update.scale2 !== undefined) this.scale2 = update.scale2;
  95.       }
  96.     };
  97.   }
  98.   /**
  99.    * 公共样式封装
  100.    */
  101.   @Styles
  102.   bounceStyle() {
  103.     .width('100%')
  104.     .height('100%')
  105.     .opacity(0.6)
  106.     .borderRadius(this.spinSize / 2) // 圆形效果
  107.     .backgroundColor(this.spinColor)
  108.   }
  109. }
复制代码
3.修改Index.ets文件,代码如下:
  1. import { SpinTwo } from './Spin';
  2. @Entry
  3. @Component
  4. struct Index {
  5.   @State message: string = 'Hello World';
  6.   build() {
  7.     Column() {
  8.       SpinTwo({
  9.         spinSize: 60,
  10.         spinColor: '#FF0000'
  11.       })
  12.     }
  13.     .alignItems(HorizontalAlign.Center)
  14.     .justifyContent(FlexAlign.Center)
  15.     .height('100%')
  16.     .width('100%')
  17.   }
  18. }
复制代码

<strong>代码如下:</strong>
  1. import { SpinTwo } from './Spin';
  2. @Entry
  3. @Component
  4. struct Index {
  5.   @State message: string = 'Hello World';
  6.   build() {
  7.     Column() {
  8.       SpinTwo({
  9.         spinSize: 60,
  10.         spinColor: '#FF0000'
  11.       })
  12.     }
  13.     .alignItems(HorizontalAlign.Center)
  14.     .justifyContent(FlexAlign.Center)
  15.     .height('100%')
  16.     .width('100%')
  17.   }
  18. }
复制代码
4.运行项目,登录华为账号,需进行署名

5.动画效果如下:



免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

水军大提督

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表