Skip to content

Commit 503218c

Browse files
committed
Merge pull request flutter#762 from abarth/image_repeat
Implement ImageRepeat
2 parents 7eb387b + 9c00bc5 commit 503218c

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

packages/flutter/lib/src/painting/box_painter.dart

+38-6
Original file line numberDiff line numberDiff line change
@@ -573,14 +573,43 @@ enum ImageRepeat {
573573
noRepeat
574574
}
575575

576+
Iterable<Rect> _generateImageTileRects(Rect outputRect, Rect fundamentalRect, ImageRepeat repeat) sync* {
577+
if (repeat == ImageRepeat.noRepeat) {
578+
yield fundamentalRect;
579+
return;
580+
}
581+
582+
int startX = 0;
583+
int startY = 0;
584+
int stopX = 0;
585+
int stopY = 0;
586+
double strideX = fundamentalRect.width;
587+
double strideY = fundamentalRect.height;
588+
589+
if (repeat == ImageRepeat.repeat || repeat == ImageRepeat.repeatX) {
590+
startX = ((outputRect.left - fundamentalRect.left) / strideX).floor();
591+
stopX = ((outputRect.right - fundamentalRect.right) / strideX).ceil();
592+
}
593+
594+
if (repeat == ImageRepeat.repeat || repeat == ImageRepeat.repeatY) {
595+
startY = ((outputRect.top - fundamentalRect.top) / strideY).floor();
596+
stopY = ((outputRect.bottom - fundamentalRect.bottom) / strideY).ceil();
597+
}
598+
599+
for (int i = startX; i <= stopX; ++i) {
600+
for (int j = startY; j <= stopY; ++j)
601+
yield fundamentalRect.shift(new Offset(i * strideX, j * strideY));
602+
}
603+
}
604+
576605
/// Paint an image into the given rectangle in the canvas
577606
void paintImage({
578607
Canvas canvas,
579608
Rect rect,
580609
ui.Image image,
581610
ColorFilter colorFilter,
582611
ImageFit fit,
583-
repeat: ImageRepeat.noRepeat,
612+
ImageRepeat repeat: ImageRepeat.noRepeat,
584613
Rect centerSlice,
585614
double alignX,
586615
double alignY
@@ -640,18 +669,21 @@ void paintImage({
640669
// as we apply a nine-patch stretch.
641670
assert(sourceSize == inputSize);
642671
}
643-
// TODO(abarth): Implement |repeat|.
644672
Paint paint = new Paint()..isAntiAlias = false;
645673
if (colorFilter != null)
646674
paint.colorFilter = colorFilter;
647675
double dx = (outputSize.width - destinationSize.width) * (alignX ?? 0.5);
648676
double dy = (outputSize.height - destinationSize.height) * (alignY ?? 0.5);
649677
Point destinationPosition = rect.topLeft + new Offset(dx, dy);
650678
Rect destinationRect = destinationPosition & destinationSize;
651-
if (centerSlice == null)
652-
canvas.drawImageRect(image, Point.origin & sourceSize, destinationRect, paint);
653-
else
654-
canvas.drawImageNine(image, centerSlice, destinationRect, paint);
679+
if (centerSlice == null) {
680+
Rect sourceRect = Point.origin & sourceSize;
681+
for (Rect tileRect in _generateImageTileRects(rect, destinationRect, repeat))
682+
canvas.drawImageRect(image, sourceRect, tileRect, paint);
683+
} else {
684+
for (Rect tileRect in _generateImageTileRects(rect, destinationRect, repeat))
685+
canvas.drawImageNine(image, centerSlice, tileRect, paint);
686+
}
655687
}
656688

657689
/// A background image for a box.

packages/flutter/lib/src/rendering/image.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class RenderImage extends RenderBox {
2525
ColorFilter colorFilter,
2626
ImageFit fit,
2727
FractionalOffset alignment,
28-
repeat: ImageRepeat.noRepeat,
28+
ImageRepeat repeat: ImageRepeat.noRepeat,
2929
Rect centerSlice
3030
}) : _image = image,
3131
_width = width,

0 commit comments

Comments
 (0)