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

鸿蒙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.lryc.cn/news/2380403.html

相关文章:

  • Python 的 os 库常见使用方法(操作目录及文件)
  • 【Linux】Linux安装并配置Redis
  • 【11408学习记录】考研英语辞职信写作三步法:真题精讲+妙句活用+范文模板
  • 数据库(一):分布式数据库
  • Java并发编程-线程池(三)
  • 《黑马前端ajax+node.js+webpack+git教程》(笔记)——node.js教程+webpack教程(nodejs教程)
  • Flink 快速入门
  • 高效管理多后端服务:Nginx 配置与实践指南
  • 阻塞队列:线程安全与生产者消费者模型解析
  • 【入门|Docker】基础知识扫盲:什么是 Docker?
  • 如何利用 Java 爬虫获得某书笔记详情:实战指南
  • 【MYSQL】基本查询,表的增删查改
  • 在嵌入式系统中, 一般链路层断开多久,断开TCP为好
  • Android Studio 日志系统详解
  • 基于matlab的D2D 功率控制仿真
  • 互联网大厂Java面试:从基础到复杂场景的技术挑战
  • 使用Redission来实现布隆过滤器
  • 为 Windows 和 Ubuntu 中设定代理服务器的详细方法
  • Feign异步模式丢失上下文问题
  • OpenCV阈值处理完全指南:从基础到高级应用
  • 【AWS入门】Amazon SageMaker简介
  • ArcGIS Pro 3.4 二次开发 - 内容
  • 如何在 MongoDB 中设计文档结构?与关系型数据库的表结构设计有何不同?
  • MYSQL 故障排查与生产环境优化
  • 解决使用@JsonFormat(pattern = “yyyy-MM-dd HH:mm:ss“, timezone = “GMT+8“)时区转换无效的问题
  • 计算机网络概要
  • Word压缩解决方案
  • Spring Boot开发—— 整合Lucene构建轻量级毫秒级响应的全文检索引擎
  • TDengine 2025年产品路线图
  • vue3中element-plus修改el-tooltip的宽度