我试图了解 customPaint 的工作原理,我想在画布上逐帧绘制自定义动画。
我可以通过每 1/60 秒重新绘制小部件来使其工作,但这听起来效率不高。我想每 1/60 秒渲染一次 CustomPainter,但这似乎不起作用。非常感谢任何注释或评论,以帮助我理解我应该如何实现这一目标。谢谢。
这是我正在使用的代码:
class CustomAnimatedWidgetState extends State<CustomAnimatedWidget> {
CustomPaint _paint=null;
MyCustomPainter _painter=null;
double animationFrame=0;
void tick() {
//called eery 1/60 seconds
animationFrame+=1/60;
_painter.setAnimationFrame(animationFrame);
_paint.METHOD_I_DONT_KNOW_TO_FORCE_REDRAW();
// I want to avoid setState({animationFrame+=1/60;}); which works actually, but that doesn't sound very efficient to redraw the widget every 1/60 seconds, unless it's the right way to do it ?
}
@override
Widget build(BuildContext context) {
//developer.log('axis='+axis.toString(), name: 'DEBUG');
_painter=MyCustomPainter();
_painter.setAnimationFrame(animationFrame);
_paint=CustomPaint(
painter: _painter,
child: Container(),
);
return _paint;
}
}