Пример #1
0
        public static void paintImage(
            Canvas canvas               = null,
            Rect rect                   = null,
            Image image                 = null,
            float scale                 = 1.0f,
            ColorFilter colorFilter     = null,
            BoxFit?fit                  = null,
            Alignment alignment         = null,
            Rect centerSlice            = null,
            ImageRepeat repeat          = ImageRepeat.noRepeat,
            bool flipHorizontally       = false,
            bool invertColors           = false,
            FilterQuality filterQuality = FilterQuality.low
            )
        {
            D.assert(canvas != null);
            D.assert(rect != null);
            D.assert(image != null);
            alignment = alignment ?? Alignment.center;

            if (rect.isEmpty)
            {
                return;
            }

            Size   outputSize  = rect.size;
            Size   inputSize   = new Size(image.width, image.height);
            Offset sliceBorder = null;

            if (centerSlice != null)
            {
                sliceBorder = new Offset(
                    centerSlice.left + inputSize.width - centerSlice.right,
                    centerSlice.top + inputSize.height - centerSlice.bottom
                    );
                outputSize -= sliceBorder;
                inputSize  -= sliceBorder;
            }

            fit = fit ?? (centerSlice == null ? BoxFit.scaleDown : BoxFit.fill);
            D.assert(centerSlice == null || (fit != BoxFit.none && fit != BoxFit.cover),
                     () => $"centerSlice was used with a BoxFit {fit} that is not supported.");
            FittedSizes fittedSizes     = FittedSizes.applyBoxFit(fit.Value, inputSize / scale, outputSize);
            Size        sourceSize      = fittedSizes.source * scale;
            Size        destinationSize = fittedSizes.destination;

            if (centerSlice != null)
            {
                outputSize      += sliceBorder;
                destinationSize += sliceBorder;
                D.assert(sourceSize == inputSize,
                         () =>
                         $"centerSlice was used with a BoxFit {fit} that does not guarantee that the image is fully visible.");
            }

            if (repeat != ImageRepeat.noRepeat && destinationSize == outputSize)
            {
                repeat = ImageRepeat.noRepeat;
            }

            Paint paint = new Paint();

            if (colorFilter != null)
            {
                paint.colorFilter = colorFilter;
            }

            if (sourceSize != destinationSize)
            {
                paint.filterQuality = filterQuality;
            }

            paint.invertColors = invertColors;

            float  halfWidthDelta  = (outputSize.width - destinationSize.width) / 2.0f;
            float  halfHeightDelta = (outputSize.height - destinationSize.height) / 2.0f;
            float  dx = halfWidthDelta + (flipHorizontally ? -alignment.x : alignment.x) * halfWidthDelta;
            float  dy = halfHeightDelta + alignment.y * halfHeightDelta;
            Offset destinationPosition = rect.topLeft.translate(dx, dy);
            Rect   destinationRect     = destinationPosition & destinationSize;
            bool   needSave            = repeat != ImageRepeat.noRepeat || flipHorizontally;

            if (needSave)
            {
                canvas.save();
            }

            if (flipHorizontally)
            {
                float dxInside = -(rect.left + rect.width / 2.0f);
                canvas.translate(-dxInside, 0.0f);
                canvas.scale(-1.0f, 1.0f);
                canvas.translate(dxInside, 0.0f);
            }

            if (repeat != ImageRepeat.noRepeat)
            {
                canvas.clipRect(rect);
            }

            if (centerSlice == null)
            {
                Rect sourceRect = alignment.inscribe(
                    sourceSize, Offset.zero & inputSize
                    );
                if (repeat == ImageRepeat.noRepeat)
                {
                    canvas.drawImageRect(image, sourceRect, destinationRect, paint);
                }
                else
                {
                    foreach (Rect tileRect in _generateImageTileRects(rect, destinationRect, repeat))
                    {
                        canvas.drawImageRect(image, sourceRect, tileRect, paint);
                    }
                }
            }
            else
            {
                if (repeat == ImageRepeat.noRepeat)
                {
                    canvas.drawImageNine(image, centerSlice, destinationRect, paint);
                }
                else
                {
                    foreach (Rect tileRect in _generateImageTileRects(rect, destinationRect, repeat))
                    {
                        canvas.drawImageNine(image, centerSlice, tileRect, paint);
                    }
                }
            }

            if (needSave)
            {
                canvas.restore();
            }
        }