Flutter 画笔(Paint)、绘制弧(drawArc)

人生在勤,不索何获。--张衡 

drawArc(绘制弧线)

 

drawArc(Rect rect, double startAngle, double sweepAngle, bool useCenter, Paint paint)
rect构造矩形
startAngle开始角度
sweepAngle绘制的弧形占用角度弧度(360°角=2π)
userCenter扇形或圆弧段(true或false)
paint画笔
/// The PI constant.
const double pi = 3.1415926535897932;

paint(画笔)

isAntiAlias是否抗锯齿(bool)
strokeWidth画笔宽度 (double)
color画笔颜色(Color)
strokeCap延伸绘制(画 冒)
invertColors转换颜色值(二进制补码)

 useCenter=false(绘制圆弧段)

Rect.fromCircle({ Offset center, double radius })
center圆弧中心点(相对于原点的偏移量)
radius圆弧半径(double)

 

class MyPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    var paint = Paint()
      ..isAntiAlias = true
      ..strokeWidth=0.0
      ..color=Colors.redAccent
      ..invertColors=false;

    Rect rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 100.0);
    canvas.drawArc(rect, 0.0, 2*pi/4, false, paint);

  }

  //在实际场景中正确利用此回调可以避免重绘开销,本示例我们简单的返回true
  @override
  bool shouldRepaint(CustomPainter oldDelegate) => true;
}

Rect.fromLTRB(this.left, this.top, this.right, this.bottom)
left矩形最左边相对于原点(0,0)偏移量
top矩形最顶部相对于原点(0,0)偏移量
right矩形最右边相对于原点(0,0)偏移量
bottom矩形最底部相对于原点(0,0)偏移量

 

class MyPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    var paint = Paint()
      ..isAntiAlias = true
      ..strokeWidth=0.0
      ..color=Colors.redAccent
      ..invertColors=false;
    
    Rect rect=Rect.fromLTRB(0.0, 0.0, size.width, size.height/2);
    canvas.drawArc(rect, 0.0, 2*pi/4, false, paint);

  }

  //在实际场景中正确利用此回调可以避免重绘开销,本示例我们简单的返回true
  @override
  bool shouldRepaint(CustomPainter oldDelegate) => true;
}

Rect.fromCenter({ Offset center, double width, double height })
center矩形中心相对于原点偏移量
width矩形宽度
height矩形高度

 

class MyPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    var paint = Paint()
      ..isAntiAlias = true
      ..strokeWidth=0.0
      ..color=Colors.redAccent
      ..invertColors=false;

    Rect rect=Rect.fromCenter(center: Offset(size.width/2,size.height/2),width:size.width/2,height:size.height/2);
    canvas.drawArc(rect, 0.0, 2*pi/4, false, paint);

  }

  //在实际场景中正确利用此回调可以避免重绘开销,本示例我们简单的返回true
  @override
  bool shouldRepaint(CustomPainter oldDelegate) => true;
}

Rect.fromLTWH(double left, double top, double width, double height)
left矩形最左边相对于原点偏移量
top矩形最顶部相对于原点偏移量
width矩形宽度
height矩形高度

 

class MyPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    var paint = Paint()
      ..isAntiAlias = true
      ..strokeWidth=0.0
      ..color=Colors.redAccent
      ..invertColors=false;

    Rect rect=Rect.fromLTWH(0.0, 0.0, size.width/2, size.height/2);
    canvas.drawArc(rect, 0.0, 2*pi/4, false, paint);

  }

  //在实际场景中正确利用此回调可以避免重绘开销,本示例我们简单的返回true
  @override
  bool shouldRepaint(CustomPainter oldDelegate) => true;
}

 Rect.fromPoints(Offset a, Offset b) : this.fromLTRB(
    math.min(a.dx, b.dx),
    math.min(a.dy, b.dy),
    math.max(a.dx, b.dx),
    math.max(a.dy, b.dy),
  );
a相对于原点x方向上的偏移范围
b相对于原点y方向上的偏移范围

 下面三种写法效果一样(主要是取x或y方向上偏移量的范围)

class MyPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    var paint = Paint()
      ..isAntiAlias = true
      ..strokeWidth=1.0
      ..color=Colors.redAccent
      ..invertColors=false;

    Rect rect=Rect.fromPoints(Offset(size.width, 0.0),Offset(0,size.height));
    canvas.drawArc(rect, 0.0, 2*pi/4, false, paint);

  }

  //在实际场景中正确利用此回调可以避免重绘开销,本示例我们简单的返回true
  @override
  bool shouldRepaint(CustomPainter oldDelegate) => true;
}

 

class MyPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    var paint = Paint()
      ..isAntiAlias = true
      ..strokeWidth=1.0
      ..color=Colors.redAccent
      ..invertColors=false;

    Rect rect=Rect.fromPoints(Offset(0.0, 0.0),Offset(size.width,size.height));
    canvas.drawArc(rect, 0.0, 2*pi/4, false, paint);

  }

  //在实际场景中正确利用此回调可以避免重绘开销,本示例我们简单的返回true
  @override
  bool shouldRepaint(CustomPainter oldDelegate) => true;
}
class MyPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    var paint = Paint()
      ..isAntiAlias = true
      ..strokeWidth=1.0
      ..color=Colors.redAccent
      ..invertColors=false;

    Rect rect=Rect.fromPoints(Offset(size.width, size.height),Offset(0.0,0.0));
    canvas.drawArc(rect, 0.0, 2*pi/4, false, paint);

  }

  //在实际场景中正确利用此回调可以避免重绘开销,本示例我们简单的返回true
  @override
  bool shouldRepaint(CustomPainter oldDelegate) => true;
}

 useCenter=false(绘制扇形)

class MyPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    var paint = Paint()
      ..isAntiAlias = true
      ..strokeWidth=0.0
      ..color=Colors.redAccent
      ..invertColors=false;

    Rect rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 100.0);
    canvas.drawArc(rect, 0.0, 2*pi/4, true, paint);

  }

  //在实际场景中正确利用此回调可以避免重绘开销,本示例我们简单的返回true
  @override
  bool shouldRepaint(CustomPainter oldDelegate) => true;
}

class MyPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    var paint = Paint()
      ..isAntiAlias = true
      ..strokeWidth=0.0
      ..color=Colors.redAccent
      ..invertColors=false;

    Rect rect=Rect.fromLTRB(0.0, 0.0, size.width, size.height/2);
    canvas.drawArc(rect, 0.0, 2*pi/4, true, paint);

  }

  //在实际场景中正确利用此回调可以避免重绘开销,本示例我们简单的返回true
  @override
  bool shouldRepaint(CustomPainter oldDelegate) => true;
}

Rect.lerp(在两个矩形之间进行线性插值)

源码:

 static Rect lerp(Rect a, Rect b, double t) {
    assert(t != null);
    if (a == null && b == null)
      return null;
    if (a == null)
      return Rect.fromLTRB(b.left * t, b.top * t, b.right * t, b.bottom * t);
    if (b == null) {
      final double k = 1.0 - t;
      return Rect.fromLTRB(a.left * k, a.top * k, a.right * k, a.bottom * k);
    }
    return Rect.fromLTRB(
      lerpDouble(a.left, b.left, t),
      lerpDouble(a.top, b.top, t),
      lerpDouble(a.right, b.right, t),
      lerpDouble(a.bottom, b.bottom, t),
    );
  }
double lerpDouble(num a, num b, double t) {
  if (a == null && b == null)
    return null;
  a ??= 0.0;
  b ??= 0.0;
  return a + (b - a) * t;
}
a矩形
b矩形
t时间值(0.0~1.0之间)

class MyPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    var paint = Paint()
      ..isAntiAlias = true
      ..strokeWidth=0.0
      ..color=Colors.redAccent
      ..invertColors=false;
    Rect a=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: size.width/3);
    Rect b=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: size.width/2);
    Rect rect=Rect.lerp(a, b, 1.0);
    canvas.drawArc(rect, 0.0, 2*pi/4, true, paint);

    rect=Rect.lerp(a, b, 0.2);
    canvas.drawArc(rect, 0.0, 2*pi/4, true, paint..color=Colors.blue);
  }

  //在实际场景中正确利用此回调可以避免重绘开销,本示例我们简单的返回true
  @override
  bool shouldRepaint(CustomPainter oldDelegate) => true;
}

class MyPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    var paint = Paint()
      ..isAntiAlias = true
      ..strokeWidth=0.0
      ..color=Colors.redAccent
      ..invertColors=false;
    Rect a=Rect.fromLTRB(80.0, 80.0, size.width-30.0, size.height-60.0);
    Rect b=Rect.fromLTRB(50.0, 50.0, size.width-20.0, size.height-20.0);
    Rect rect=Rect.lerp(a, b, 1.0);
    canvas.drawArc(rect, 0.0, 2*pi/4, true, paint);

    rect=Rect.lerp(a, b, 0.2);
    canvas.drawArc(rect, 0.0, 2*pi/4, true, paint..color=Colors.blue);
  }

  //在实际场景中正确利用此回调可以避免重绘开销,本示例我们简单的返回true
  @override
  bool shouldRepaint(CustomPainter oldDelegate) => true;
}

class MyPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    var paint = Paint()
      ..isAntiAlias = true
      ..strokeWidth=0.0
      ..color=Colors.redAccent
      ..invertColors=false;
    Rect a=Rect.fromLTRB(80.0, 80.0, size.width-80.0, size.height-80.0);
    Rect b=Rect.fromLTRB(50.0, 50.0, size.width-50.0, size.height-50.0);
    Rect rect=Rect.lerp(a, b, 1.0);
    canvas.drawArc(rect, 0.0, 2*pi/4, true, paint);

    rect=Rect.lerp(a, b, 0.7);
    canvas.drawArc(rect, 0.0, 2*pi/4, true, paint..color=Colors.blue);

    rect=Rect.lerp(a, b, 0.5);
    canvas.drawArc(rect, 0.0, 2*pi/4, true, paint..color=Colors.green);

    rect=Rect.lerp(a, b, 0.3);
    canvas.drawArc(rect, 0.0, 2*pi/4, true, paint..color=Colors.deepPurpleAccent);

    rect=Rect.lerp(a, b, 0.1);
    canvas.drawArc(rect, 0.0, 2*pi/4, true, paint..color=Colors.cyanAccent);

    rect=Rect.lerp(a, b, -0.2);
    canvas.drawArc(rect, 0.0, 2*pi/4, true, paint..color=Colors.lightGreenAccent);
  }

  //在实际场景中正确利用此回调可以避免重绘开销,本示例我们简单的返回true
  @override
  bool shouldRepaint(CustomPainter oldDelegate) => true;
}

画圆

class MyPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    var paint = Paint()
      ..isAntiAlias = true
      ..strokeWidth=0.0
      ..color=Colors.redAccent
      ..invertColors=false;

    Rect rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 100.0);
    canvas.drawArc(rect, 0.0, 2*pi, false, paint);

  }

  //在实际场景中正确利用此回调可以避免重绘开销,本示例我们简单的返回true
  @override
  bool shouldRepaint(CustomPainter oldDelegate) => true;
}

画同心圆

class MyPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    var paint = Paint()
      ..isAntiAlias = true
      ..strokeWidth=0.0
      ..color=Colors.redAccent
      ..invertColors=false;

    Rect rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 100.0);
    canvas.drawArc(rect, 0.0, 2*pi, false, paint);

    rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 80.0);
    canvas.drawArc(rect, 0.0, 2*pi, false, paint..color=Colors.blue);

    rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 60.0);
    canvas.drawArc(rect, 0.0, 2*pi, false, paint..color=Colors.deepPurple);

    rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 40.0);
    canvas.drawArc(rect, 0.0, 2*pi, false, paint..color=Colors.lightGreenAccent);

    rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 20.0);
    canvas.drawArc(rect, 0.0, 2*pi, false, paint..color=Colors.grey);

    rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 10.0);
    canvas.drawArc(rect, 0.0, 2*pi, false, paint..color=Colors.pinkAccent);

    rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 2.0);
    canvas.drawArc(rect, 0.0, 2*pi, false, paint..color=Colors.blueAccent);
  }

  //在实际场景中正确利用此回调可以避免重绘开销,本示例我们简单的返回true
  @override
  bool shouldRepaint(CustomPainter oldDelegate) => true;
}

绘制渐变圆

class MyPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    var paint = Paint()
      ..isAntiAlias = true
      ..strokeWidth=0.0
      ..invertColors=false;

    Rect rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 100.0);
    canvas.drawArc(rect, 0.0, 2*pi, false, paint..color=Colors.redAccent.withOpacity(0.2));

    rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 80.0);
    canvas.drawArc(rect, 0.0, 2*pi, false, paint..color=Colors.redAccent.withOpacity(0.3));

    rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 60.0);
    canvas.drawArc(rect, 0.0, 2*pi, false, paint..color=Colors.redAccent.withOpacity(0.4));

    rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 40.0);
    canvas.drawArc(rect, 0.0, 2*pi, false, paint..color=Colors.redAccent.withOpacity(0.5));

    rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 20.0);
    canvas.drawArc(rect, 0.0, 2*pi, false, paint..color=Colors.redAccent.withOpacity(0.6));
    

  }

  //在实际场景中正确利用此回调可以避免重绘开销,本示例我们简单的返回true
  @override
  bool shouldRepaint(CustomPainter oldDelegate) => true;
}

同心圆段拼凑正矩形 

class MyPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    var paint = Paint()
      ..isAntiAlias = true
      ..strokeWidth=0.0
      ..invertColors=false;

    Rect rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 100.0);
    canvas.drawArc(rect, 0, 2*pi/4, false, paint..color=Colors.redAccent);

     rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 100.0);
    canvas.drawArc(rect, 2*pi/4, 2*pi/4, false, paint..color=Colors.redAccent);

    rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 100.0);
    canvas.drawArc(rect, 2*pi/2, 2*pi/4, false, paint..color=Colors.redAccent);

    rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 100.0);
    canvas.drawArc(rect, 2*pi/4*3, 2*pi/4, false, paint..color=Colors.redAccent);


  }

  //在实际场景中正确利用此回调可以避免重绘开销,本示例我们简单的返回true
  @override
  bool shouldRepaint(CustomPainter oldDelegate) => true;
}

