Flutter 動畫鼻祖之CustomPaint

老孟導讀:CustomPaint能夠稱之爲動畫鼻祖,它能夠實現任何酷炫的動畫和效果。CustomPaint自己沒有動畫屬性,僅僅是繪製屬性,通常狀況下,CustomPaint會和動畫控制配合使用,達到理想的效果。git

基本用法

CustomPaint的用法很是簡單,以下:canvas

CustomPaint(
  painter: MyCustomPainter(),
)

MyCustomPainter定義以下:微信

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

  @override
  bool shouldRepaint(MyCustomPainter oldDelegate) {
    return this != oldDelegate;
  }
}

上面的MyCustomPainter爲了看起來清晰,什麼也沒有作,一般狀況下,在paint方法內繪製自定義的效果。shouldRepaint方法一般在當前實例和舊實例屬性不一致時返回true。ide

paint經過canvas繪製,size爲當前控件的大小,下面看看canvas的方法。函數

繪製點

Paint _paint = Paint()
    ..color = Colors.red
    ..strokeWidth = 3;

  @override
  void paint(Canvas canvas, Size size) {
    var points = [
      Offset(0, 0),
      Offset(size.width / 2, size.height / 2),
      Offset(size.width, size.height),
    ];
    canvas.drawPoints(PointMode.points, points, _paint);
  }

image-20200601094814789

PointMode有3種模式:動畫

  • points:點
  • lines:將2個點繪製爲線段,若是點的個數爲奇數,最後一個點將會被忽略
  • polygon:將整個點繪製爲一條線

繪製線

canvas.drawLine(Offset(0, 0),Offset(size.width, size.height), _paint);

image-20200601095248302

繪製路徑

Paint _paint = Paint()
  ..color = Colors.red
  ..style = PaintingStyle.stroke
  ..strokeWidth = 3;

@override
void paint(Canvas canvas, Size size) {
  print('size:$size');
  var _path = Path()
    ..moveTo(0, 0)
    ..lineTo(size.width, 0)
    ..lineTo(size.width, size.height)
  ..close();
  canvas.drawPath(_path, _paint);
}

image-20200601110532164

這裏注意Paint.style,還能夠設置爲PaintingStyle.fill,效果以下:this

image-20200601110642252

此時Path的路徑不要在一條直線上,不然會看不到效果。code

繪製各類形狀

繪製圓形blog

canvas.drawCircle(Offset(size.width/2, size.height/2), 20, _paint);

image-20200601111802952

繪製橢圓ci

canvas.drawOval(Rect.fromLTRB(0, 0, size.width, size.height/2), _paint);

image-20200601111910120

若是給定的Rect爲正方形,那麼橢圓將會變爲圓形。

繪製弧

canvas.drawArc(
    Rect.fromLTRB(0, 0, size.width, size.height), 0, pi/2, true, _paint);

image-20200601112255715

繪製圓角矩形

canvas.drawRRect(
    RRect.fromLTRBR(0, 0, size.width, size.height, Radius.circular(10)), _paint)

image-20200601112830212

canvas還有不少繪製函數,好比貝塞爾曲線、三次貝塞爾曲線、畫布的反轉等操做,這裏不在一一介紹。

這些函數和Android的Canvas基本同樣,若是你有Android基礎,直接套用便可。

最後奉上一個繪製玫瑰的動畫效果:

rose_gif

這個效果是否是很酷炫,咱們看下繪製花骨朵代碼:

///
/// 繪製花骨朵
///
_drawFlower(Canvas canvas, Size size) {
  //將花變爲紅色
  if (flowerPaths.length >= RoseData.flowerPoints.length) {
    var path = Path();
    for (int i = 0; i < flowerPaths.length; i++) {
      if (i == 0) {
        path.moveTo(flowerPaths[i].dx, flowerPaths[i].dy);
      } else {
        path.lineTo(flowerPaths[i].dx, flowerPaths[i].dy);
      }
    }
    _paint.style = PaintingStyle.fill;
    _paint.color = _flowerColor;
    canvas.drawPath(path, _paint);
  }
  //繪製線
  _paint.style = PaintingStyle.stroke;
  _paint.color = _strokeColor;
  //去掉最後2個點,最後2個點爲了繪製紅色
  var points = flowerPaths.sublist(0, max(0, flowerPaths.length - 2));
  canvas.drawPoints(PointMode.polygon, points, _paint);
}

花骨朵的繪製只經過canvas.drawPath就實現了,其實整個玫瑰花的繪製都是經過canvas.drawPath加上動畫控制實現的。

CustomPaint能夠實現任何你想要的動畫的效果,好比繪畫版就能夠經過此控件實現。

獲取完整代碼方式掃碼下方二維碼回覆:rose

交流

老孟Flutter博客地址(近200個控件用法):http://laomengit.com

歡迎加入Flutter交流羣(微信:laomengit)、關注公衆號【老孟Flutter】:

相關文章
相關標籤/搜索