示例#1
0
        protected override void OnDraw(Canvas canvas)
        {
            base.OnDraw(canvas);

            if (_path == null)
            {
                return;
            }

            AMatrix transformMatrix = CreateMatrix();

            _path.Transform(transformMatrix);
            transformMatrix.MapRect(_pathFillBounds);
            transformMatrix.MapRect(_pathStrokeBounds);

            if (_fill != null)
            {
                _drawable.Paint.SetStyle(Paint.Style.Fill);
                _drawable.Paint.Color = _fill;
                _drawable.Draw(canvas);
                _drawable.Paint.SetShader(null);
            }

            if (_stroke != null)
            {
                _drawable.Paint.SetStyle(Paint.Style.Stroke);
                _drawable.Paint.Color = _stroke;
                _drawable.Draw(canvas);
                _drawable.Paint.SetShader(null);
            }

            AMatrix inverseTransformMatrix = new AMatrix();

            transformMatrix.Invert(inverseTransformMatrix);
            _path.Transform(inverseTransformMatrix);
            inverseTransformMatrix.MapRect(_pathFillBounds);
            inverseTransformMatrix.MapRect(_pathStrokeBounds);
        }
示例#2
0
        protected override void OnDraw(Canvas canvas)
        {
            base.OnDraw(canvas);

            if (_path == null)
            {
                return;
            }

            AMatrix transformMatrix = CreateMatrix();

            _path.Transform(transformMatrix);
            transformMatrix.MapRect(_pathFillBounds);
            transformMatrix.MapRect(_pathStrokeBounds);

            if (_fill != null)
            {
                _drawable.Paint.SetStyle(Paint.Style.Fill);

                if (_fill is GradientBrush fillGradientBrush)
                {
                    if (fillGradientBrush is LinearGradientBrush linearGradientBrush)
                    {
                        _fillShader = CreateLinearGradient(linearGradientBrush, _pathFillBounds);
                    }

                    if (fillGradientBrush is RadialGradientBrush radialGradientBrush)
                    {
                        _fillShader = CreateRadialGradient(radialGradientBrush, _pathFillBounds);
                    }

                    _drawable.Paint.SetShader(_fillShader);
                }
                else
                {
                    AColor fillColor = Colors.Transparent.ToAndroid();

                    if (_fill is SolidColorBrush solidColorBrush && solidColorBrush.Color != null)
                    {
                        fillColor = solidColorBrush.Color.ToAndroid();
                    }

                    _drawable.Paint.Color = fillColor;
                }

                _drawable.Draw(canvas);
                _drawable.Paint.SetShader(null);
            }

            if (_stroke != null)
            {
                _drawable.Paint.SetStyle(Paint.Style.Stroke);

                if (_stroke is GradientBrush strokeGradientBrush)
                {
                    UpdatePathStrokeBounds();

                    if (strokeGradientBrush is LinearGradientBrush linearGradientBrush)
                    {
                        _strokeShader = CreateLinearGradient(linearGradientBrush, _pathStrokeBounds);
                    }

                    if (strokeGradientBrush is RadialGradientBrush radialGradientBrush)
                    {
                        _strokeShader = CreateRadialGradient(radialGradientBrush, _pathStrokeBounds);
                    }

                    _drawable.Paint.SetShader(_strokeShader);
                }
                else
                {
                    AColor strokeColor = Graphics.Colors.Transparent.ToAndroid();

                    if (_stroke is SolidColorBrush solidColorBrush && solidColorBrush.Color != null)
                    {
                        strokeColor = solidColorBrush.Color.ToAndroid();
                    }

                    _drawable.Paint.Color = strokeColor;
                }

                _drawable.Draw(canvas);
                _drawable.Paint.SetShader(null);
            }

            AMatrix inverseTransformMatrix = new AMatrix();

            transformMatrix.Invert(inverseTransformMatrix);
            _path.Transform(inverseTransformMatrix);
            inverseTransformMatrix.MapRect(_pathFillBounds);
            inverseTransformMatrix.MapRect(_pathStrokeBounds);
        }
