我刚开始接触自定义画家,我已经创建了一个基本的十字架,但不知道如何使角落变圆并使颜色成为渐变?
似乎需要一个渐变createShader
,rect
但我没有,因为我使用了路径。
我也想把十字架的拐角弄圆一点,但不知道是怎么做的。我考虑过创建矩形,但似乎也无法创建倾斜的矩形。
class CrossPainter extends CustomPainter {
CrossPainter({this.bodyColour, this.strokeColour, this.stroke});
final Color bodyColour;
final Color strokeColour;
final double stroke;
@override
void paint(Canvas canvas, Size size) {
Paint paint = Paint();
paint
..color = strokeColour
..style = PaintingStyle.stroke
..strokeCap = StrokeCap.round
..strokeWidth = stroke;
Paint paint1 = Paint();
paint1
..color = bodyColour
..style = PaintingStyle.fill
..strokeWidth = 0;
double width = size.width;
double height = size.height;
Path path = Path();
path.addPolygon([Offset.zero, Offset(width / 4, 0), Offset(width, height), Offset(width - (width / 4), height)], true);
Path path2 = Path();
path2.addPolygon(
[Offset(width, 0), Offset(width - (width / 4), 0), Offset(0, height), Offset(0 + (width / 4), height)], true);
canvas.drawPath(path, paint1);
canvas.drawPath(path2, paint1);
// canvas.drawPath(path, paint); //border
// canvas.drawPath(path2, paint); //border
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
return true;
}
}
我也有paint1
它添加了一个边框,但它看起来不太好,因为每个多边形都是一个单独的对象。