#鸿蒙通关秘籍#在HarmonyOS Next中如何通过设置动画参数来控制水波纹效果的播放速度?

HarmonyOS
2024-11-29 14:54:11
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
时光笔BI

通过调整animateTo函数中的duration属性来控制水波纹效果的播放速度,duration定义了动画持续的时间,单位为毫秒(ms)。同时,通过设置iterations属性可让动画无限次执行。下面的代码实现展示了如何设置不同的duration以控制播放速度:

@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: 800, iterations: -1 }, () => { // Duration is set to 800ms for faster animation
              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:03:54
相关问题
如何实现组件水波纹动画案例
1079浏览 • 1回复 待解决