Skip to content

Instantly share code, notes, and snippets.

@BrunoVT1992
Created May 13, 2016 13:03
Show Gist options
  • Save BrunoVT1992/c218b325142bb75a85368f0f927ea630 to your computer and use it in GitHub Desktop.
Save BrunoVT1992/c218b325142bb75a85368f0f927ea630 to your computer and use it in GitHub Desktop.
Rounded corners for a framelayout in Xamarin Android
using Android.Content;
using Android.Graphics;
using Android.Util;
using Android.Widget;
namespace Droid
{
public class RoundedCornersFrameLayout : FrameLayout
{
private const float CornerRadius = 5.0f;
private Path _path = new Path();
private RectF _rect = new RectF();
public bool RoundedCorners { get; set; } = false;
public RoundedCornersFrameLayout(Context ctx, IAttributeSet attr)
: base(ctx, attr)
{
}
protected override void OnSizeChanged(int w, int h, int oldw, int oldh)
{
base.OnSizeChanged(w, h, oldw, oldh);
if (RoundedCorners)
{
_path.Reset();
_rect.Set(0, 0, w, h);
_path.AddRoundRect(_rect, CornerRadius, CornerRadius, Path.Direction.Cw);
_path.Close();
}
}
protected override void DispatchDraw(Canvas canvas)
{
if (RoundedCorners)
{
var save = canvas.Save();
canvas.ClipPath(_path);
base.DispatchDraw(canvas);
canvas.RestoreToCount(save);
}
else
{
base.DispatchDraw(canvas);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment