Correctly handle centerSlice with resoultion-aware assets. (#68325)

This commit is contained in:
Cyp
2020-10-23 06:57:03 +02:00
committed by GitHub
parent 1edec6fc20
commit 45e02daf22
2 changed files with 28 additions and 8 deletions

View File

@@ -448,12 +448,9 @@ void paintImage({
Size inputSize = Size(image.width.toDouble(), image.height.toDouble());
Offset? sliceBorder;
if (centerSlice != null) {
sliceBorder = Offset(
centerSlice.left + inputSize.width - centerSlice.right,
centerSlice.top + inputSize.height - centerSlice.bottom,
);
sliceBorder = inputSize / scale - centerSlice.size as Offset;
outputSize = outputSize - sliceBorder as Size;
inputSize = inputSize - sliceBorder as Size;
inputSize = inputSize - sliceBorder * scale as Size;
}
fit ??= centerSlice == null ? BoxFit.scaleDown : BoxFit.fill;
assert(centerSlice == null || (fit != BoxFit.none && fit != BoxFit.cover));
@@ -559,7 +556,7 @@ void paintImage({
}
}
final bool needSave = repeat != ImageRepeat.noRepeat || flipHorizontally;
final bool needSave = centerSlice != null || repeat != ImageRepeat.noRepeat || flipHorizontally;
if (needSave)
canvas.save();
if (repeat != ImageRepeat.noRepeat)
@@ -581,11 +578,12 @@ void paintImage({
canvas.drawImageRect(image, sourceRect, tileRect, paint);
}
} else {
canvas.scale(1 / scale);
if (repeat == ImageRepeat.noRepeat) {
canvas.drawImageNine(image, centerSlice, destinationRect, paint);
canvas.drawImageNine(image, _scaleRect(centerSlice, scale), _scaleRect(destinationRect, scale), paint);
} else {
for (final Rect tileRect in _generateImageTileRects(rect, destinationRect, repeat))
canvas.drawImageNine(image, centerSlice, tileRect, paint);
canvas.drawImageNine(image, _scaleRect(centerSlice, scale), _scaleRect(tileRect, scale), paint);
}
}
if (needSave)
@@ -619,3 +617,5 @@ Iterable<Rect> _generateImageTileRects(Rect outputRect, Rect fundamentalRect, Im
yield fundamentalRect.shift(Offset(i * strideX, j * strideY));
}
}
Rect _scaleRect(Rect rect, double scale) => Rect.fromLTRB(rect.left * scale, rect.top * scale, rect.right * scale, rect.bottom * scale);

View File

@@ -108,6 +108,26 @@ void main() {
FlutterError.onError = oldFlutterError;
});
test('centerSlice with scale ≠ 1', () async {
final TestCanvas canvas = TestCanvas();
paintImage(
canvas: canvas,
rect: const Rect.fromLTRB(10, 20, 430, 420),
image: image300x300,
scale: 2.0,
centerSlice: const Rect.fromLTRB(50, 40, 250, 260),
);
final Invocation command = canvas.invocations.firstWhere((Invocation invocation) {
return invocation.memberName == #drawImageNine;
});
expect(command, isNotNull);
expect(command.positionalArguments[0], equals(image300x300));
expect(command.positionalArguments[1], equals(const Rect.fromLTRB(100.0, 80.0, 500.0, 520.0)));
expect(command.positionalArguments[2], equals(const Rect.fromLTRB(20.0, 40.0, 860.0, 840.0)));
});
testWidgets('Reports Image painting', (WidgetTester tester) async {
late ImageSizeInfo imageSizeInfo;
int count = 0;