using System; using Xamarin.Forms.Platform.Android; using Xamarin.Forms; using ShaneSiebken.Mobile.Cuisinier.Controls; using ShaneSiebken.Mobile.Cuisinier.Droid.Renderers; using Android.Graphics; using System.ComponentModel; [assembly: ExportRendererAttribute(typeof(ClippingFrame), typeof(ClippingFrameRenderer))] namespace ShaneSiebken.Mobile.Cuisinier.Droid.Renderers { public class ClippingFrameRenderer : FrameRenderer { public ClippingFrameRenderer () { this.SetWillNotDraw(false); } // // // // protected override void OnElementChanged(ElementChangedEventArgs e) { base.OnElementChanged(e); if (e.OldElement == null) { //Only enable hardware accelleration on lollipop if ((int)Android.OS.Build.VERSION.SdkInt < 21) { SetLayerType(Android.Views.LayerType.Software, null); } } } public override void Draw(Canvas canvas) { ClippingFrame cfv = this.Element as ClippingFrame; Rect rc = new Rect(); GetDrawingRect(rc); Rect interior = rc; interior.Inset((int)cfv.StrokeThickness, (int)cfv.StrokeThickness); //Path clipPath = new Path (); /*clipPath.AddRoundRect (new RectF(interior), (float)cfv.CornerRadius,(float)cfv.CornerRadius, Path.Direction.Cw); */ Paint p = new Paint() { AntiAlias = true, Color = cfv.BackgroundColor.ToAndroid() }; canvas.DrawRoundRect(new RectF(interior), (float)cfv.CornerRadius, (float)cfv.CornerRadius, p); p.Color = cfv.OutlineColor.ToAndroid(); p.StrokeWidth = (float)cfv.StrokeThickness; p.SetStyle(Paint.Style.Stroke); canvas.DrawRoundRect(new RectF(rc), (float)cfv.CornerRadius, (float)cfv.CornerRadius, p); System.Diagnostics.Debug.WriteLine (ViewGroup.ChildCount); for (int i =0; i < ViewGroup.ChildCount; i++){ DrawChild(canvas, ViewGroup.GetChildAt (i), DateTime.UtcNow.Millisecond); } //viewGroup.GetChildAt (0).Draw (canvas); } protected override bool DrawChild(Canvas canvas, global::Android.Views.View child, long drawingTime) { System.Diagnostics.Debug.WriteLine ("Drawing child"); try { ClippingFrame cfv = this.Element as ClippingFrame; Rect rc = new Rect(); GetDrawingRect(rc); Rect interior = rc; interior.Inset((int)cfv.StrokeThickness, (int)cfv.StrokeThickness); Path path = new Path (); path.AddRoundRect (new RectF(interior), (float)cfv.CornerRadius,(float)cfv.CornerRadius, Path.Direction.Cw); canvas.Save(); canvas.ClipPath(path); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Unable to create image: " + ex); } return base.DrawChild(canvas, child, drawingTime); } } }