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

鸿蒙NEXT开发动画案例10

 1.创建空白项目


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

interface TranslateOffset {x?:numbery?:number
}
/*** SpinKit动画组件 - SpinTen* author: CSDN-鸿蒙布道师* since: 2025/05/16*/
@ComponentV2
export struct SpinTen {@Require @Param spinSize: number = 36;@Require @Param spinColor: ResourceColor;@Local scaleSize: number = this.spinSize * 0.75;@Local tran1: number = 0;private oldSize: number = this.spinSize;aboutToAppear(): void {this.oldSize = this.spinSize;this.scaleSize = this.spinSize * 0.75;}build() {Stack() {Grid() {// 使用循环构建9个 GridItemForEach([0, 1, 2, 3, 4, 5, 6, 7, 8], (index: number) => {GridItem() {Canvas().chunkStyle()}.translate(this.getTranslateOffset(index))})}.rowsTemplate('1fr 1fr 1fr').columnsTemplate('1fr 1fr 1fr').width(this.scaleSize).height(this.scaleSize)}.width(this.oldSize).height(this.oldSize).alignContent(Alignment.Center).onAppear(() => {this.startAnimation();})}// 抽取 translate 偏移量计算逻辑getTranslateOffset(index: number): TranslateOffset {switch (index) {case 0: return { x: this.tran1 };case 1: return { x: this.tran1 };case 2: return { y: this.tran1 };case 3: return { y: -this.tran1 };case 4: return {}; // 中间项无偏移case 5: return { y: this.tran1 };case 6: return { y: -this.tran1 };case 7: return { x: -this.tran1 };case 8: return { x: -this.tran1 };default: return {};}}// 启动 Keyframe 动画startAnimation(): void {this.getUIContext().keyframeAnimateTo({ iterations: -1, delay: 0 }, [{duration: 500,curve: Curve.EaseInOut,event: () => {this.tran1 = 0;this.scaleSize = this.oldSize * 1.1;}},{duration: 190,curve: Curve.Linear,event: () => {this.tran1 = this.oldSize / 3;}},{duration: 0,curve: Curve.Linear,event: () => {this.tran1 = 0;}},{duration: 10,curve: Curve.Linear,event: () => {}},{duration: 500,curve: Curve.EaseIn,event: () => {this.scaleSize = this.oldSize * 0.75;this.tran1 = 0;}}]);}@StyleschunkStyle() {.width(this.oldSize * 0.25).height(this.oldSize * 0.25).backgroundColor(this.spinColor).shadow(ShadowStyle.OUTER_DEFAULT_XS)}
}
代码如下:
interface TranslateOffset {x?:numbery?:number
}
/*** SpinKit动画组件 - SpinTen* author: CSDN-鸿蒙布道师* since: 2025/05/16*/
@ComponentV2
export struct SpinTen {@Require @Param spinSize: number = 36;@Require @Param spinColor: ResourceColor;@Local scaleSize: number = this.spinSize * 0.75;@Local tran1: number = 0;private oldSize: number = this.spinSize;aboutToAppear(): void {this.oldSize = this.spinSize;this.scaleSize = this.spinSize * 0.75;}build() {Stack() {Grid() {// 使用循环构建9个 GridItemForEach([0, 1, 2, 3, 4, 5, 6, 7, 8], (index: number) => {GridItem() {Canvas().chunkStyle()}.translate(this.getTranslateOffset(index))})}.rowsTemplate('1fr 1fr 1fr').columnsTemplate('1fr 1fr 1fr').width(this.scaleSize).height(this.scaleSize)}.width(this.oldSize).height(this.oldSize).alignContent(Alignment.Center).onAppear(() => {this.startAnimation();})}// 抽取 translate 偏移量计算逻辑getTranslateOffset(index: number): TranslateOffset {switch (index) {case 0: return { x: this.tran1 };case 1: return { x: this.tran1 };case 2: return { y: this.tran1 };case 3: return { y: -this.tran1 };case 4: return {}; // 中间项无偏移case 5: return { y: this.tran1 };case 6: return { y: -this.tran1 };case 7: return { x: -this.tran1 };case 8: return { x: -this.tran1 };default: return {};}}// 启动 Keyframe 动画startAnimation(): void {this.getUIContext().keyframeAnimateTo({ iterations: -1, delay: 0 }, [{duration: 500,curve: Curve.EaseInOut,event: () => {this.tran1 = 0;this.scaleSize = this.oldSize * 1.1;}},{duration: 190,curve: Curve.Linear,event: () => {this.tran1 = this.oldSize / 3;}},{duration: 0,curve: Curve.Linear,event: () => {this.tran1 = 0;}},{duration: 10,curve: Curve.Linear,event: () => {}},{duration: 500,curve: Curve.EaseIn,event: () => {this.scaleSize = this.oldSize * 0.75;this.tran1 = 0;}}]);}@StyleschunkStyle() {.width(this.oldSize * 0.25).height(this.oldSize * 0.25).backgroundColor(this.spinColor).shadow(ShadowStyle.OUTER_DEFAULT_XS)}
}

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

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

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

5.动画效果如下:

 

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

相关文章:

  • 账号风控突破:云手机设备指纹篡改检测与反制技术解析
  • 学习日志38-cpelx求解器使用
  • 前端脚手架开发指南:提高开发效率的核心操作
  • React学习———React Router
  • Jenkins 执行器(Executor)如何调整限制?
  • vue3中预览Excel文件
  • 技术文章:解决汇川MD500系列变频器干扰问题——GRJ9000S EMC滤波器的应用
  • 影楼精修-肤色统一算法解析
  • 7-15 计算圆周率
  • C++ Kafka客户端(cppkafka)安装与问题解决指南
  • Oracle — 总结
  • 智能接处警系统:以秒级联动响应重塑应急处置效能
  • 使用exceljs将excel文件转化为html预览最佳实践(完整源码)
  • 掌握HTML文件上传:从基础到高级技巧
  • 【HTML 全栈进阶】从语义化到现代 Web 开发实战
  • 数字化工厂升级引擎:Modbus TCP转Profinet网关助力打造柔性生产系统
  • SPL做量化---MTM(动量指数)
  • 在 Ubuntu 20.04 中使用 init.d 或者systemd实现开机自动执行脚本
  • 炼丹学习笔记3---ubuntu2004部署运行openpcdet记录
  • OrangePi Zero 3学习笔记(Android篇)11 - IR遥控器
  • 现在环保方面有什么新的技术动态
  • Qt/C++编写音视频实时通话程序/画中画/设备热插拔/支持本地摄像头和桌面
  • MySQL 8.0 OCP 1Z0-908 121-130题
  • 蓝牙协议架构与调试工具详解(含 BLE、HCI 命令、调试命令)
  • Prometheus实战教程:k8s平台-Mysql监控案例
  • 记一次缓存填坑省市区级联获取的操作
  • 自学嵌入式 day19-数据结构 链表
  • 【前端】构建关系图谱的前端组件推荐
  • SqlHelper 实现类,支持多数据库,提供异步操作、自动重试、事务、存储过程、分页、缓存等功能。
  • 使用哈希表封装myunordered_set和myunordered_map