当前位置: 首页 > 科技观察

HarmonyOS-ArkUI(JS)实现充电动画

时间:2023-03-13 01:04:20 科技观察

了解更多开源请访问:开源基础软件社区https://ost.51cto.com电池图标显示充电动画效果。下面是基于HarmonyOSJSAPI实现的一个充电动画效果。效果展示及实现原理电池充电的动画是一个加载宽度从0%到100%的过程,所以在充电状态下,只需要动态改变显示的宽度就可以实现充电效果,而电池的轮廓和充电状态在本地加载。以图片的形式实现。实现步骤1.实现电池图标首先加载空电池轮廓的图片,在电池轮廓上叠加显示电池电量的进度条,在电池电量进度条上叠加闪电图标显示充电状态。battery.hml:

css样式:.container{高度:48px;宽度:48px;证明内容:居中;对齐项目:中心;}.progress{高度:16px;背景色:黑色;位置:绝对;50%;transform:translate(0,-50%);}.charging{height:48px;宽度:48px;位置:绝对;左:50%;顶部:50%;transform:translate(-50%,-50%);}2.获取手机电池信息通过系统接口提供的接口获取手机电池信息,导入batteryInfo模块,该接口从API版本6开始支持。从“@ohos.batteryInfo”导入电池信息;batteryInfo详细属性:nametype可读可写descriptionbatterySOCnumber表示当前设备电池剩余电量的百分比。chargingStatus[BatteryChargeState]是否表示当前设备电池的充电状态。healthStatus[BatteryHealthState]是否表示当前设备电池的健康状态。pluggedType[BatteryPluggedType]是否表示当前设备连接的充电器类型。voltagenumber是否表示当前设备电池的电压,单位为微伏。technologystring是否表示当前设备电池的技术型号。batteryTemperaturenumber是否表示当前设备电池的温度,单位为0.1摄氏度。isBatteryPresent7+boolean是否表示当前设备是否支持电池或是否存在电池。index.js中获取电池电量和充电状态//获取设备电池电量百分比this.batterySoc=batteryInfo.batterySOC//设备电池是否处于充电状态this.chargingStatus=batteryInfo.chargingStatus==batteryInfo.BatteryChargeState.ENABLE3.实现动态变化进度条宽度进度条必须显示在电池图标的轮廓内,所以进度条的宽度不能超过电池图标。根据获取的电量百分比,可以动态改变宽度,但不能超过充满电时的最大宽度。充满电时的最大宽度可以根据效果进行调整。以下代码:getBatteryNum(){letwidth=(this.percent/100)*this.maxWidthif(this.percent>=100){returnthis.maxWidth}console.log("getBatteryNum"+Math.round(width))returnMath.round(width);},4.实现充电动画效果使用系统提供的动画接口导入animator模块。API版本6支持该接口。从“@ohos.animator”导入动画师;animator有以下方法:nameparametertypedescriptionupdateupdate(options:AnimatorOptions):void更新当前animatorplayplay():voidplayanimationfinishfinish():voidendanimationpausepause():void暂停动画cancellancel():voiddelete动画reversereverse():void以相反顺序播放动画onframeonframe:(progress:number)=>voidtriggeronfinishonfinish:()=>voidanimationcompleteoncanceloncancel:()=>voidanimationCanceledonrepeatonrepeat:()=>voidanimationrepeatplaybackAnimatorOptions属性:name参数类型必填说明durationnumber是动画播放的时长,单位毫秒,默认为0。easingstring是动画插值曲线,默认是ease'。delaynumber是动画延迟的时长,单位毫秒,默认为0,表示不延迟。填写“无”|“转发”|“向后”|“both”是动画执行后是否回到初始状态,默认值为“none”。动画执行后,动画结束时的状态(在最后一个关键帧中定义)被保留。方向“正常”|“反向”|“备用”|“alternate-reverse”为动画播放模式,默认值为“normal”。iterationsnumber为动画播放次数,默认值为1。设置为0时不播放,设置为-1时无限播放。beginnumber是动画插值的起始点,不设置则默认为0。endnumber为动画插值结束点,不设置则默认为1。当触发充电状态时,反复播放Animator动画,并在onframe()回调中动态改变电池电量百分比,从而改变进度条的宽度。详细代码如下:battery.js完整代码:importanimatorfrom'@ohos.animator';importbatteryInfofrom'@ohos.batteryInfo';导出默认{props:['quantity','charge'],data(){return{percent:this.quantity,chargeState:this.charge,batteryIcon:"/common/img/ic_battery.png",animator:null,最大宽度:30};},onInit(){console.log("batteryonInit")this.setBatteryIcon()this.$watch('quantity','onQuantityChange')this.$watch('charge','onChargeChange')varoptions={duration:2000,easing:'friction',fill:'forwards',direction:'reverse',iterations:-1,begin:0,end:this.maxWidth};this.animator=animator.createAnimator(选项);var_this=这个;this.animator.onframe=function(value){_this.percent=(parseInt(value)/_this.maxWidth)*100};},onPageShow(){console.log("batteryonPageShowpercent="+this.percent+"chargeState="+this.chargeState)},//监听是否变化电量onQuantityChange(newV,oldV){console.log('======proQuantity============newV='+newV+"oldV="+oldV)this.percent=newV},//监听是取消充电onChargeChange(newV,oldV){console.log('======proIsCharge============newV='+newV+"oldV="+oldV)this.chargeState=newVif(this.chargeState){this.animator.play();}else{this.percent=batteryInfo.batterySOCthis.animator.cancel();}},getBatteryNum(){letwidth=(this.percent/100)*this.maxWidthif(this.percent>=100){returnthis.maxWidth}console.log("getBatteryNum"+Math.round(width))关于转Math.round(width);},onDestroy(){this.animator.cancel();}}总结以上只是一个翻译动画效果的简单实现,结合系统的Animator可以实现各种炫酷的动画效果,可以提升应用的视觉效果和用户体验。掌握HarmonyOS动画的使用,可以让我们更好的了解和参与HarmonyOS的开发。了解更多开源内容,请访问:软件社区https://ost.51cto.com。