简体   繁体   English

Flutter.路径缩放

[英]Flutter. Path scaling

How can I change the size of path in this section of code?如何更改这部分代码中路径的大小? When drawing, the path goes beyond the phone screen.绘图时,路径会超出手机屏幕。 I should not have access to drawing the paths themselves.我不应该自己绘制路径。

Path path = getPath();  //the path is given dynamically

canvas.drawPath(
  path,
  Paint()
    ..style = PaintingStyle.stroke
    ..color = Colors.black
    ..strokeWidth = 2.0);

I tried to use the method path.transform .我尝试使用方法path.transform But the path is not transformed correctly.但是路径没有正确转换。

The code for adjustment is as follows.调整代码如下。

main() => runApp(MaterialApp(home: SO()));

class SO extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Container(
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height,
        child: CustomPaint(
          painter: SOP(),
        ),
      ),
    );
  }
}

class SOP extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    Path p = getPath();

    Rect b = p.getBounds();
    var path_width = b.width;
    var path_height = b.height;
    var screen_width = size.width;
    var screen_height = size.height;
    var x_scale = screen_width / path_width;
    var y_scale = screen_height / path_height;

    //UNCOMMENT the following line to see the scaling effect
//    canvas.scale(x_scale, y_scale);

    canvas.drawPath(p, Paint()..color = Colors.red);
  }

  Path getPath() {
    Path p = Path();
    double w = 100;
    double h = 100;
    p.moveTo(0, 0);
    p.lineTo(0, h);
    p.lineTo(w, h);
    p.lineTo(w, 0);
    p.close();
    return p;
  }

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

Uncomment the scaling line to see the effect.取消注释缩放线以查看效果。

Doc proposed a solution by scaling the canvas. Another way would be to scale the path by using the transform method: Doc 通过缩放 canvas 提出了一个解决方案。另一种方法是使用 transform 方法缩放路径:

var scalingMatrix = Float64List.fromList(
    [size.width, 0, 0, 0,
    0, size.height, 0, 0,
    0, 0, 1, 0,
    0, 0, 0, 1]);
path = path.transform(scalingMatrix);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM