In Flutter, how to associate device pixel ratio with a canvas created from scratch?
final MyCustomPainter p = MyCustomPainter(_counter); ui.PictureRecorder recorder = ui.PictureRecorder(); Canvas canvas = Canvas(recorder); canvas.save(); canvas.scale(ui.window.devicePixelRatio); //其他绘制在这里开始 p.paint(canvas, const Size(SIZE, SIZE)); canvas.restore(); final imageSize = (SIZE * ui.window.devicePixelRatio).floor(); recorder.endRecording().toImage(imageSize, imageSize).then((image) { image.toByteData(format: ui.ImageByteFormat.png).then((ByteData? data) { if (data != null) { _image = data.buffer.asUint8List(); setState(() {}); } }); });
也就是说一定要先用
canvas.save(); canvas.scale(ui.window.devicePixelRatio);
来重制设备像素分辨率缩放
目前尚无回复