鸿蒙App开发--心愿池的动画特效:投币动画与进度条
发布时间:2026/6/10 1:56:22
分类:文化教育
浏览:1234

心愿池的动画特效投币动画与进度条如果你有心愿目标想攒钱实现推荐去鸿蒙应用市场搜一下**「心愿池」**下载体验体验。创建心愿、投币储蓄、追踪进度一套走下来对攒钱目标会有更清晰的把控。体验完再回来看这篇文章你会更清楚投币动画和进度条背后是怎么实现的。写在前面大家好我是一名写了十多年Web前端的老兵。从jQuery时代一路走到React/VueCSS3动画、requestAnimationFrame、Web Animation API这些都算是看家本领。去年开始转战鸿蒙生态用ArkTS开发App这一路踩了不少坑也积累了不少心得。很多人觉得前端转鸿蒙应该很容易——都是写UI嘛组件化、状态管理、生命周期概念都差不多。但真正上手之后你会发现相似的地方让你觉得亲切不同的地方让你抓狂。比如动画实现Web用CSSkeyframes或requestAnimationFrame鸿蒙用animator模块。数据存储Web的localStorage到了ArkTS变成了ohos.data.preferences。状态管理React的useState变成了State。接下来这篇文章我会用心愿池的实际开发经历带你看看投币动画、进度条动画、成就系统的实现。这篇文章聊什么心愿池的动画特效功能核心要解决三个问题投币动画投币时的视觉反馈进度条动画储蓄进度的平滑动画成就系统激励用户坚持储蓄第一步心愿数据结构interfaceWish{id:string;name:string;category:string;// 8种分类priority:string;// high/medium/lowtargetAmount:number;savedAmount:number;progress:number;// 0-1records:SavingsRecord[];createdAt:number;}interfaceSavingsRecord{id:string;amount:number;note:string;timestamp:number;}// 8种心愿分类constWISH_CATEGORIES[{id:travel,name:旅行,icon:✈️},{id:tech,name:数码,icon:},{id:education,name:学习,icon:},{id:home,name:家居,icon:},{id:fashion,name:时尚,icon:},{id:health,name:健康,icon:},{id:hobby,name:爱好,icon:},{id:other,name:其他,icon:}];第二步投币动画EntryComponentstruct WishDetailPage{Statewish:Wish|nullnullStatecoinAnimation:booleanfalseStateprogressValue:number0privateanim:AnimatorResult|nullnullaboutToDisappear(){if(this.anim){this.anim.cancel()}}// 投币动画playCoinAnimation(){this.coinAnimationtrue;this.animanimator.create({duration:800,iterations:1,curve:EaseOut});this.anim.onFrame(value:number){// 0-0.5: 硬币下落// 0.5-1: 进度条更新if(value0.5){// 硬币下落阶段}else{// 进度条更新阶段this.progressValuethis.wish?this.wish.progress:0;}};this.anim.onFinish(){this.coinAnimationfalse;};this.anim.play()}// 快速投币asyncquickSave(amount:number){if(!this.wish)return;constrecord:SavingsRecord{id:save_${Date.now()},amount,note:,timestamp:Date.now()};this.wish.records.push(record);this.wish.savedAmountamount;this.wish.progressMath.min(this.wish.savedAmount/this.wish.targetAmount,1);awaitthis.saveWish();this.playCoinAnimation();}asyncsaveWish(){if(!this.wish)return;conststoreawaitpreferences.getPreferences(getContext(),xinyuanchi_data);letwishes:Wish[]JSON.parse(awaitstore.get(wishes,[])asstring);constindexwishes.findIndex(ww.idthis.wish!.id);if(index-1){wishes[index]this.wish;}awaitstore.set(wishes,JSON.stringify(wishes));awaitstore.flush();}build(){Column(){if(this.wish){// 心愿名称Text(this.wish.name).fontSize(24).fontWeight(FontWeight.Bold).margin({bottom:8})// 进度环Stack(){Circle({width:160,height:160}).stroke(#374151).strokeWidth(12).fill(transparent)Circle({width:160,height:160}).stroke(#A855F7).strokeWidth(12).fill(transparent).strokeDashArray([this.progressValue*500,500])Column(){Text(${Math.round(this.progressValue*100)}%).fontSize(32).fontWeight(FontWeight.Bold).fontColor(#A855F7)Text(¥${this.wish.savedAmount}/ ¥${this.wish.targetAmount}).fontSize(12).fontColor(#9CA3AF)}}.margin({top:16,bottom:24})// 快速投币Text(快速投币).fontSize(14).fontColor(#9CA3AF).margin({bottom:8})Flex({wrap:FlexWrap.Wrap}){ForEach([10,50,100,500],(amount:number){Button(¥${amount}).onClick(()this.quickSave(amount)).width(70).height(40).margin(4).backgroundColor(#A855F7).borderRadius(20)})}.justifyContent(FlexAlign.Center)}}.width(100%).height(100%).padding(16).backgroundColor(#111827)}}第三步成就系统constACHIEVEMENTS[{id:first_wish,name:许愿,desc:创建第一个心愿,check:(s)s.wishCount1},{id:first_save,name:第一笔,desc:完成第一次储蓄,check:(s)s.saveCount1},{id:ten_saves,name:坚持储蓄,desc:累计储蓄10次,check:(s)s.saveCount10},{id:first_complete,name:心愿达成,desc:完成第一个心愿,check:(s)s.completedWishes1},{id:total_1000,name:千元储蓄,desc:累计储蓄1000元,check:(s)s.totalSaved1000},{id:total_10000,name:万元储蓄,desc:累计储蓄10000元,check:(s)s.totalSaved10000},{id:five_wishes,name:多心愿,desc:同时管理5个心愿,check:(s)s.wishCount5},{id:streak_30,name:连续30天,desc:连续30天有储蓄,check:(s)s.consecutiveDays30}];总结这篇文章围绕心愿池的动画特效功能讲解了三个核心主题投币动画用animator实现硬币下落和进度更新的两阶段动画进度环用strokeDashArray实现圆弧进度条成就系统8个成就激励用户坚持储蓄进度环的核心是strokeDashArray——通过控制虚线的长度来显示进度。这个技巧在Web和鸿蒙里都适用。如果你有心愿目标想攒钱实现希望这篇文章能帮你理解心愿池背后的动画实现。去鸿蒙应用市场下载体验一下吧有问题欢迎交流。