在Dart中定期绘制到画布

时间:2017-05-13 19:11:20

标签: canvas dart flutter

嗨,我对Dart很新。我想要实现的是定期在画布上绘制,留下之前的东西。

import 'dart:developer';
import 'dart:typed_data';
import 'dart:ui' as ui;


Duration timeBase;
double x = 0.0;
double y = 0.0;
int i = 0;

ui.Image image;

void beginFrame(Duration timeStamp){
  Timeline.timeSync('beginFrame',(){
    if(timeBase == null)timeBase = timeStamp;
    final double devicePixelRatio = ui.window.devicePixelRatio;
    // paint
    ui.Rect paintBounds = ui.Offset.zero & ui.window.physicalSize;
    ui.PictureRecorder recorder = new ui.PictureRecorder();
    ui.Canvas canvas = new ui.Canvas(recorder,paintBounds);
    if(image != null){
      canvas.drawImage(image,ui.Offset.zero,new ui.Paint());
    }

    var paint = new ui.Paint();
    paint.color = const ui.Color.fromARGB(255,0,255,0);
    paint.strokeCap = ui.StrokeCap.round;
    paint.strokeWidth = 17.0;
    canvas.drawLine(new ui.Offset(x,y),new ui.Offset(x + 25,y + 25),paint);
    x = x + 1;
    if(x > ui.window.physicalSize.width / devicePixelRatio){
      x = x - ui.window.physicalSize.width / devicePixelRatio;
    }
    y = y + 1;
    if(y > ui.window.physicalSize.height / devicePixelRatio){
      y = y - ui.window.physicalSize.height / devicePixelRatio;
    }


    ui.Picture picture = recorder.endRecording();
    image = picture.toImage(ui.window.physicalSize.width.toInt(),ui.window.physicalSize.height.toInt());


    // composite
    Float64List deviceTransform = new Float64List(16)
      ..[0] = devicePixelRatio
      ..[5] = devicePixelRatio
      ..[10] = 1.0
      ..[15] = 1.0;
    ui.SceneBuilder sceneBuilder = new ui.SceneBuilder()
      ..pushTransform(deviceTransform)
      ..addPicture(ui.Offset.zero,picture)
      ..pop();
    ui.window.render(sceneBuilder.build());
  });
  ui.window.scheduleFrame();
}
void main() {
  ui.window.onBeginFrame = beginFrame;
  ui.window.scheduleFrame();
}

问题是以前的图像,使用方法drawImage绘制时没有出现在画布上。 感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

这是一个fixed的错误。如果您在主分支上,请运行flutter upgrade,您应该很高兴。

这就是它的样子:screenshot