HarmonyOS 鸿蒙Next如何实现组件点击的水波纹效果

HarmonyOS 鸿蒙Next如何实现组件点击的水波纹效果

如何实现组件点击的水波纹效果

2 回复

@Entry

@Component

struct Index {

  @State message: string = 'Hello World';

  @State flag: boolean = false

  @State ballX: number = 0

  @State ballY: number = 0

  build() {

    Column() {

      Column() {

      }.layoutWeight(1).backgroundColor(Color.Pink).width("100%")

      Stack() {

        Button("按钮").width("100%").height("100%").stateEffect(false)

        if (this.flag) {

          Row()

            .width(5)

            .height(5)

            .position({

              x: this.ballX,

              y: this.ballY

            })

            .backgroundColor("rgba(0,0,0,0.7)")

            .transition(TransitionEffect.asymmetric(

              TransitionEffect.IDENTITY,

              TransitionEffect.OPACITY.animation({ duration: 1000, curve: Curve.EaseInOut }).combine(

                TransitionEffect.scale({ x: 100, y: 100 })

              )

            ))

            .borderRadius(15)

        }

      }.width("100%").height(60).clip(true)

      .onClick((e) => {

        this.ballX = e.x - 3

        this.ballY = e.y - 3

        this.flag = !this.flag;

        setTimeout(() => {

          this.flag = !this.flag;

        }, 100)

      })

      Column() {

      }.layoutWeight(1).backgroundColor(Color.Pink).width("100%")

    }

    .width('100%')

    .height('100%')

  }

}

 

更多关于HarmonyOS 鸿蒙Next如何实现组件点击的水波纹效果的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS 鸿蒙Next系统中,实现组件点击的水波纹效果可以通过以下步骤进行:

  1. 定义动画资源: 在resources/anim目录下创建一个新的XML文件(例如ripple_effect.xml),定义水波纹动画效果。这通常涉及使用alphascale等动画属性来模拟波纹扩散的效果。

  2. 设置组件点击事件: 在布局文件中,为需要添加水波纹效果的组件设置点击事件监听器。例如,在一个Button组件上设置点击监听器。

  3. 触发动画: 在点击事件监听器的回调方法中,通过动画资源管理器加载并启动ripple_effect.xml定义的动画。可以使用AnimationUtil等工具类来加载动画,并通过组件的startAnimation方法开始动画。

  4. 确保动画效果: 动画效果应覆盖在组件上方,可能需要调整组件的层级或背景,以确保动画的可见性和正确性。

  5. 调试与优化: 根据实际效果,调整动画的持续时间、插值器等属性,以获得最佳的水波纹效果。

如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html。

回到顶部