![](/img/trans.png)
[英]new to flutter. I'm want to go to a webapi based on url passed not hard code path
[英]Flutter. Path scaling
如何更改这部分代码中路径的大小? 绘图时,路径会超出手机屏幕。 我不应该自己绘制路径。
Path path = getPath(); //the path is given dynamically
canvas.drawPath(
path,
Paint()
..style = PaintingStyle.stroke
..color = Colors.black
..strokeWidth = 2.0);
我尝试使用方法path.transform
。 但是路径没有正确转换。
调整代码如下。
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;
}
}
取消注释缩放线以查看效果。
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.