コード例 #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 invertColors       = false,
            FilterMode filterMode   = FilterMode.Bilinear
            )
        {
            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;
                paint.color       = colorFilter.color;
                paint.blendMode   = colorFilter.blendMode;
            }

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

            paint.invertColors = invertColors;

            float  halfWidthDelta  = (outputSize.width - destinationSize.width) / 2.0f;
            float  halfHeightDelta = (outputSize.height - destinationSize.height) / 2.0f;
            float  dx = halfWidthDelta + 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;

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

            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();
            }
        }