vid_painter.dart 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import 'package:flutter/material.dart';
  2. import 'dart:ui' as ui;
  3. class VidPainter extends CustomPainter {
  4. VidPainter(
  5. {required this.image,
  6. required this.frameIndex,
  7. this.colorFilterMatrix = const <double>[
  8. 1, 0, 0, 0, 0, // red
  9. 0, 1, 0, 0, 0, // green
  10. 0, 0, 1, 0, 0, // blue
  11. 0, 0, 0, 1, 0, // alpha
  12. ]});
  13. ui.Image image;
  14. int frameIndex;
  15. List<double> colorFilterMatrix;
  16. @override
  17. void paint(Canvas canvas, Size size) async {
  18. canvas.save();
  19. final double scaleX = size.width / image.width;
  20. final double scaleY = size.height / image.height;
  21. final double scale = scaleX < scaleY ? scaleX : scaleY;
  22. final double offsetX = (size.width - image.width * scale) / 2;
  23. final double offsetY = (size.height - image.height * scale) / 2;
  24. canvas.drawImageRect(
  25. image,
  26. Rect.fromLTWH(0, 0, image.width.toDouble(), image.height.toDouble()),
  27. Rect.fromLTWH(
  28. offsetX, offsetY, image.width * scale, image.height * scale),
  29. Paint()
  30. ..colorFilter = ColorFilter.matrix(colorFilterMatrix)
  31. ..filterQuality = FilterQuality.high);
  32. canvas.restore();
  33. }
  34. @override
  35. bool shouldRepaint(covariant VidPainter oldDelegate) {
  36. return oldDelegate.frameIndex != frameIndex ||
  37. oldDelegate.colorFilterMatrix != colorFilterMatrix;
  38. }
  39. }