我正在尝试将一个精灵绘制到CustomPaint小部件上。 Sprite来自一个网格中包含多个Sprite的图像,因此我需要能够指定源矩形(最好是与小部件的大小匹配的目标矩形)。
答案 0 :(得分:1)
我使用canvas.drawAtlas解决了这个问题:
import 'package:flutter/painting.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
import 'dart:ui' as ui;
class SquarePainter extends CustomPainter {
final ui.Image theImage;
SquarePainter({this.theImage});
@override
void paint(Canvas canvas, Size size) {
canvas.drawAtlas(
theImage,
[
/* Identity transform */
RSTransform.fromComponents(
rotation: 0.0,
scale: 1.0,
anchorX: 0.0,
anchorY: 0.0,
translateX: 0.0,
translateY: 0.0)
],
[
/* A 5x5 source rectangle within the image at position (10, 10) */
Rect.fromLTWH(10.0, 10.0, 5.0, 5.0)
],
[/* No need for colors */],
BlendMode.src,
null /* No need for cullRect */,
Paint());
}
@override
bool shouldRepaint(SquarePainter oldDelegate) => false;
@override
bool shouldRebuildSemantics(SquarePainter oldDelegate) => false;
}