#鸿蒙通关秘籍#如何在HarmonyOS Next中实现轮播图的缩放动画?

HarmonyOS
6h前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
JSON碧落黄泉

自定义缩放动画可以通过customContentTransition事件实现。结合position属性计算缩放比例,在中间图片为重点显示状态,左右两边图片缩小:

@Entry
@Component
struct ScaleTransitionSwiper {
  controller: SwiperController = new SwiperController()
  imageList: string[] = [
    'https://example.com/image1.jpg',
    'https://example.com/image2.jpg',
    'https://example.com/image3.jpg'
  ]

  build() {
    Column() {
      Swiper(this.controller) {
        ForEach(this.imageList, (item: string, index: number) => {
          Image(item).height(300).borderRadius(10).scale({
            y: 1 - Math.abs(this.positionList[index]),
          })
        }, (index: number) => index.toString())
      }
      .width('100%')
      .customContentTransition({
        timeout: 1000,
        transition: (proxy: SwiperContentTransitionProxy) => {
          let scale = 1 - Math.abs(proxy.position) * 0.2
          this.positionList[proxy.index] = proxy.position
          console.log(`Scale for index ${proxy.index}: ${scale}`)
        }
      })
    }
    .width('100%')
    .height('100%')
  }
}
分享
微博
QQ
微信
回复
3h前
相关问题