Skip to content

Instantly share code, notes, and snippets.

@QiMata
Created February 1, 2016 21:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save QiMata/cb96860873b4eafb6d5b to your computer and use it in GitHub Desktop.
Save QiMata/cb96860873b4eafb6d5b to your computer and use it in GitHub Desktop.
Only works for Android 4.2 or higher. The android renderer for the ContentViewRoundedCorners control.
class ContentViewRoundedCornersRenderer : VisualElementRenderer<ContentView>
{
private float _cornerRadius;
private RectF _bounds;
private Path _path;
protected override void OnElementChanged(ElementChangedEventArgs<ContentView> e)
{
base.OnElementChanged(e);
if (e.OldElement != null)
{
return;
}
var element = (ContentViewRoundedCorners) Element;
_cornerRadius = TypedValue.ApplyDimension(ComplexUnitType.Dip, element.CornerRadius,
Context.Resources.DisplayMetrics);
}
protected override void OnSizeChanged(int w, int h, int oldw, int oldh)
{
base.OnSizeChanged(w, h, oldw, oldh);
if (w != oldw && h != oldh)
{
_bounds = new RectF(0, 0, w, h);
}
_path = new Path();
_path.Reset();
_path.AddRoundRect(_bounds, _cornerRadius, _cornerRadius, Path.Direction.Cw);
_path.Close();
}
public override void Draw(Canvas canvas)
{
canvas.Save();
canvas.ClipPath(_path);
base.Draw(canvas);
canvas.Restore();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment