当前位置: 首页 > news >正文

鸿蒙NEXT开发动画案例3

 1.创建空白项目


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

// ===== 接口定义(必须放在使用前)=====
interface KeyframeAnimationConfig {iterations: number;delay: number;
}interface KeyframeState {duration: number;curve?: Curve;event: () => void;
}
// ===== 接口定义结束 =====/*** TODO SpinKit动画组件* author: 鸿蒙布道师* since: 2025/05/07*/
@ComponentV2
export struct SpinThree {// 参数定义@Require @Param spinSize: number = 48;@Require @Param spinColor: ResourceColor = '#209ED8';// 动画状态@Local scale1: number = 0.4;@Local scale2: number = 0.4;@Local scale3: number = 0.4;@Local scale4: number = 0.4;@Local scale5: number = 0.4;build() {Row() {Canvas().chunkStyle().scale({ y: this.scale1 })Canvas().chunkStyle().scale({ y: this.scale2 })Canvas().chunkStyle().scale({ y: this.scale3 })Canvas().chunkStyle().scale({ y: this.scale4 })Canvas().chunkStyle().scale({ y: this.scale5 })}.width(this.spinSize).height(this.spinSize * 0.8).onAppear(() => {this.startAnimations();});}/*** 启动所有子动画*/private startAnimations(): void {const uiContext = this.getUIContext();if (!uiContext) return;const ANIMATION_DELAY_INTERVAL = 100; // 每个动画延迟间隔for (let i = 1; i <= 5; i++) {const keyframes = this.createKeyframes(i);const delay = -1100 + (i - 1) * ANIMATION_DELAY_INTERVAL;uiContext.keyframeAnimateTo({ iterations: -1, delay },keyframes);}}/*** 根据索引创建对应的关键帧动画* @param index - 第几个动画(1~5)*/private createKeyframes(index: number): Array<KeyframeState> {const updateScale = (value: number) => {switch (index) {case 1: this.scale1 = value; break;case 2: this.scale2 = value; break;case 3: this.scale3 = value; break;case 4: this.scale4 = value; break;case 5: this.scale5 = value; break;}};return [{duration: 240,curve: Curve.EaseInOut,event: ():void => updateScale(1),},{duration: 240,curve: Curve.EaseInOut,event: ():void => updateScale(0.4),},{duration: 720,event: () => {},}];}/*** 公共样式封装*/@StyleschunkStyle() {.height('100%').width('14%').margin({ left: '3%', right: '3%' }).backgroundColor(this.spinColor).shadow(ShadowStyle.OUTER_DEFAULT_XS)}
}
代码如下:
// ===== 接口定义(必须放在使用前)=====
interface KeyframeAnimationConfig {iterations: number;delay: number;
}interface KeyframeState {duration: number;curve?: Curve;event: () => void;
}
// ===== 接口定义结束 =====/*** TODO SpinKit动画组件* author: 鸿蒙布道师* since: 2025/05/07*/
@ComponentV2
export struct SpinThree {// 参数定义@Require @Param spinSize: number = 48;@Require @Param spinColor: ResourceColor = '#209ED8';// 动画状态@Local scale1: number = 0.4;@Local scale2: number = 0.4;@Local scale3: number = 0.4;@Local scale4: number = 0.4;@Local scale5: number = 0.4;build() {Row() {Canvas().chunkStyle().scale({ y: this.scale1 })Canvas().chunkStyle().scale({ y: this.scale2 })Canvas().chunkStyle().scale({ y: this.scale3 })Canvas().chunkStyle().scale({ y: this.scale4 })Canvas().chunkStyle().scale({ y: this.scale5 })}.width(this.spinSize).height(this.spinSize * 0.8).onAppear(() => {this.startAnimations();});}/*** 启动所有子动画*/private startAnimations(): void {const uiContext = this.getUIContext();if (!uiContext) return;const ANIMATION_DELAY_INTERVAL = 100; // 每个动画延迟间隔for (let i = 1; i <= 5; i++) {const keyframes = this.createKeyframes(i);const delay = -1100 + (i - 1) * ANIMATION_DELAY_INTERVAL;uiContext.keyframeAnimateTo({ iterations: -1, delay },keyframes);}}/*** 根据索引创建对应的关键帧动画* @param index - 第几个动画(1~5)*/private createKeyframes(index: number): Array<KeyframeState> {const updateScale = (value: number) => {switch (index) {case 1: this.scale1 = value; break;case 2: this.scale2 = value; break;case 3: this.scale3 = value; break;case 4: this.scale4 = value; break;case 5: this.scale5 = value; break;}};return [{duration: 240,curve: Curve.EaseInOut,event: ():void => updateScale(1),},{duration: 240,curve: Curve.EaseInOut,event: ():void => updateScale(0.4),},{duration: 720,event: () => {},}];}/*** 公共样式封装*/@StyleschunkStyle() {.height('100%').width('14%').margin({ left: '3%', right: '3%' }).backgroundColor(this.spinColor).shadow(ShadowStyle.OUTER_DEFAULT_XS)}
}

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

import { SpinThree } from './Spin';@Entry
@Component
struct Index {@State message: string = 'Hello World';build() {Column() {SpinThree({spinSize: 60,spinColor: '#FF0000'})}.alignItems(HorizontalAlign.Center).justifyContent(FlexAlign.Center).height('100%').width('100%')}
}


代码如下:

import { SpinThree } from './Spin';@Entry
@Component
struct Index {@State message: string = 'Hello World';build() {Column() {SpinThree({spinSize: 60,spinColor: '#FF0000'})}.alignItems(HorizontalAlign.Center).justifyContent(FlexAlign.Center).height('100%').width('100%')}
}

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

5.动画效果如下:

相关文章:

  • python-71-基于pyecharts的通用绘图流程
  • Unity-Socket通信实例详解
  • AI预测3D新模型百十个定位预测+胆码预测+去和尾2025年5月7日第71弹
  • 14前端项目----登录/注册
  • Android 有线网开发调试总结
  • STC单片机--仿真调试
  • 安装spark与配置环境变量
  • 【Python】算法笔记
  • TR-5000
  • Linux系统基本指令和知识指南
  • Vue 3 中的 nextTick 使用详解与实战案例
  • MES 运维
  • Vuerouter 的底层实现原理
  • boost笔记: Cannot open include file: ‘boost/mpl/aux_/preprocessed/plain/.hpp‘
  • 用3D slicer 去掉影像中的干扰体素而还原干净影像(脱敏切脸处理同)
  • CUDA:out of memory的解决方法(实测有效)
  • 【MATLAB代码解析】基于蜜蜂交配优化算法的排列组合优化问题MATLAB实现
  • Paper.js 的 simplify()方法在绘制高精度path时,消失问题
  • Nginx核心原理以及案例分析(AI)
  • 学习黑客 Linux 网络管理
  • 中国工程院院士、国医大师石学敏逝世
  • 佩斯科夫:俄会考虑30天停火提议,但试图对俄施压无用
  • 印度军方否认S-400防空系统被摧毁
  • 印度32座机场暂停民用航班运营,印称在边境多处发现无人机
  • 墨西哥宣布就“墨西哥湾”更名一事起诉谷歌
  • 习近平会见古巴国家主席迪亚斯-卡内尔