Skip to content

Instantly share code, notes, and snippets.

@HeikkiDev
Created March 25, 2021 12:26
Show Gist options
  • Select an option

  • Save HeikkiDev/9df8a732c3834fecd1da9ce3182cf26d to your computer and use it in GitHub Desktop.

Select an option

Save HeikkiDev/9df8a732c3834fecd1da9ce3182cf26d to your computer and use it in GitHub Desktop.
Rounded corners WebView Renderer for Android using Canvas.ClipPath
internal class AdvancedWebViewRenderer : WebViewRenderer
{
private readonly float _density;
public AdvancedWebViewRenderer(Context context) : base(context)
{
var displayMetrics = new DisplayMetrics();
CrossCurrentActivity.Current.Activity.WindowManager.DefaultDisplay.GetMetrics(displayMetrics);
_density = displayMetrics.Density;
}
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
SetLayerType(Android.Views.LayerType.Hardware, null);
}
}
protected override void DispatchDraw(Canvas? canvas)
{
float radius = 150;
var path = new Path();
path.SetFillType(Path.FillType.InverseWinding);
path.AddRoundRect(new RectF(0, 0, (float) Element.Width * _density, (float) Element.Height * _density), 150, 150, Path.Direction.Cw);
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
canvas.ClipOutPath(path);
}
else
{
canvas.ClipPath(path, Region.Op.Difference);
}
base.DispatchDraw(canvas);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment