HarmonyOS 如何实现WaveView动画?

语音搜索的时候,按住语音按钮,下面的背景有水波纹的动画效果。

HarmonyOS
2024-09-27 11:19:15
921浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Heiang
@Entry  
@Component  
struct Page240423185507109 {  
  private settings: RenderingContextSettings = new RenderingContextSettings(true)  
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)  
  waveSpeed: number = 0.03  
  phase: number = 0  
  
  drawWave() {  
    this.context?.clearRect(0, 0, this.context.width, this.context.height)  
  
    this.context?.beginPath()  
    for (let x = 0; x <= this.context.width; x += 10) {  
      const y = this.context.height / 2 + Math.sin(x * 0.01 + this.phase) * 20  
      this.context?.lineTo(x, y)  
    }  
    this.context?.lineTo(this.context.width, this.context.height)  
    this.context?.lineTo(0, this.context.height)  
    this.context?.closePath()  
  
    this.context.fillStyle = '#30007bff'  
    this.context?.fill()  
  
    this.context?.beginPath()  
    for (let x = 0; x <= this.context.width; x += 10) {  
      const y = this.context.height / 2 + Math.sin(x * 0.02 + this.phase) * 10  
      this.context?.lineTo(x, y)  
    }  
    this.context?.lineTo(this.context.width, this.context.height)  
    this.context?.lineTo(0, this.context.height)  
    this.context?.closePath()  
  
    this.context.fillStyle = '#50007bff'  
    this.context?.fill()  
  
    this.context?.beginPath()  
    for (let x = 0; x <= this.context.width; x += 10) {  
      const y = this.context.height / 2 + Math.sin(x * 0.015 + this.phase) * 30  
      this.context?.lineTo(x, y)  
    }  
    this.context?.lineTo(this.context.width, this.context.height)  
    this.context?.lineTo(0, this.context.height)  
    this.context?.closePath()  
  
    this.context.fillStyle = '#20007bff'  
    this.context?.fill()  
  
    this.phase += this.waveSpeed  
  }  
  
  build() {  
    Column() {  
      Canvas(this.context)  
        .width(200)  
        .height(100)  
        .backgroundColor(Color.Pink)  
        .onReady(() => {  
          setInterval(this.drawWave.bind(this), 20)  
        })  
    }.width("100%").height("100%").justifyContent(FlexAlign.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.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.

具体实现逻辑:

this.context?.beginPath()  
for (let x = 0; x <= this.context.width; x += 10) {  
  const y = this.context.height / 2 + Math.sin(x * 0.01 + this.phase) * 20  
  this.context?.lineTo(x, y)  
}  
this.context?.lineTo(this.context.width, this.context.height)  
this.context?.lineTo(0, this.context.height)  
this.context?.closePath()  
  
this.context.fillStyle = '#30007bff'  
this.context?.fill()
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

这一段代码,就是一条水波纹。

分享
微博
QQ
微信
回复
2024-09-27 17:06:56
相关问题
HarmonyOS canvas动画如何实现逐帧动画
863浏览 • 1回复 待解决
HarmonyOS 组合动画如何实现
631浏览 • 1回复 待解决
HarmonyOS 如何实现RippleView动画
896浏览 • 1回复 待解决
HarmonyOS 如何实现呼吸灯动画
829浏览 • 1回复 待解决
属性动画如何实现宽高动画效果
2920浏览 • 1回复 待解决
HarmonyOS 如何实现音频声浪动画
1555浏览 • 1回复 待解决
HarmonyOS如何实现动态缩放动画
1039浏览 • 1回复 待解决
HarmonyOS 如何实现旋转动画
1146浏览 • 1回复 待解决
HarmonyOS 如何实现动画集合?
1025浏览 • 1回复 待解决
HarmonyOS 如何实现放大缩小的动画
1156浏览 • 1回复 待解决
HarmonyOS router跳转动画如何实现
561浏览 • 1回复 待解决
HarmonyOS 动画实现
941浏览 • 1回复 待解决
HarmonyOS Canvas 实现动画
1131浏览 • 1回复 待解决
如何应用属性动画实现宽高的动画
1039浏览 • 1回复 待解决
如何实现动画转场效果
1729浏览 • 1回复 待解决
HarmonyOS 动画效果实现
1041浏览 • 1回复 待解决
鸿蒙如何实现动画值变化
10004浏览 • 1回复 待解决
文字动画效果如何实现
2914浏览 • 0回复 待解决
求教ArkUI如何实现组合动画
6323浏览 • 1回复 待解决
如何实现list的折叠动画效果
1316浏览 • 1回复 待解决
在 ArkUl中如何实现动画效果?
710浏览 • 0回复 待解决
HarmonyOS Navigation实现Dialog转场动画
704浏览 • 1回复 待解决
HarmonyOS Canvas的动画实现
498浏览 • 1回复 待解决
渐变动画效果如何实现
277浏览 • 0回复 待解决