Пример #1
0
        protected override void OnElementChanged(ElementChangedEventArgs <ContentView> e)
        {
            base.OnElementChanged(e);

            if (element != null)
            {
                return;
            }

            element = (RoundedContentView)e.NewElement;

            this.SetupCorners();
        }
        public override void Draw(CoreGraphics.CGRect rect)
        {
            base.Draw(rect);

            this.LayoutIfNeeded();

            RoundedContentView rcv = (RoundedContentView)Element;

            if (rcv == null)
            {
                return;
            }
            this.ClipsToBounds         = true;
            this.Layer.BackgroundColor = rcv.FillColor.ToCGColor();
            this.Layer.MasksToBounds   = true;
            this.Layer.CornerRadius    = (nfloat)rcv.CornerRadius;
            if (rcv.HasShadow)
            {
                this.Layer.ShadowRadius  = 3.0f;
                this.Layer.ShadowColor   = UIColor.Gray.CGColor;
                this.Layer.ShadowOffset  = new CGSize(1, 1);
                this.Layer.ShadowOpacity = 0.60f;
                //this.Layer.ShadowPath = UIBezierPath.FromRect(Layer.ContentsRect).CGPath;
                this.Layer.MasksToBounds = false;
            }
            if (rcv.Circle)
            {
                this.Layer.CornerRadius = (int)(Math.Min(Element.Width, Element.Height) / 2);
            }
            this.Layer.BorderWidth = 0;

            if (rcv.BorderWidth > 0 && rcv.BorderColor.A > 0.0)
            {
                this.Layer.BorderWidth = rcv.BorderWidth;
                this.Layer.BorderColor =
                    new UIKit.UIColor(
                        (nfloat)rcv.BorderColor.R,
                        (nfloat)rcv.BorderColor.G,
                        (nfloat)rcv.BorderColor.B,
                        (nfloat)rcv.BorderColor.A).CGColor;
            }
        }
Пример #3
0
 public RoundedDrawable(RoundedContentView element, Func <double, float> convertToPixels)
 {
     _element                  = element;
     _convertToPixels          = convertToPixels;
     _element.PropertyChanged += Drawable_PropertyChanged;
 }
        protected override bool DrawChild(Canvas canvas, global::Android.Views.View child, long drawingTime)
        {
            if (Element == null)
            {
                return(false);
            }

            RoundedContentView rcv = (RoundedContentView)Element;

            this.SetClipChildren(true);

            rcv.Padding = new Thickness(0, 0, 0, 0);
            int radius = (int)(rcv.CornerRadius);

            if (rcv.Circle)
            {
                radius = Math.Min(Width, Height) / 2;
            }
            radius *= 2;

            try
            {
                var path = new Path();
                path.AddRoundRect(new RectF(0, 0, Width, Height),
                                  new float[] { radius, radius, radius, radius, radius, radius, radius, radius },
                                  Path.Direction.Ccw);
                if (rcv.HasShadow)
                {
                    var shadowPath = new Path();
                    shadowPath.AddRoundRect(new RectF(5, 5, Width, Height),
                                            new float[] { radius, radius, radius, radius, radius, radius, radius, radius },
                                            Path.Direction.Ccw);
                    var paint = new Paint();
                    paint.AntiAlias   = true;
                    paint.StrokeWidth = 5;
                    paint.SetStyle(Paint.Style.Stroke);
                    paint.Color = Xamarin.Forms.Color.FromRgba(0, 0, 0, 0.3).ToAndroid();
                    canvas.DrawPath(shadowPath, paint);
                }
                canvas.Save();
                canvas.ClipPath(path);
                canvas.DrawColor(rcv.FillColor.ToAndroid());
                var result = base.DrawChild(canvas, child, drawingTime);

                canvas.Restore();
                if (rcv.BorderWidth > 0)
                {
                    var paint = new Paint();
                    paint.AntiAlias   = true;
                    paint.StrokeWidth = rcv.BorderWidth;
                    paint.SetStyle(Paint.Style.Stroke);
                    paint.Color = rcv.BorderColor.ToAndroid();
                    canvas.DrawPath(path, paint);
                }
                path.Dispose();
                return(result);
            }
            catch (Exception ex)
            {
                System.Console.Write(ex.Message);
            }

            return(base.DrawChild(canvas, child, drawingTime));
        }