Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 使用颤振绘制路径时的奇怪性能问题_Flutter_Dart_Flutter Animation - Fatal编程技术网

Flutter 使用颤振绘制路径时的奇怪性能问题

Flutter 使用颤振绘制路径时的奇怪性能问题,flutter,dart,flutter-animation,Flutter,Dart,Flutter Animation,我试图在CustomPaint上绘制一些定点三角形,当我更改三角形的形状时,我会遇到性能问题。如果执行当前代码,刷新速度会很快 如果使用偏移量(x,y+8)更改零件,使用偏移量(x,y+4),三角形形状略有变化,但性能下降,刷新率低 有人知道为什么会这样吗?我如何解决这个问题 import 'dart:math' as Math; import 'package:flutter/material.dart'; void main() => runApp( Mate

我试图在CustomPaint上绘制一些定点三角形,当我更改三角形的形状时,我会遇到性能问题。如果执行当前代码,刷新速度会很快

如果使用偏移量(x,y+8)更改零件使用偏移量(x,y+4),三角形形状略有变化,但性能下降,刷新率低

有人知道为什么会这样吗?我如何解决这个问题

import 'dart:math' as Math;
import 'package:flutter/material.dart';

void main() =>
    runApp(
        MaterialApp(
          title: 'Demo',
          home: Scaffold(
            body: Shapes()
          ),
        )
    );


class Shapes extends StatefulWidget {
  @override
  _ShapesState createState() => new _ShapesState();
}

class _ShapesState extends State<Shapes> with SingleTickerProviderStateMixin{
  Animation<double> animation;
  AnimationController animationController;


  @override
  void initState() {
    super.initState();
    animationController = AnimationController(
      vsync: this,
      duration: Duration(seconds: 5),
    )..addListener(() => setState(() {}));
    animation = Tween<double>(
      begin: 50.0,
      end: 120.0,
    ).animate(animationController);

    animationController.forward();

    animation.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        animationController.repeat();
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return CustomPaint(painter: ShapesPainter());
  }
}

class ShapesPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint();
    paint.strokeWidth = 2;

    for (var i = 0; i < 1000; i++) {
      Math.Random rnd = new Math.Random();
      double x = 5 + rnd.nextInt(300 - 5).toDouble();
      double y = 5 + rnd.nextInt(1200 - 5).toDouble();

      final Path path = Path();
      path.addPolygon([
        Offset(x+6,y+8),
        Offset(x,y+-8),
        Offset(x-6,y+8),
        Offset(x,y+8),
      ], true);

      paint.color = Color(0xFF3f6cbf);
      paint.style = PaintingStyle.stroke;
      canvas.drawPath(path, paint);

      paint.color = Color.fromARGB(255, 255, 0, 0);
      paint.style = PaintingStyle.fill;
      canvas.drawPath(path, paint);

    }

  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) => true;
}
导入'dart:math'作为数学;
进口“包装:颤振/材料.省道”;
void main()=>
runApp(
材料聚丙烯(
标题:“演示”,
家:脚手架(
正文:形状()
),
)
);
类形状扩展了StatefulWidget{
@凌驾
_ShapeState createState()=>new_ShapeState();
}
类_shapeState使用SingleTickerProviderStateMixin扩展状态{
动画;
动画控制器;
@凌驾
void initState(){
super.initState();
animationController=animationController(
vsync:这个,,
持续时间:持续时间(秒数:5),
)..addListener(()=>setState((){}));
动画=吐温(
开始:50.0,
完:120.0,,
).设置动画(animationController);
animationController.forward();
animation.addStatusListener((状态){
如果(状态==AnimationStatus.completed){
animationController.repeat();
}
});
}
@凌驾
小部件构建(构建上下文){
返回CustomPaint(画家:ShapePaint());
}
}
类ShapesPaint扩展了CustomPainter{
@凌驾
空心油漆(帆布,尺寸){
最终油漆=油漆();
paint.strokeWidth=2;
对于(变量i=0;i<1000;i++){
Math.Random rnd=新的Math.Random();
double x=5+rnd.nextInt(300-5.toDouble();
双y=5+rnd.nextInt(1200-5).toDouble();
最终路径路径=路径();
path.addPolygon([
偏移量(x+6,y+8),
偏移量(x,y+-8),
偏移量(x-6,y+8),
偏移量(x,y+8),
],对);
paint.color=颜色(0xFF3f6cbf);
paint.style=PaintingStyle.stroke;
画布.绘制路径(路径,绘制);
paint.color=color.fromARGB(255,255,0,0);
paint.style=PaintingStyle.fill;
画布.绘制路径(路径,绘制);
}
}
@凌驾
bool shouldRepaint(CustomPainter oldDelegate)=>true;
}

Btw,您可以使用“看不见”在绘制三角形时如何准确地使用动画值。。。看起来您没有在任何地方使用tweened值…
Offset(x,y+-8)
假设是指
Offset(x,y)
?@AlexanderArendar感谢您的回复!未来将使用动画值!目前,我正试图解决三角形改变时性能缓慢的问题。您正在物理设备上测试?顺便说一句,您可以使用“看不见”在绘制三角形时如何准确地使用动画值。。。看起来您没有在任何地方使用tweened值…
Offset(x,y+-8)
假设是指
Offset(x,y)
?@AlexanderArendar感谢您的回复!未来将使用动画值!目前,我正试图解决三角形改变时性能缓慢的问题您在物理设备上测试吗?