1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- import 'package:flutter/material.dart';
- import 'dart:ui' as ui;
- class CarotidVidPainter extends CustomPainter {
- CarotidVidPainter({required this.image, this.stdSize = const Size(599, 400)});
- ui.Image image;
- Size stdSize;
- @override
- void paint(Canvas canvas, Size size) async {
- canvas.save();
- final double stdScaleX = size.width / stdSize.width;
- final double stdScaleY = size.height / stdSize.height;
- final double stdScale = stdScaleX < stdScaleY ? stdScaleX : stdScaleY;
- final double scaleX = size.width / image.width;
- final double scaleY = size.height / image.height;
- final double scale = scaleX < scaleY ? scaleX : scaleY;
- bool needScale =
- image.width < stdSize.width && image.height < stdSize.height;
- final double finalScale = needScale ? stdScale : scale;
- final double offsetX = (size.width - image.width * finalScale) / 2;
- final double offsetY = (size.height - image.height * finalScale) / 2;
- canvas.drawImageRect(
- image,
- Rect.fromLTWH(0, 0, image.width.toDouble(), image.height.toDouble()),
- Rect.fromLTWH(offsetX, offsetY, image.width * finalScale,
- image.height * finalScale),
- Paint());
- // 小图像绘制一圈蓝色轮廓
- if (needScale) {
- canvas.drawRect(
- Rect.fromLTWH(offsetX, offsetY, image.width * finalScale,
- image.height * finalScale),
- Paint()
- ..color = Colors.blue
- ..strokeWidth = 1
- ..style = PaintingStyle.stroke);
- }
- canvas.restore();
- }
- @override
- bool shouldRepaint(covariant CarotidVidPainter oldDelegate) {
- return true;
- }
- }
|