Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

Big canvas bounds around custom control

$
0
0

Hi, I am new in Xamarin Forms. I create new custom control (round box) and add it to grid. But when custom control was drawing (Draw method) canvas had a very big bounds. How can i cahnge it bounds?
My control

public class RoundedBoxView : BoxView
{
public static readonly BindableProperty StrokeWidthProperty = BindableProperty.Create<RoundedBoxView, double>(p => p.StrokeWidth, default(double));

    public static readonly BindableProperty StrokeColorProperty = BindableProperty.Create<RoundedBoxView, Color>(p => p.StrokeColor, default(Color));

    public double StrokeWidth
    {
        get { return (double)GetValue(StrokeWidthProperty); }
        set { SetValue(StrokeWidthProperty, value); }
    }

    public Color StrokeColor
    {
        get { return (Color)GetValue(StrokeColorProperty); }
        set { SetValue(StrokeColorProperty, value); }
    }
}

My renderer

public class RoundedBoxViewRenderer : BoxRenderer
{
protected override void OnElementChanged(Xamarin.Forms.Platform.Android.ElementChangedEventArgs e)
{
base.OnElementChanged(e);

        if (e.OldElement == null)
        {
            if ((int)Android.OS.Build.VERSION.SdkInt < 18)
            {
                this.SetLayerType(Android.Views.LayerType.Software, null);
            }
        }
    }

    public override void Draw(Android.Graphics.Canvas canvas)
    {
    var roundedBox = this.Element as RoundedBoxView;
        if (roundedBox != null)
        {
            roundedBox.HorizontalOptions = LayoutOptions.Start;
            roundedBox.VerticalOptions = LayoutOptions.Start;
            using (canvas)
            {
                var circleCenterX = (float)((roundedBox.Width / 2) + roundedBox.StrokeWidth);
                var circleCenterY = (float)((roundedBox.Height / 2) + roundedBox.StrokeWidth);

                var mainCircleRadius = (float)Math.Min(roundedBox.Width, roundedBox.Height) / 2f;
                using (var backgroundPaint = new Android.Graphics.Paint())
                {
                    backgroundPaint.Color = new Android.Graphics.Color((byte)(roundedBox.BackgroundColor.R * 255), (byte)(roundedBox.BackgroundColor.G * 255), (byte)(roundedBox.BackgroundColor.B * 255));

                    canvas.DrawCircle(circleCenterX, circleCenterY, mainCircleRadius, backgroundPaint);
                }

                using (var strokePaint = new Android.Graphics.Paint())
                {
                    strokePaint.Color = new Android.Graphics.Color((byte)(roundedBox.StrokeColor.R * 255), (byte)(roundedBox.StrokeColor.G * 255), (byte)(roundedBox.StrokeColor.B * 255));
                    strokePaint.StrokeWidth = (float)roundedBox.StrokeWidth;
                    strokePaint.AntiAlias = true;

                    strokePaint.SetStyle(Android.Graphics.Paint.Style.Stroke);

                    canvas.DrawCircle(circleCenterX, circleCenterY, mainCircleRadius, strokePaint);
                }

                base.Draw(canvas);
            }
        }
    }

Viewing all articles
Browse latest Browse all 58056

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>