示例#3
0
        public override void Draw(Canvas canvas)
        {
            if (_path == null)
            {
                return;
            }

            AMatrix transformMatrix = CreateMatrix();

            _path.Transform(transformMatrix);
            transformMatrix.MapRect(_pathFillBounds);
            transformMatrix.MapRect(_pathStrokeBounds);

            if (_fill != null)
            {
                this.Paint.SetStyle(_strokeWidth > 0 ? Paint.Style.Fill : Paint.Style.FillAndStroke);

                if (_fill is GradientBrush fillGradientBrush)
                {
                    if (fillGradientBrush is LinearGradientBrush linearGradientBrush)
                    {
                        _fillShader = CreateLinearGradient(linearGradientBrush, _pathFillBounds);
                    }

                    if (fillGradientBrush is RadialGradientBrush radialGradientBrush)
                    {
                        _fillShader = CreateRadialGradient(radialGradientBrush, _pathFillBounds);
                    }

                    this.Paint.SetShader(_fillShader);
                }
                else
                {
                    AColor fillColor = Color.Default.ToAndroid();

                    if (_fill is SolidColorBrush solidColorBrush && solidColorBrush.Color != Color.Default)
                    {
                        fillColor = solidColorBrush.Color.ToAndroid();
                    }

                    this.Paint.Color = fillColor;
                }

                base.Draw(canvas);
                this.Paint.SetShader(null);
            }

            if (_stroke != null && _strokeWidth > 0)
            {
                this.Paint.SetStyle(Paint.Style.Stroke);

                if (_stroke is GradientBrush strokeGradientBrush)
                {
                    if (strokeGradientBrush is LinearGradientBrush linearGradientBrush)
                    {
                        _strokeShader = CreateLinearGradient(linearGradientBrush, _pathStrokeBounds);
                    }

                    if (strokeGradientBrush is RadialGradientBrush radialGradientBrush)
                    {
                        _strokeShader = CreateRadialGradient(radialGradientBrush, _pathStrokeBounds);
                    }

                    this.Paint.SetShader(_strokeShader);
                }
                else
                {
                    AColor strokeColor = Color.Default.ToAndroid();

                    if (_stroke is SolidColorBrush solidColorBrush && solidColorBrush.Color != Color.Default)
                    {
                        strokeColor = solidColorBrush.Color.ToAndroid();
                    }

                    this.Paint.Color = strokeColor;
                }

                base.Draw(canvas);
                this.Paint.SetShader(null);
            }

            AMatrix inverseTransformMatrix = new AMatrix();

            transformMatrix.Invert(inverseTransformMatrix);
            _path.Transform(inverseTransformMatrix);
            inverseTransformMatrix.MapRect(_pathFillBounds);
            inverseTransformMatrix.MapRect(_pathStrokeBounds);
        }
示例#4
0
        protected override void OnDraw(droidGraphics.Canvas canvas)
        {
            base.OnDraw(canvas);

            // Prevent clipping of negative coordinates Step 2
            // TODO: Is there a better place / way to do this?
            if (Parent != null)
            {
                if (Parent.Parent as global::Android.Views.ViewGroup != null)
                {
                    (Parent.Parent as global::Android.Views.ViewGroup).SetClipChildren(false);
                }
            }

            if (Path == null)
            {
                return;
            }

            droidGraphics.Matrix stretchMatrix = ComputeStretchMatrix();

            Path.Transform(stretchMatrix);
            stretchMatrix.MapRect(pathFillBounds);
            stretchMatrix.MapRect(pathStrokeBounds);

            // Special processing for Rectangle because RadiusX, RadiusY can't be subject to Stretch transform
            if (this is RectangleDrawableView)
            {
                float radiusX = (this as RectangleDrawableView).RadiusX;
                float radiusY = (this as RectangleDrawableView).RadiusY;

                Path.Reset();
                Path.AddRoundRect(pathFillBounds, radiusX, radiusY, droidGraphics.Path.Direction.Cw);       // TODO: is the direction right?
            }

            if (fill != null)
            {
                drawable.Paint.SetStyle(droidGraphics.Paint.Style.Fill);

                if (fill is Color)
                {
                    Color fillColor = (Color)fill;

                    drawable.Paint.SetARGB((int)(255 * fillColor.A),
                                           (int)(255 * fillColor.R),
                                           (int)(255 * fillColor.G),
                                           (int)(255 * fillColor.B));
                }
                else if (fillShader == null)
                {
                    if (fill is LinearGradientBrush)
                    {
                        fillShader = CreateLinearGradient(fill as LinearGradientBrush, pathFillBounds, stretchMatrix);
                    }
                    else if (fill is ImageBrush)
                    {
                        fillShader = CreateBitmapShader(fill as ImageBrush);
                    }

                    drawable.Paint.SetShader(fillShader);
                }

                drawable.Draw(canvas);
                drawable.Paint.SetShader(null);
            }

            if (stroke != null)
            {
                drawable.Paint.SetStyle(droidGraphics.Paint.Style.Stroke);

                if (stroke is Color)
                {
                    Color strokeColor = (Color)stroke;

                    drawable.Paint.SetARGB((int)(255 * strokeColor.A),
                                           (int)(255 * strokeColor.R),
                                           (int)(255 * strokeColor.G),
                                           (int)(255 * strokeColor.B));
                }
                else if (strokeShader == null)
                {
                    if (stroke is LinearGradientBrush)
                    {
                        strokeShader = CreateLinearGradient(stroke as LinearGradientBrush, pathStrokeBounds, stretchMatrix);
                    }
                    else if (stroke is ImageBrush)
                    {
                        strokeShader = CreateBitmapShader(stroke as ImageBrush);
                    }

                    drawable.Paint.SetShader(strokeShader);
                }

                drawable.Draw(canvas);
                drawable.Paint.SetShader(null);
            }

            // Return everything back to its pre-stretched state
            droidGraphics.Matrix inverseStretchMatrix = new droidGraphics.Matrix();
            stretchMatrix.Invert(inverseStretchMatrix);

            Path.Transform(inverseStretchMatrix);
            inverseStretchMatrix.MapRect(pathFillBounds);
            inverseStretchMatrix.MapRect(pathStrokeBounds);
        }