使用颤动绘制时的性能问题 [英] performance issue in drawing using flutter

查看:76
本文介绍了使用颤动绘制时的性能问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用gestuereDetector检测偏移量,并使用CustomePaint绘制颤动中的偏移量.但是绘制后,绘制的性能会变慢,因此请帮助我解决此问题.

I'm using gestuereDetector to detect offset and CustomePaint to paint the offset in flutter. but after drawing the performance of drawing is becoming slow so please help me to solve this issue.

如何提高效率.我的代码如下所示

How can I make it more efficient. My code just follows the below

 Widget build(BuildContext context) {
    _currentPainter = new DrawPainting(_points);

    return new Container(
      child: new ConstrainedBox(
        constraints: const BoxConstraints.expand(),
        child: new GestureDetector(
          onPanUpdate: (DragUpdateDetails details) {
            setState(() {
              RenderBox referenceBox = context.findRenderObject();

              Offset localPosition =
              referenceBox.globalToLocal(details.globalPosition);
              _points = new List.from(_points)..add(localPosition);
            });
          },
          onPanEnd: (DragEndDetails details) =>_points.add(null),
          child: new CustomPaint(
            painter: _currentPainter,

          ),
        ),
      ),
    );
  }


class DrawPainting extends CustomPainter {
  List<Offset> points = [];
  Canvas _lastCanvas;
  Size _lastSize;
  DrawPainting(points){

    this.points = points;
  }

  void paint(Canvas canvas, Size size) {
    print({"the main paint is called .... ": {"size" : size}});
    _lastCanvas = canvas;
    _lastSize = size;
    Paint paint = new Paint()
      ..color = Colors.black
      ..strokeCap = StrokeCap.round
      ..strokeWidth = 8.0;


    for (int i = 0; i < points.length - 1; i++) {
      if (points[i] != null &&
          points[i + 1] != null &&
          (points[i].dx >= 0 &&
              points[i].dy >= 0 &&
              points[i].dx < size.width &&
              points[i].dy < size.height) &&
          (points[i + 1].dx >= 0 &&
              points[i + 1].dy >= 0 &&
              points[i + 1].dx < size.width &&
              points[i + 1].dy < size.height)){
        canvas.drawLine(points[i], points[i + 1], paint);
      }
    }
  }
  bool shouldRepaint(DrawPainting other) => other.points != points;
}

推荐答案

通过使用 drawPath ()而不是使用 setState ,可以解决使用Flutter的绘图应用程序中的性能问题. >对于每个要更新的点,请使用 NotifyListener ()进行刷新,这将比setState效率更高.

The performance issue in drawing app using flutter is resolved by using drawPath() and instead of using setState for each points to update use NotifyListener() to refresh than it will be more efficient than setState.

这篇关于使用颤动绘制时的性能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