当前位置: 首页 > 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.动画效果如下:

http://www.xdnf.cn/news/327511.html

相关文章:

  • 写程序,统计两会政府工作报告热词频率,并生成词云
  • 2025-05-07 Unity 网络基础7——TCP异步通信
  • 卷积神经网络基础(六)
  • Python 运维脚本
  • AI系列:智能音箱技术简析
  • void*在c语言中什么意思(非常详细)
  • scanpy处理:使用自定义 python 函数读取百迈客空间转录组数据(百创智造S1000)
  • 深度学习:智能车牌识别系统(python)
  • htop筛选进程时,出现重复进程
  • 德州仪器技术干货 | 48V 集成式热插拔电子保险丝:为现代 AI 数据中心高效供电
  • Python案例实战《水果识别模型训练及调用》
  • Linux 内核学习(7) --- 字符设备驱动
  • eFish-SBC-RK3576工控板CAN接口测试操作指南
  • leetcode 3341. 到达最后一个房间的最少时间 I 中等
  • Unity_JK框架【3】 事件系统的简单使用示例
  • 169.多数元素
  • openstack虚拟机状态异常处理
  • java集合菜鸟教程
  • 从 CodeBuddy Craft 到 edgeone-pages-mcp 上线算命网站的一次完整体验分享
  • 多语言网站的 UX 陷阱与国际化实践陷阱清单
  • 前端面试每日三题 - Day 27
  • 【Python】os模块
  • 使用 Gradio + Qwen3 + vLLM 部署 Text2SQL 多表查询系统
  • 【Prometheus】深入解析 Prometheus 特殊标签 `__param_<name>`:动态抓取参数的艺术
  • Android 数据持久化之数据库存储 Room 框架
  • 50个精选DeepSeek指令
  • ifconfig statistics
  • springboot使用阿里云OSS实现文件上传
  • 云上玩转Qwen3系列之二:PAI-LangStudio搭建联网搜索和RAG增强问答应用
  • C++初阶 —— 类和对象