画不同颜色等分弧度

class MyPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    var paint = Paint()
      ..isAntiAlias = true
      ..strokeWidth=0.0
      ..invertColors=false;

    Rect rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 100.0);
    canvas.drawArc(rect, 0, 2*pi/4, true, paint..color=Colors.redAccent);

     rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 100.0);
    canvas.drawArc(rect, 2*pi/4, 2*pi/4, true, paint..color=Colors.blueAccent);

    rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 100.0);
    canvas.drawArc(rect, 2*pi/2, 2*pi/4, true, paint..color=Colors.grey);

    rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 100.0);
    canvas.drawArc(rect, 2*pi/4*3, 2*pi/4, true, paint..color=Colors.indigo);


  }

  //在实际场景中正确利用此回调可以避免重绘开销,本示例我们简单的返回true
  @override
  bool shouldRepaint(CustomPainter oldDelegate) => true;
}

五彩蜘蛛网

class MyPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    var paint = Paint()
      ..isAntiAlias = true
      ..strokeWidth=0.0
      ..invertColors=false;

    for(int i=0;i<10;i++){
      Rect rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 100.0);
      canvas.drawArc(rect, i*2*pi/10, 2*pi/10, false, paint..color=Colors.indigo);

      rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 90.0);
      canvas.drawArc(rect, i*2*pi/10, 2*pi/10, false, paint..color=Colors.grey);

      rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 80.0);
      canvas.drawArc(rect, i*2*pi/10, 2*pi/10, false, paint..color=Colors.blueAccent);

      rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 70.0);
      canvas.drawArc(rect, i*2*pi/10, 2*pi/10, false, paint..color=Colors.blue);

      rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 60.0);
      canvas.drawArc(rect, i*2*pi/10, 2*pi/10, false, paint..color=Colors.purpleAccent);

      rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 50.0);
      canvas.drawArc(rect, i*2*pi/10, 2*pi/10, false, paint..color=Colors.purple);

      rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 40.0);
      canvas.drawArc(rect, i*2*pi/10, 2*pi/10, false, paint..color=Colors.pinkAccent);

      rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 30.0);
      canvas.drawArc(rect, i*2*pi/10, 2*pi/10, false, paint..color=Colors.lightGreen);

      rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 20.0);
      canvas.drawArc(rect, i*2*pi/10, 2*pi/10, false, paint..color=Colors.cyan);

      rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 10.0);
      canvas.drawArc(rect, i*2*pi/10, 2*pi/10, false, paint..color=Colors.tealAccent);
    }
  }

  //在实际场景中正确利用此回调可以避免重绘开销,本示例我们简单的返回true
  @override
  bool shouldRepaint(CustomPainter oldDelegate) => true;
}

旋转五彩扇叶

class MyPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    var paint = Paint()
      ..isAntiAlias = true
      ..strokeWidth=0.0
      ..invertColors=false;

    for(int i=0;i<10;i++){
      Rect rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 100.0);
      canvas.drawArc(rect, i*2*2*pi/10, 2*pi/10, false, paint..color=Colors.indigo);

      rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 90.0);
      canvas.drawArc(rect, i*2*2*pi/10, 2*pi/10, false, paint..color=Colors.grey);

      rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 80.0);
      canvas.drawArc(rect, i*2*2*pi/10, 2*pi/10, false, paint..color=Colors.blueAccent);

      rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 70.0);
      canvas.drawArc(rect, i*2*2*pi/10, 2*pi/10, false, paint..color=Colors.blue);

      rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 60.0);
      canvas.drawArc(rect, i*2*2*2*pi/10, 2*pi/10, false, paint..color=Colors.purpleAccent);

      rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 50.0);
      canvas.drawArc(rect, i*2*2*2*pi/10, 2*pi/10, false, paint..color=Colors.purple);

      rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 40.0);
      canvas.drawArc(rect, i*2*2*pi/10, 2*pi/10, false, paint..color=Colors.pinkAccent);

      rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 30.0);
      canvas.drawArc(rect, i*2*2*pi/10, 2*pi/10, false, paint..color=Colors.lightGreen);

      rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 20.0);
      canvas.drawArc(rect, i*2*2*pi/10, 2*pi/10, false, paint..color=Colors.cyan);

      rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 10.0);
      canvas.drawArc(rect, i*2*2*pi/10, 2*pi/10, false, paint..color=Colors.tealAccent);
    }
  }

  //在实际场景中正确利用此回调可以避免重绘开销,本示例我们简单的返回true
  @override
  bool shouldRepaint(CustomPainter oldDelegate) => true;
}

 

评论 28
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

️ 邪神

你自己看着办,你喜欢打赏我就赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值