1

所以我一直在尝试调整 a 的大小CustomPainter并让它看起来像是像素艺术,但我没能做到。

// for example, I have this widget, that sets an specific size to a CustomPainter,
// in this case width and height are 16

         SizedBox(
            width: width*scaleFactor,
            height: height*scaleFactor,
            child: ClipRect(
              child: CustomPaint(
                size: Size(width*scaleFactor, height*scaleFactor),
                painter: MyCustomPainter(scaleFactor),
              ),
            ),
          );

// and this would be the custom Painter

class MyCustomPainter extends CustomPainter{

final double scaleFactor;
  MyCustomPainter(this.scaleFactor);

  @override
  void paint(Canvas canvas, Size size) {
    canvas.scale(scaleFactor);
    canvas.drawPaint(Paint()..color = Colors.white70);
    canvas.drawLine(const Offset(4,4), const Offset(12,12), Paint()
      ..color = Colors.black
      ..style = PaintingStyle.stroke
      ..strokeWidth = 1
      ..isAntiAlias = false);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) => true;

}

如果我将 scaleFactor 设置为 1,我当然会得到我想要的结果

customPainter_scale_1

如果我改变 scaleFactor 我会得到这样的结果:

在此处输入图像描述

但我实际上希望它喜欢的是这样的:

在此处输入图像描述

我什至尝试过使用Transform.scale小部件(甚至没有改变FilterQuality帮助),甚至在画布内设置了一个比例矩阵,canvas.transform()但我没有得到任何好的结果。

4

0 回答 0