Skip to content

Instantly share code, notes, and snippets.

@ads90
Created February 13, 2018 02:05
Show Gist options
  • Save ads90/49cdea7d609a5b364eb34c9feedb7271 to your computer and use it in GitHub Desktop.
Save ads90/49cdea7d609a5b364eb34c9feedb7271 to your computer and use it in GitHub Desktop.
Xamarin BoxRenderer
[assembly: ExportRenderer(typeof(Sample.CustomBoxView), typeof(SABoxViewRenderer))]
namespace Sample.Droid.UIRenderers
{
public class SABoxViewRenderer : BoxRenderer
{
public SABoxViewRenderer(Android.Content.Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<BoxView> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
SetWillNotDraw(false);
e.NewElement.SizeChanged += (sender, er) =>
{
var box = e.NewElement as CustomBoxView;
if (box.IsCircular)
{
bool takeWidth = box.Height <= box.Width;
box.HeightRequest = takeWidth ? box.Width : box.Height;
box.WidthRequest = takeWidth ? box.Width : box.Height;
}
Invalidate();
};
Invalidate();
}
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (Element == null || e == null) { return; }
if (e.PropertyName == CustomBoxView.CornerRadiusProperty.PropertyName)
{
Invalidate();
}
}
public override void Draw(Canvas canvas)
{
var box = Element as CustomBoxView;
var cRadiou = (Math.Abs(box.CornerRadius - -9) < 1) ? box.Width / 2 : box.CornerRadius;
if (box.IsCircular)
{
bool takeWidth = box.Height <= box.Width;
box.HeightRequest = takeWidth ? box.Width : box.Height;
box.WidthRequest = takeWidth ? box.Width : box.Height;
cRadiou = box.WidthRequest / 2;
Invalidate();
}
var rect = new Rect();
var paint = new Paint()
{
Color = box.BackgroundColor.ToAndroid(),
AntiAlias = true,
};
GetDrawingRect(rect);
var radius = (float)(rect.Width() / box.Width * cRadiou);
//if (box.ClassId?.ToString() == "right") { }
//var radiusX = radius;
//var radiusY = radius;
canvas.DrawRoundRect(new RectF(rect), radius, radius, paint);
//x,y
}
}
[assembly: ExportRenderer(typeof(Sample.CustomBoxView), typeof(SABoxViewRenderer))]
namespace Sample.iOS.UIRenderers
{
public class SABoxViewRenderer : BoxRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<BoxView> e)
{
base.OnElementChanged(e);
if (Element != null)
{
Layer.MasksToBounds = true;
e.NewElement.SizeChanged += (sender, er) => { UpdateCornerRadius(Element as CustomBoxView); };
UpdateCornerRadius(Element as CustomBoxView);
}
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == CustomBoxView.CornerRadiusProperty.PropertyName)
{
UpdateCornerRadius(Element as CustomBoxView);
}
}
void UpdateCornerRadius(CustomBoxView box)
{
if (box.IsCircular)
{
Layer.CornerRadius = (nfloat)box.Width / 2;
}
else
{
Layer.CornerRadius = (nfloat)box.CornerRadius;
}
}
}
}
namespace Sample
{
public class CustomBoxView : BoxView
{
public static readonly BindableProperty CornerRadiusProperty =
BindableProperty.Create("CornerRadius", typeof(double), typeof(SABoxView), 0.0);
/// <summary>
/// Gets or sets the corner radius.
/// </summary>
public double CornerRadius
{
get { return (double)GetValue(CornerRadiusProperty); }
set { SetValue(CornerRadiusProperty, value); }
}
public bool IsCircular { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment