Skip to content

Instantly share code, notes, and snippets.

@juucustodio
Last active November 10, 2017 21:20
Show Gist options
  • Save juucustodio/ada3093ae2659468b3e7cab2adc856a9 to your computer and use it in GitHub Desktop.
Save juucustodio/ada3093ae2659468b3e7cab2adc856a9 to your computer and use it in GitHub Desktop.
Example of BadgeView with circle box using custom renderer - http://julianocustodio.com/badgeview
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using System.ComponentModel;
using Android.Graphics;
using BadgeView;
using BadgeView.Droid;
[assembly: ExportRenderer(typeof(CircleBox), typeof(CircleBoxRenderer))]
namespace BadgeView.Droid
{
public class CircleBoxRenderer : BoxRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<BoxView> e)
{
base.OnElementChanged(e);
SetWillNotDraw(false);
Invalidate();
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == CircleBox.CornerRadiusProperty.PropertyName)
{
Invalidate();
}
}
public override void Draw(Canvas canvas)
{
var box = Element as CircleBox;
var rect = new Rect();
var paint = new Paint()
{
Color = box.BackgroundColor.ToAndroid(),
AntiAlias = true,
};
GetDrawingRect(rect);
var radius = (float) (rect.Width() / box.Width * box.CornerRadius);
canvas.DrawRoundRect(new RectF(rect), radius, radius, paint);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment