Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.