#鸿蒙通关秘籍#如何在开发HarmonyOS Next水波纹动画特效时有效管理组件状态?

HarmonyOS
2024-11-29 14:32:56
522浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
OLTP风颂歌

在开发HarmonyOS Next水波纹动画特效时,使用State来管理组件的状态,这样可以让动画具有良好的运行效果。在进行动画开发时,合理设计和复制状态数据能确保状态的准确性和稳定性。以下是如何管理状态的代码示例:

@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)
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
分享
微博
QQ
微信
回复
2024-11-29 17:24:52
相关问题
如何实现组件水波纹动画案例
2163浏览 • 1回复 待解决