Paints an image into the given rectangle in the canvas.
canvas
: The canvas onto which the image will be painted.rect
: The region of the canvas into which the image will be painted. The image might not fill the entire rectangle (e.g., depending on thefit
).image
: The image to paint onto the canvas.colorFilter
: If non-null, the color filter to apply when painting the image.fit
: How the image should be inscribed intorect
. If null, the default behavior depends oncenterSlice
. IfcenterSlice
is also null, the default behavior isImageFit.scaleDown
. IfcenterSlice
is non-null, the default behavior isImageFit.fill
. See ImageFit for details.repeat
: If the image does not fillrect
, whether and how the image should be repeated to fillrect
. By default, the image is not repeated. See ImageRepeat for details.centerSlice
: The image is drawn in nine portions described by splitting the image by drawing two horizontal lines and two vertical lines, wherecenterSlice
describes the rectangle formed by the four points where these four lines intersect each other. (This forms a 3-by-3 grid of regions, the center region being described bycenterSlice
.) The four regions in the corners are drawn, without scaling, in the four corners of the destination rectangle defined by applyingfit
. The remaining five regions are drawn by stretching them to fit such that they exactly cover the destination rectangle while maintaining their relative positions.alignment
: How the destination rectangle defined by applyingfit
is aligned withinrect
. For example, iffit
isImageFit.contain
andalignment
isFractionalOffset.bottomRight
, the image will be as large as possible withinrect
and placed with its bottom right corner at the bottom right corner ofrect
.
Source
void paintImage({ @required Canvas canvas, @required Rect rect, @required ui.Image image, ColorFilter colorFilter, ImageFit fit, ImageRepeat repeat: ImageRepeat.noRepeat, Rect centerSlice, FractionalOffset alignment }) { assert(canvas != null); assert(image != null); Size outputSize = rect.size; Size inputSize = new Size(image.width.toDouble(), image.height.toDouble()); Offset sliceBorder; if (centerSlice != null) { sliceBorder = new Offset( centerSlice.left + inputSize.width - centerSlice.right, centerSlice.top + inputSize.height - centerSlice.bottom ); outputSize -= sliceBorder; inputSize -= sliceBorder; } fit ??= centerSlice == null ? ImageFit.scaleDown : ImageFit.fill; assert(centerSlice == null || (fit != ImageFit.none && fit != ImageFit.cover)); final FittedSizes fittedSizes = applyImageFit(fit, inputSize, outputSize); final Size sourceSize = fittedSizes.source; Size destinationSize = fittedSizes.destination; if (centerSlice != null) { outputSize += sliceBorder; destinationSize += sliceBorder; // We don't have the ability to draw a subset of the image at the same time // as we apply a nine-patch stretch. assert(sourceSize == inputSize); } if (repeat != ImageRepeat.noRepeat && destinationSize == outputSize) { // There's no need to repeat the image because we're exactly filling the // output rect with the image. repeat = ImageRepeat.noRepeat; } Paint paint = new Paint()..isAntiAlias = false; if (colorFilter != null) paint.colorFilter = colorFilter; if (sourceSize != destinationSize) { // Use the "low" quality setting to scale the image, which corresponds to // bilinear interpolation, rather than the default "none" which corresponds // to nearest-neighbor. paint.filterQuality = FilterQuality.low; } double dx = (outputSize.width - destinationSize.width) * (alignment?.dx ?? 0.5); double dy = (outputSize.height - destinationSize.height) * (alignment?.dy ?? 0.5); Point destinationPosition = rect.topLeft + new Offset(dx, dy); Rect destinationRect = destinationPosition & destinationSize; if (repeat != ImageRepeat.noRepeat) { canvas.save(); canvas.clipRect(rect); } if (centerSlice == null) { final Rect sourceRect = (alignment ?? FractionalOffset.center).inscribe( fittedSizes.source, Point.origin & inputSize ); for (Rect tileRect in _generateImageTileRects(rect, destinationRect, repeat)) canvas.drawImageRect(image, sourceRect, tileRect, paint); } else { for (Rect tileRect in _generateImageTileRects(rect, destinationRect, repeat)) canvas.drawImageNine(image, centerSlice, tileRect, paint); } if (repeat != ImageRepeat.noRepeat) canvas.restore(); }