Created
March 25, 2021 12:26
-
-
Save HeikkiDev/9df8a732c3834fecd1da9ce3182cf26d to your computer and use it in GitHub Desktop.
Rounded corners WebView Renderer for Android using Canvas.ClipPath
This file contains hidden or 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
| 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