Created
May 13, 2016 13:03
-
-
Save BrunoVT1992/c218b325142bb75a85368f0f927ea630 to your computer and use it in GitHub Desktop.
Rounded corners for a framelayout in Xamarin Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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