carotid_vid_painter.dart 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import 'package:flutter/material.dart';
  2. import 'dart:ui' as ui;
  3. class CarotidVidPainter extends CustomPainter {
  4. CarotidVidPainter({required this.image, this.stdSize = const Size(599, 400)});
  5. ui.Image image;
  6. Size stdSize;
  7. @override
  8. void paint(Canvas canvas, Size size) async {
  9. canvas.save();
  10. final double stdScaleX = size.width / stdSize.width;
  11. final double stdScaleY = size.height / stdSize.height;
  12. final double stdScale = stdScaleX < stdScaleY ? stdScaleX : stdScaleY;
  13. final double scaleX = size.width / image.width;
  14. final double scaleY = size.height / image.height;
  15. final double scale = scaleX < scaleY ? scaleX : scaleY;
  16. bool needScale =
  17. image.width < stdSize.width && image.height < stdSize.height;
  18. final double finalScale = needScale ? stdScale : scale;
  19. final double offsetX = (size.width - image.width * finalScale) / 2;
  20. final double offsetY = (size.height - image.height * finalScale) / 2;
  21. canvas.drawImageRect(
  22. image,
  23. Rect.fromLTWH(0, 0, image.width.toDouble(), image.height.toDouble()),
  24. Rect.fromLTWH(offsetX, offsetY, image.width * finalScale,
  25. image.height * finalScale),
  26. Paint());
  27. // 小图像绘制一圈蓝色轮廓
  28. if (needScale) {
  29. canvas.drawRect(
  30. Rect.fromLTWH(offsetX, offsetY, image.width * finalScale,
  31. image.height * finalScale),
  32. Paint()
  33. ..color = Colors.blue
  34. ..strokeWidth = 1
  35. ..style = PaintingStyle.stroke);
  36. }
  37. canvas.restore();
  38. }
  39. @override
  40. bool shouldRepaint(covariant CarotidVidPainter oldDelegate) {
  41. return true;
  42. }
  43. }