九、Flutter水波动画

作者: 星星编程 | 来源:发表于2019-07-31 06:48 被阅读24次

目录
一、效果展示
二、CustomPaint
三、CustomPainter
四、水波效果
五、文字效果
六、动起来

一、效果展示

Flutter水波动画.png

二、CustomPaint

WaterWave继承CustomPainter用画笔画出来的水波动画效果需要CustomPaint呈现出来。
CustomPaint有两个参数painter和size,painter是一个继承了CustomPainter的对象,主要实现了绘画控件的功能。size指定了控件的大小,如果CustomPaint的child不为空,size的值就是child控件的大小,指定size的值则无效;如果child为空,所指定的size的值,就是画布的大小。

CustomPaint(
                painter: WaterWave(this.variable, 0.79, this.offsetX),
                size: Size(160, 160),
              )

三、CustomPainter

WaterWave继承了CustomPainter,实现了其中两个重要方法:paint和shouldRepaint。paint当自定义控件需要重画时被调用。shouldRepaint则决定当条件变化时是否需要重画。

class WaterWave extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
  }

  @override
  bool shouldRepaint(ClockPainter oldDelegate) {
    return true;
  }
}

四、水波效果

绘出水波效果有几个重要的参数:波纹振幅:double waveAmplitude;
波纹周期:double waveCycle;波纹上升速度:double waveGrowth;
波浪x位移:double offsetX;当前波浪上市高度Y(高度从大到小 坐标系向下增长):double currentWavePointY; 模拟波纹:double variable;这几个参数在sin函数和cos函数地计算下模拟出水波的形状,然后画出来。

void firstWave() {
    Path path = new Path();
    double y = this.currentWavePointY;
    path.moveTo(0, y);
    for (double x = 0.0; x <= this.waterWaveWidth; x++) {
      y = this.waveAmplitude * sin(this.waveCycle * x + this.offsetX) +
          this.currentWavePointY;
      path.lineTo(x, y);
    }
    path.lineTo(this.waterWaveWidth, this.size.height);
    path.lineTo(0, this.size.height);
    path.close();
    var paint = new Paint()
      ..color = Color.fromRGBO(223, 83, 64, 1)
      ..style = PaintingStyle.fill
      ..maskFilter = MaskFilter.blur(BlurStyle.inner, 5.0);
    this.canvas.drawPath(path, paint);
  }


void secondWave() {
    Path path = new Path();
    double y = this.currentWavePointY;
    path.moveTo(0, y);
    for (double x = 0.0; x <= this.waterWaveWidth; x++) {
      y = this.waveAmplitude * cos(this.waveCycle * x + this.offsetX) +
          this.currentWavePointY;
      path.lineTo(x, y);
    }
    path.lineTo(this.waterWaveWidth, this.size.height);
    path.lineTo(0, this.size.height);
    path.close();
    var paint = new Paint()
      ..color = Color.fromRGBO(236, 90, 66, 1)
      ..style = PaintingStyle.fill
      ..maskFilter = MaskFilter.blur(BlurStyle.inner, 5.0);
    this.canvas.drawPath(path, paint);
}

五、文字效果

绘画文字的时候需要添加引用,import 'dart:ui' as UI;

 UI.ParagraphBuilder pb = UI.ParagraphBuilder(UI.ParagraphStyle(
      textAlign: TextAlign.center,
      fontWeight: FontWeight.w500,
      fontStyle: FontStyle.normal,
      fontSize: 70.0,
    ));
    pb.pushStyle(UI.TextStyle(color: Colors.white));
    pb.addText('86');
    UI.Paragraph paragraph = pb.build();
    paragraph.layout(UI.ParagraphConstraints(width: 140));
    Offset offset = Offset(10, 30.0);
    this.canvas.drawParagraph(paragraph, offset);


    UI.ParagraphBuilder pb2 = UI.ParagraphBuilder(UI.ParagraphStyle(
      textAlign: TextAlign.center,
      fontWeight: FontWeight.w500,
      fontStyle: FontStyle.normal,
      fontSize: 18.0,
    ));
    pb2.pushStyle(UI.TextStyle(color: Colors.white));
    pb2.addText('评分');
    UI.Paragraph paragraph2 = pb2.build();
    paragraph2.layout(UI.ParagraphConstraints(width: 100));
    Offset offset2 = Offset(30, 105.0);
    this.canvas.drawParagraph(paragraph2, offset2);

六、动起来

重新initState方法,添加一个定时器没25毫秒改变一下水波的波纹。
variable模拟波纹上下浮动的效果,offsetX模拟波浪的位移,从而能够增强波纹震动的效果。

@override
  void initState() {
    super.initState();

    this.variable = widget.variable;
    this.increase = widget.increase;
    this.waveSpeed = 0.4 / pi;
    this.offsetX = 0.0;

    this.timer = Timer.periodic(new Duration(milliseconds: 25), (timer) {
      setState(() {
        this.offsetX += this.waveSpeed;

        if (this.increase) {
          this.variable += 0.01;
        } else {
          this.variable -= 0.01;
        }

        if (this.variable <= 1) {
          this.increase = true;
        }

        if (this.variable >= 1.6) {
          this.increase = false;
        }
      });
    });
  }
关注公众号,查看更多内容.jpg

相关文章

  • 九、Flutter水波动画

    目录一、效果展示二、CustomPaint三、CustomPainter四、水波效果五、文字效果六、动起来 一、效...

  • flutter九宫格看图

    flutter九宫格图片 利用GridView实现布局,用Hero动画进行展示

  • Flutter 动画效果

    在动画方面 Flutter 提供了 Animation 类提供使用。 动画 Flutter 中创建动画,请创建名为...

  • Flutter动画animation

    参考 动画 flutter中动画抽象 划重点 Flutter 中的动画系统基于Animation对象,widget...

  • Flutter中的动画

    Flutter中的动画 Flutter中的动画 https://flutterchina.club/tutoria...

  • 水波动画

    分析 每一个水波都是一个CAShapeLayer的实例,然后使用CADisplayLink来刷新界面,每次刷新的时...

  • Flutter系列九:Flutter动画源码解析

    上篇文章我们详细介绍了动画的使用,本文我们将从源码的角度解析动画的底层逻辑。 动画的实现机制 动画的控制是由Ani...

  • Flutter(九) 实现抽屉动画

    您好,欢迎关注我,本文章是关于 Flutter 的系列文,从简单的 Flutter 介绍开始,一步步带你了解进入 ...

  • Flutter从入门到写出完整App Day12

    20.3.27 Flutter实现动画 Flutter有自己的渲染闭环 一. 动画API认识 1. Animati...

  • Flutter轮播图

    前端开发当中最有意思的就是实现动画特效,Flutter提供的各种动画组件可以方便实现各种动画效果。Flutter中...

网友评论

    本文标题:九、Flutter水波动画

    本文链接:https://www.haomeiwen.com/subject/qegkkctx.html