#鸿蒙通关秘籍#如何在HarmonyOS Next上实现不间断的水波纹动画效果?

HarmonyOS
2024-11-29 15:05:45
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
时光笔SMM

为了在HarmonyOS Next实现不间断的水波纹动画效果,使用animateTo动画函数,并设置iterations: -1参数,使动画无限循环。在组件的onAppear事件中调用该函数,使得动画在组件加载后自动执行。以下是具体实现代码:

@Entry
@Component
struct Demo {
  @State scaleList: number[] = [] 
  @State opacityList: number[] = [] 
  private cloneScaleList: number[] = [] 
  private cloneOpacityList: number[] = [] 
  private scaleRatio:number=0.7 

  aboutToAppear(): void {
    this.scaleList = new Array(3).fill('').map((item: string, index: number) => 1 + index * this.scaleRatio)
    this.opacityList = new Array(3).fill('').map((item: string, index: number) => (3 - index)/10 )
    this.cloneScaleList = this.scaleList.slice()
    this.cloneOpacityList = this.opacityList.slice()
  }

  build() {
    Column({ space: 50 }) {
      Stack() {
        ForEach(this.scaleList, (item: number, index: number) => {
          Column() {
          }
          .width(50)
          .height(50)
          .borderRadius(25)
          .backgroundColor('#1463F4')
          .opacity(this.opacityList[index])
          .scale({ x: this.scaleList[index], y: this.scaleList[index] })
          .onAppear(() => {
            animateTo({ duration: 1200, iterations: -1 }, () => {
              this.scaleList[index] = this.cloneScaleList[index] + this.scaleRatio
              this.opacityList[index] = this.cloneOpacityList[index] -  0.1
            })
          })
        }, (item: number, index: number) => index.toString())

        Image($r('app.media.app_icon')).width(50).borderRadius(25)
      }
    }.height('100%').width('100%').justifyContent(FlexAlign.Center).alignItems(HorizontalAlign.Center)
  }
}
分享
微博
QQ
微信
回复
2024-11-29 17:09:22
相关问题
如何实现组件水波纹动画案例
1092浏览 • 1回复 待解决