class MyPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
var paint = Paint()
…isAntiAlias = true
…strokeWidth=0.0
…color=Colors.redAccent
…invertColors=false;
Rect rect=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: 100.0);
canvas.drawArc(rect, 0.0, 2*pi/4, true, paint);
}
//在实际场景中正确利用此回调可以避免重绘开销,本示例我们简单的返回true
@override
bool shouldRepaint(CustomPainter oldDelegate) => true;
}
class MyPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
var paint = Paint()
…isAntiAlias = true
…strokeWidth=0.0
…color=Colors.redAccent
…invertColors=false;
Rect rect=Rect.fromLTRB(0.0, 0.0, size.width, size.height/2);
canvas.drawArc(rect, 0.0, 2*pi/4, true, paint);
}
//在实际场景中正确利用此回调可以避免重绘开销,本示例我们简单的返回true
@override
bool shouldRepaint(CustomPainter oldDelegate) => true;
}
Rect.lerp(在两个矩形之间进行线性插值)
源码:
static Rect lerp(Rect a, Rect b, double t) {
assert(t != null);
if (a == null && b == null)
return null;
if (a == null)
return Rect.fromLTRB(b.left * t, b.top * t, b.right * t, b.bottom * t);
if (b == null) {
final double k = 1.0 - t;
return Rect.fromLTRB(a.left * k, a.top * k, a.right * k, a.bottom * k);
}
return Rect.fromLTRB(
lerpDouble(a.left, b.left, t),
lerpDouble(a.top, b.top, t),
lerpDouble(a.right, b.right, t),
lerpDouble(a.bottom, b.bottom, t),
);
}
double lerpDouble(num a, num b, double t) {
if (a == null && b == null)
return null;
a ??= 0.0;
b ??= 0.0;
return a + (b - a) * t;
}
a | 矩形 |
b | 矩形 |
t | 时间值(0.0~1.0之间) |
class MyPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
var paint = Paint()
…isAntiAlias = true
…strokeWidth=0.0
…color=Colors.redAccent
…invertColors=false;
Rect a=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: size.width/3);
Rect b=Rect.fromCircle(center: Offset(size.width/2,size.height/2),radius: size.width/2);
Rect rect=Rect.lerp(a, b, 1.0);
canvas.drawArc(rect, 0.0, 2*pi/4, true, paint);
rect=Rect.lerp(a, b, 0.2);
canvas.drawArc(rect, 0.0, 2*pi/4, true, paint…color=Colors.blue);
}
//在实际场景中正确利用此回调可以避免重绘开销,本示例我们简单的返回true
@override
bool shouldRepaint(CustomPainter oldDelegate) => true;
}
class MyPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
var paint = Paint()
…isAntiAlias = true
…strokeWidth=0.0
…color=Colors.redAccent
…invertColors=false;
Rect a=Rect.fromLTRB(8