public static void ReloadBackgroundImage(this IStyleHelper helper, IStyleSheet styleSheet, IBound bound, Control control)
 {
     IBackgroundImage image;
     if (helper.TryGet(out image) && !string.IsNullOrWhiteSpace(image.Path))
     {
         var imageCache = styleSheet.GetCache<ImageCache>();
         using (BitmapDrawable background = imageCache.GetImage(image.Path, bound.Width, bound.Height))
             control.SetBackground(background);
     }
 }
Example #2
0
        protected async Task ChangeBackgroundImageAsync()
        {
            if (!(Element is CalendarButton element) || element.BackgroundImage == null)
            {
                return;
            }

            var d     = new List <Drawable>();
            var image = await GetBitmap(element.BackgroundImage);

            d.Add(new BitmapDrawable(image));
            var layer = new LayerDrawable(d.ToArray());

            layer.SetLayerInset(d.Count - 1, 0, 0, 0, 0);
            Control?.SetBackground(layer);
        }
        private void UpdateImage()
        {
            NinePatchDrawable image = null;

            if (Element?.Path != null)
            {
                var resourceId = GetResourceId();
                if (resourceId > 0)
                {
                    var sourceImage = BitmapFactory.DecodeResource(Resources, resourceId);
                    var chunk       = sourceImage.GetNinePatchChunk();
                    image = new NinePatchDrawable(Resources, sourceImage, chunk, new Rect(), null);
                }
            }
            Control?.SetBackground(image);
        }
Example #4
0
        protected override void OnElementChanged(ElementChangedEventArgs <Entry> e)
        {
            base.OnElementChanged(e);
            if (e.NewElement != null && Control != null)
            {
                bgColor = new ColorDrawable(Android.Graphics.Color.Transparent);
                Control?.SetBackground(bgColor);
                Control?.SetBackgroundColor(Android.Graphics.Color.Transparent);
                if (!Control.Enabled)
                {
                    Control?.SetTextColor(Android.Graphics.Color.Gray);
                }
            }

            if (e.OldElement != null)
            {
                bgColor.Dispose();
            }
        }
Example #5
0
        protected override void OnElementChanged(ElementChangedEventArgs <DatePicker> e)
        {
            base.OnElementChanged(e);
            Control.SetPadding(8, 8, 8, 8);


            var view = (CustomDatePicker)Element;

            var color           = view.BackgroundColor.ToAndroid();
            GradientDrawable gd = new GradientDrawable();

            if (view.BackgroundColor.IsDefault)
            {
                gd.SetColor(Color.White);
                //gd.SetColor();
            }
            else
            {
                gd.SetColor(color);
            }

            Control.SetBackground(gd);
        }
        protected override void OnElementChanged(ElementChangedEventArgs <Entry> e)
        {
            base.OnElementChanged(e);

            if (Control != null)
            {
                var view    = (CustomEntry)Element;
                var outline = new GradientDrawable();
                outline.SetShape(ShapeType.Rectangle);
                outline.SetColor(view.BackgroundColor.ToAndroid());
                outline.SetStroke(view.BorderWidth, view.BorderColor.ToAndroid());
                e.NewElement.Focused += (sender, evt) =>
                {
                    view.BorderColor = Color.Black;
                };
                if (view.IsCurvedCornersEnabled)
                {
                    outline.SetCornerRadius(15f);
                }
                Control.SetPadding(20, Control.PaddingTop, 20, Control.PaddingTop);
                Control.SetBackground(outline);
            }
        }
        protected override void OnElementChanged(ElementChangedEventArgs <Editor> e)
        {
            base.OnElementChanged(e);
            if (e.NewElement == null)
            {
                return;
            }

            var customEntry = e.NewElement as ExtraEditor;

            if (customEntry == null)
            {
                return;
            }

            GradientDrawable gd = new GradientDrawable();

            gd.SetCornerRadius(customEntry.CornerRadius);
            gd.SetStroke(customEntry.BorderThickness, customEntry.BorderColor.ToAndroid());
            gd.SetColor(customEntry.MyBackgroundColor.ToAndroid());
            backColor = customEntry.MyBackgroundColor.ToAndroid();
            Control.SetBackground(gd);
        }
        protected override void OnElementChanged(ElementChangedEventArgs <Xamarin.Forms.Button> e)
        {
            base.OnElementChanged(e);
            if (e.NewElement != null)
            {
                //Subscribe to the events and stuff
            }
            else if (e.OldElement != null)
            {
                //Unsubscribe from events
            }


            if (Control != null)
            {
                GradientDrawable gradientDrawable = new GradientDrawable();
                gradientDrawable.SetShape(ShapeType.Rectangle);
                gradientDrawable.SetColor(Element.BackgroundColor.ToAndroid());
                gradientDrawable.SetStroke(4, Element.BorderColor.ToAndroid());
                gradientDrawable.SetCornerRadius(38.0f);
                Control.SetBackground(gradientDrawable);
            }
        }
        protected override void OnElementChanged(ElementChangedEventArgs <Label> e)
        {
            base.OnElementChanged(e);

            var view = (Abstractions.RoundedLabel)Element;

            if (view == null)
            {
                return;
            }

            gradientDrawable = new GradientDrawable();
            gradientDrawable.SetShape(ShapeType.Rectangle);
            gradientDrawable.SetColor(view.CurvedBackgroundColor.ToAndroid());

            gradientDrawable.SetStroke(4, view.CurvedBackgroundColor.ToAndroid());

            gradientDrawable.SetCornerRadius(
                DpToPixels(Context,
                           Convert.ToSingle(view.RoundedCornerRadius)));

            Control.SetBackground(gradientDrawable);
        }
Example #10
0
        protected override void OnElementChanged(ElementChangedEventArgs <Xamarin.Forms.Button> e)
        {
            base.OnElementChanged(e);

            if (e.OldElement != null || Element == null)
            {
                Control.Touch -= ButtonTouched;
            }

            if (Control != null)
            {
                try
                {
                    Control.Touch            += ButtonTouched;
                    Control.StateListAnimator = new Android.Animation.StateListAnimator();
                    Control.SetBackground(DrawGradient(e));
                }
                catch (Exception ex)
                {
                    // handle exception
                }
            }
        }
Example #11
0
        protected override void OnElementChanged(ElementChangedEventArgs <Xamarin.Forms.Button> e)
        {
            base.OnElementChanged(e);

            if (e.NewElement != null)
            {
            }

            if (e.OldElement != null)
            {
            }

            if (Control != null)
            {
                var GradientDrawable = new GradientDrawable();
                GradientDrawable.SetShape(ShapeType.Rectangle);
                GradientDrawable.SetColor(Element.BackgroundColor.ToAndroid());
                GradientDrawable.SetStroke(4, Element.BorderColor.ToAndroid());
                GradientDrawable.SetCornerRadius(38.0f);

                Control.SetBackground(GradientDrawable);
            }
        }
 protected override void OnElementChanged(ElementChangedEventArgs <Button> e)
 {
     base.OnElementChanged(e);
     if (e.NewElement != null)
     {
         var roundedButton    = (RoundedButton)e.NewElement;
         var gradientDrawable = new GradientDrawable();
         gradientDrawable.SetShape(ShapeType.Rectangle);
         gradientDrawable.SetColor(roundedButton.BackgroundColor.ToAndroid());
         gradientDrawable.SetStroke(Convert.ToInt32(roundedButton.BorderWidth),
                                    roundedButton.BorderColor.ToAndroid());
         gradientDrawable.SetCornerRadius(roundedButton.BorderRadius);
         Control.SetBackground(gradientDrawable);
         Control.SetTextColor(roundedButton.TextColor.ToAndroid());
         e.NewElement.Released += OnReleased;
         e.NewElement.Pressed  += OnPressed;
     }
     else if (e.OldElement != null)
     {
         e.OldElement.Pressed  -= OnPressed;
         e.OldElement.Released -= OnReleased;
     }
 }
Example #13
0
        protected override void OnElementChanged(ElementChangedEventArgs <Entry> e)
        {
            base.OnElementChanged(e);

            if (Control != null && Element != null)
            {
                var view = (CustomEntry)Element;

                SetIcon(view);
                var _gradientBackground = new GradientDrawable();
                _gradientBackground.SetShape(ShapeType.Rectangle);
                _gradientBackground.SetColor(view.BackgroundColor.ToAndroid());

                _gradientBackground.SetStroke(view.BorderWidth, view.BorderColor.ToAndroid());
                Control.SetBackground(_gradientBackground);

                Control.SetPadding(
                    (int)DPToPixels(this.Context, Convert.ToSingle(12)),
                    Control.PaddingTop,
                    (int)DPToPixels(this.Context, Convert.ToSingle(12)),
                    Control.PaddingBottom
                    );
            }
        }
Example #14
0
        protected override void OnElementChanged(ElementChangedEventArgs <Entry> e)
        {
            base.OnElementChanged(e);

            if (Control == null || e.NewElement == null)
            {
                return;
            }

            if (e.NewElement != null)
            {
                element = (CustomEntry)Element;

                if (element.IsCurvedCornersEnabled)
                {
                    var _gradientBackground = new GradientDrawable();
                    _gradientBackground.SetShape(ShapeType.Rectangle);
                    _gradientBackground.SetColor(element.BackgroundColor.ToAndroid());

                    _gradientBackground.SetStroke(element.BorderWidth, element.BorderColor.ToAndroid());

                    _gradientBackground.SetCornerRadius(
                        DpToPixels(this.Context,
                                   System.Convert.ToSingle(element.CornerRadius)));

                    Control.SetBackground(_gradientBackground);
                }

                Control.CompoundDrawablePadding = 25;
                Control.SetPadding(
                    (int)DpToPixels(this.Context, System.Convert.ToSingle(5)),
                    Control.PaddingTop,
                    (int)DpToPixels(this.Context, System.Convert.ToSingle(5)),
                    Control.PaddingBottom);
            }
        }
Example #15
0
        protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);
            var element = Element as CalendarButton;

            if (e.PropertyName == nameof(element.TextWithoutMeasure) || e.PropertyName == "Renderer")
            {
                Control.Text = element.TextWithoutMeasure;
            }

            if (e.PropertyName == nameof(Element.TextColor) || e.PropertyName == "Renderer")
            {
                Control.SetTextColor(Element.TextColor.ToAndroid());
            }

            if (e.PropertyName == nameof(Element.BorderWidth) || e.PropertyName == nameof(Element.BorderColor) || e.PropertyName == nameof(Element.BackgroundColor) || e.PropertyName == "Renderer")
            {
                if (element.BackgroundPattern == null)
                {
                    var drawable = new GradientDrawable();
                    drawable.SetShape(ShapeType.Rectangle);
                    drawable.SetStroke((int)Element.BorderWidth, Element.BorderColor.ToAndroid());
                    drawable.SetColor(Element.BackgroundColor.ToAndroid());
                    Control.SetBackground(drawable);
                }
                else
                {
                    ChangeBackgroundPattern();
                }
            }

            if (e.PropertyName == nameof(element.BackgroundPattern))
            {
                ChangeBackgroundPattern();
            }
        }
Example #16
0
        private void Paint(CustomButton view, string startColor, string EndColor)
        {
            int[] colors  = { Android.Graphics.Color.ParseColor(startColor), Android.Graphics.Color.ParseColor(EndColor) };
            int[] colors2 = { Android.Graphics.Color.ParseColor(EndColor), Android.Graphics.Color.ParseColor(startColor) };

            _gradientBackground = new GradientDrawable(GradientDrawable.Orientation.LeftRight, colors);
            colorStateList      = new ColorStateList(new int[][] {
                new int[] {},
                new int[] {}
            }, colors2);
            //_gradientBackground.SetShape(ShapeType.Rectangle);
            //_gradientBackground.SetColors(colors);
            //_gradientBackground.SetColor(view.CustomBackgroundColor.ToAndroid());
            // Thickness of the stroke line
            _gradientBackground.SetStroke((int)view.CustomBorderWidth, view.CustomBorderColor.ToAndroid());
            // Radius for the curves
            _gradientBackground.SetCornerRadius(
                DpToPixels(this.Context, Convert.ToSingle(view.CustomBorderRadius)));
            // set the background of the label
            Control.SetBackground(_gradientBackground);
            Control.SetTextColor(colorStateList);

            Control.SetPadding(2, 2, 2, 2);
        }
        protected async void ChangeBackgroundImage()
        {
            var element = Element as CalendarButton;

            if (element == null || element.BackgroundImage == null)
            {
                return;
            }

            var d     = new List <Drawable>();
            var image = await GetBitmap(element.BackgroundImage);

            d.Add(new BitmapDrawable(image));
            var drawable = new GradientDrawable();

            drawable.SetShape(ShapeType.Rectangle);
            drawable.SetStroke((int)Element.BorderWidth, Element.BorderColor.ToAndroid());
            drawable.SetColor(Android.Graphics.Color.Transparent);
            d.Add(drawable);
            var layer = new LayerDrawable(d.ToArray());

            layer.SetLayerInset(d.Count - 1, 0, 0, 0, 0);
            Control.SetBackground(layer);
        }
Example #18
0
        protected void ChangeBackgroundPattern()
        {
            if (!(Element is CalendarButton element) ||
                element.BackgroundPattern == null ||
                Control.Width == 0)
            {
                return;
            }

            var d = new List <Drawable>();

            for (var i = 0; i < element.BackgroundPattern.Pattern.Count; i++)
            {
                d.Add(new ColorDrawable(element.BackgroundPattern.Pattern[i].Color.ToAndroid()));
            }

            var drawable = new GradientDrawable();

            drawable.SetShape(ShapeType.Rectangle);
            drawable.SetStroke((int)Element.BorderWidth, Element.BorderColor.ToAndroid());
            drawable.SetColor(Android.Graphics.Color.Transparent);
            d.Add(drawable);

            var layer = new LayerDrawable(d.ToArray());

            for (var i = 0; i < element.BackgroundPattern.Pattern.Count; i++)
            {
                var l = (int)(Control.Width * element.BackgroundPattern.GetLeft(i));
                var t = (int)(Control.Height * element.BackgroundPattern.GetTop(i));
                var r = (int)(Control.Width * (1 - element.BackgroundPattern.Pattern[i].WidthPercent)) - l;
                var b = (int)(Control.Height * (1 - element.BackgroundPattern.Pattern[i].HightPercent)) - t;
                layer.SetLayerInset(i, l, t, r, b);
            }
            layer.SetLayerInset(d.Count - 1, 0, 0, 0, 0);
            Control.SetBackground(layer);
        }
Example #19
0
        protected override void OnElementChanged(ElementChangedEventArgs <Button> e)
        {
            base.OnElementChanged(e);

            if (Control == null)
            {
                return;
            }

            XButton element = (XButton)e.NewElement;

            // Converting values to PX.
            int borderRadiusInDp = ConvertToPx(element.BorderRadius);
            int borderWidthInDp  = ConvertToPx(Convert.ToInt32(element.BorderWidth));

            // Appliying styles.
            var gradientDrawable = new GradientDrawable();

            gradientDrawable.SetCornerRadius(borderRadiusInDp);
            gradientDrawable.SetColor(element.BackgroundColor.ToAndroid());
            gradientDrawable.SetStroke(borderWidthInDp, element.BorderColor.ToAndroid());

            Control.SetBackground(gradientDrawable);
        }
Example #20
0
        protected override void OnElementChanged(ElementChangedEventArgs <Entry> e)
        {
            base.OnElementChanged(e);

            if (Control != null)
            {
                //Control.SetBackgroundColor(global::Android.Graphics.Color.LightGreen);
                var shape = new ShapeDrawable(new Android.Graphics.Drawables.Shapes.RectShape());

                shape.Paint.Color = global::Android.Graphics.Color.ParseColor("#999797");
                shape.Paint.SetStyle(Paint.Style.Stroke);
                Control.SetBackground(shape);
                Control.SetPaddingRelative(30, 15, 30, 15);
                GradientDrawable gd = new GradientDrawable();
                //gd.SetColor(null);
                gd.SetCornerRadius(50);
                gd.SetStroke(2, Android.Graphics.Color.ParseColor("#4A4A4A"));
                Control.SetBackground(gd);
                //Control.TextAlignment = Android.Views.TextAlignment.Center;
                //Control.TextSize = 20f;
                // Fixed height creates padding at top and bottom
                //Element.HeightRequest = 45;
            }
        }
        protected override void OnElementChanged(ElementChangedEventArgs <Xamarin.Forms.DatePicker> e)
        {
            base.OnElementChanged(e);

            if (Control != null)
            {
                var ctrl = e.NewElement as IEbCustomControl;

                drawable.SetShape(ShapeType.Rectangle);
                drawable.SetCornerRadius(ctrl.BorderRadius);

                if (ctrl.BorderColor != null)
                {
                    drawable.SetStroke(ctrl.BorderThickness, ctrl.BorderColor.ToAndroid());
                }

                if (ctrl.XBackgroundColor != null)
                {
                    drawable.SetColor(ctrl.XBackgroundColor.ToAndroid());
                }

                Control.SetBackground(drawable);
            }
        }
Example #22
0
        private void CreateBorder(EntryRepeatingWord view)
        {
            if (view.IsCurvedCornersEnabled)
            {
                // creating gradient drawable for the curved background
                var _gradientBackground = new GradientDrawable();
                _gradientBackground.SetShape(ShapeType.Rectangle);
                _gradientBackground.SetColor(view.BackgroundColor.ToAndroid());

                // Thickness of the stroke line
                _gradientBackground.SetStroke(view.BorderWidth, view.BorderColor.ToAndroid());

                // Radius for the curves
                _gradientBackground.SetCornerRadius(
                    DpToPixels(this.Context, Convert.ToSingle(view.CornerRadius)));

                // set the background of the
                Control.SetBackground(_gradientBackground);
            }
            // Set padding for the internal text from border
            Control.SetPadding(
                (int)DpToPixels(this.Context, Convert.ToSingle(12)), Control.PaddingTop,
                (int)DpToPixels(this.Context, Convert.ToSingle(12)), Control.PaddingBottom);
        }
        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);
            var view = (CustomLabel)Element;

            if (view == null)
            {
                return;
            }

            // creating gradient drawable for the curved background

            _gradientBackground.SetShape(ShapeType.Rectangle);
            _gradientBackground.SetColor(view.CurvedBackgroundColor.ToAndroid());

            // Thickness of the stroke line
            _gradientBackground.SetStroke(4, Color.White.ToAndroid());

            // Radius for the curves
            _gradientBackground.SetCornerRadius(DpToPixels(Forms.Context, (float)view.CornerRadius));

            // set the background of the label
            Control.SetBackground(_gradientBackground);
        }
Example #24
0
        protected override void OnElementChanged(ElementChangedEventArgs <Label> e)
        {
            base.OnElementChanged(e);
            var view = (CustomLabel)Element;

            if (view == null)
            {
                return;
            }
            // creating gradient drawable for the curved background
            _gradientBackground = new GradientDrawable();
            _gradientBackground.SetShape(ShapeType.Rectangle);
            _gradientBackground.SetColor(view.CurvedBackgroundColor.ToAndroid());

            // Thickness of the stroke line
            _gradientBackground.SetStroke(4, view.CurvedBackgroundColor.ToAndroid());

            // Radius for the curves
            _gradientBackground.SetCornerRadius(DpToPixels(this.Context,
                                                           Convert.ToSingle(view.CurvedCornerRadius)));

            // set the background of the label
            Control.SetBackground(_gradientBackground);
        }
Example #25
0
        protected override void OnElementChanged(ElementChangedEventArgs <Button> e)
        {
            base.OnElementChanged(e);
            var element = e.NewElement;

            if (Control != null && element is FAB button)
            {
                GradientDrawable shape = new GradientDrawable();
                shape.SetColor(button.TintColor.ToAndroid());
                shape.SetShape(ShapeType.Oval);

                int[][] colorlist = new int[1][];

                colorlist[0] = new int[] { Android.Resource.Attribute.StatePressed };

                var statelist = new ColorStateList(colorlist, new int[] { button.RippleColor.ToAndroid() });

                RippleDrawable ripple = new RippleDrawable(statelist, shape, null);

                Control.SetElevation(button.Elevation);
                Control.StateListAnimator = null;
                Control.SetBackground(ripple);
            }
        }
Example #26
0
 private void UpdateBackground()
 {
     Control.SetBackground(_gradientDrawable);
 }
Example #27
0
 protected override void OnElementChanged(ElementChangedEventArgs <Xamarin.Forms.DatePicker> e)
 {
     base.OnElementChanged(e);
     Control.SetBackground(Context.GetDrawable(Resource.Drawable.bottom_border));
     Control.SetTextColor(Android.Graphics.Color.ParseColor("#4E6B4C"));
 }
Example #28
0
 protected override void OnElementChanged(ElementChangedEventArgs <Editor> e)
 {
     base.OnElementChanged(e);
     Control.SetBackground(RainbowDrawable.Get());
 }
        /// <summary>
        /// Raises the <see cref="E:ElementChanged" /> event.
        /// </summary>
        /// <param name="e">The <see cref="ElementChangedEventArgs{Entry}"/> instance containing the event data.</param>
        protected override void OnElementChanged(ElementChangedEventArgs <Entry> e)
        {
            base.OnElementChanged(e);

            if (!((Control != null) & (e.NewElement != null)))
            {
                return;
            }

            if (!(e.NewElement is EnhancedEntry entryExt))
            {
                return;
            }
            {
                Control.ImeOptions = getValueFromDescription(entryExt.ReturnKeyType);

                Control.SetImeActionLabel(entryExt.ReturnKeyType.ToString(), Control.ImeOptions);

                _gradientDrawable = new GradientDrawable();
                _gradientDrawable.SetShape(ShapeType.Rectangle);
                _gradientDrawable.SetColor(entryExt.BackgroundColor.ToAndroid());
                _gradientDrawable.SetCornerRadius(entryExt.CornerRadius);
                _gradientDrawable.SetStroke((int)entryExt.BorderWidth, entryExt.BorderColor.ToAndroid());

                Rect padding = new Rect
                {
                    Left   = entryExt.LeftPadding,
                    Right  = entryExt.RightPadding,
                    Top    = entryExt.TopBottomPadding / 2,
                    Bottom = entryExt.TopBottomPadding / 2
                };
                _gradientDrawable.GetPadding(padding);

                e.NewElement.Focused += (sender, evt) =>
                {
                    _gradientDrawable.SetStroke(
                        (int)entryExt.BorderWidth,
                        entryExt.FocusBorderColor.ToAndroid());
                };

                e.NewElement.Unfocused += (sender, evt) =>
                {
                    _gradientDrawable.SetStroke((int)entryExt.BorderWidth, entryExt.BorderColor.ToAndroid());
                };

                if (entryExt.EntryHeight != -1)
                {
                    Control.SetHeight((int)DpToPixels(_context, entryExt.EntryHeight));
                }

                Control.SetBackground(_gradientDrawable);

                if (Control != null && !string.IsNullOrEmpty(PackageName) && !string.IsNullOrEmpty(entryExt.LeftIcon))
                {
                    int identifier = Context.Resources.GetIdentifier(
                        entryExt.LeftIcon,
                        "drawable",
                        PackageName);
                    if (identifier != 0)
                    {
                        Drawable drawable = ContextCompat.GetDrawable(_context, identifier);
                        if (drawable != null)
                        {
                            Control.SetCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
                            Control.CompoundDrawablePadding = entryExt.PaddingLeftIcon;

                            entryExt.IconDrawableColorChanged += (sender, args) =>
                            {
                                foreach (Drawable d in Control.GetCompoundDrawables())
                                {
                                    if (args.IsValid)
                                    {
                                        d?.SetColorFilter(new PorterDuffColorFilter(args.Color.ToAndroid(), PorterDuff.Mode.SrcIn));
                                    }
                                    else
                                    {
                                        d?.ClearColorFilter();
                                    }
                                }
                            };
                        }
                    }
                }

                Control.EditorAction += (sender, args) =>
                {
                    if (entryExt.NextEntry == null)
                    {
                        if (_context.GetSystemService(Context.InputMethodService) is InputMethodManager inputMethodManager && _context is Activity)
                        {
                            Activity activity = (Activity)_context;
                            IBinder  token    = activity.CurrentFocus?.WindowToken;
                            inputMethodManager.HideSoftInputFromWindow(token, HideSoftInputFlags.None);

                            activity.Window.DecorView.ClearFocus();
                        }
                    }

                    entryExt.EntryActionFired();
                };
            }
        }
        public void UpdateDrawable()
        {
            if (BorderElement == null || Control == null)
            {
                return;
            }

            bool cornerRadiusIsDefault    = !BorderElement.IsSet(BorderElement.CornerRadiusProperty) || (BorderElement.CornerRadius == (int)BorderElement.CornerRadiusProperty.DefaultValue || BorderElement.CornerRadius == BorderDrawable.DefaultCornerRadius);
            bool backgroundColorIsDefault = !BorderElement.IsSet(VisualElement.BackgroundColorProperty) || BorderElement.BackgroundColor == (Color)VisualElement.BackgroundColorProperty.DefaultValue;
            bool borderColorIsDefault     = !BorderElement.IsSet(BorderElement.BorderColorProperty) || BorderElement.BorderColor == (Color)BorderElement.BorderColorProperty.DefaultValue;
            bool borderWidthIsDefault     = !BorderElement.IsSet(BorderElement.BorderWidthProperty) || BorderElement.BorderWidth == (double)BorderElement.BorderWidthProperty.DefaultValue;

            if (backgroundColorIsDefault &&
                cornerRadiusIsDefault &&
                borderColorIsDefault &&
                borderWidthIsDefault)
            {
                if (!_drawableEnabled)
                {
                    return;
                }

                if (_defaultDrawable != null)
                {
                    Control.SetBackground(_defaultDrawable);
                }

                _drawableEnabled = false;
                Reset();
            }
            else
            {
                if (_backgroundDrawable == null)
                {
                    _backgroundDrawable = new BorderDrawable(Control.Context.ToPixels, Forms.GetColorButtonNormal(Control.Context), _drawOutlineWithBackground);
                }

                _backgroundDrawable.BorderController = BorderElement;

                var useDefaultPadding = _renderer.UseDefaultPadding();

                int paddingTop  = useDefaultPadding ? Control.PaddingTop : 0;
                int paddingLeft = useDefaultPadding ? Control.PaddingLeft : 0;

                var useDefaultShadow = _renderer.UseDefaultShadow();

                // Use no shadow by default for API < 16
                float  shadowRadius = 0;
                float  shadowDy     = 0;
                float  shadowDx     = 0;
                AColor shadowColor  = Color.Transparent.ToAndroid();
                // Add Android's default material shadow if we want it
                if (useDefaultShadow)
                {
                    shadowRadius = 2;
                    shadowDy     = 4;
                    shadowDx     = 0;
                    shadowColor  = _backgroundDrawable.PressedBackgroundColor.ToAndroid();
                }
                // Otherwise get values from the control (but only for supported APIs)
                else if ((int)Build.VERSION.SdkInt >= 16)
                {
                    shadowRadius = _renderer.ShadowRadius;
                    shadowDy     = _renderer.ShadowDy;
                    shadowDx     = _renderer.ShadowDx;
                    shadowColor  = _renderer.ShadowColor;
                }

                _backgroundDrawable.SetPadding(paddingTop, paddingLeft);
                if (_renderer.IsShadowEnabled())
                {
                    _backgroundDrawable
                    .SetShadow(shadowDy, shadowDx, shadowColor, shadowRadius);
                }

                if (_drawableEnabled)
                {
                    return;
                }

                if (_defaultDrawable == null)
                {
                    _defaultDrawable = Control.Background;
                }

                if (!backgroundColorIsDefault || _drawOutlineWithBackground)
                {
                    if (Forms.IsLollipopOrNewer)
                    {
                        var rippleColor = _backgroundDrawable.PressedBackgroundColor.ToAndroid();

                        _rippleDrawable = new RippleDrawable(ColorStateList.ValueOf(rippleColor), _backgroundDrawable, null);
                        Control.SetBackground(_rippleDrawable);
                    }
                    else
                    {
                        Control.SetBackground(_backgroundDrawable);
                    }
                }

                _drawableEnabled = true;
            }

            Control.Invalidate();
        }
Example #31
0
        protected override void OnElementChanged(ElementChangedEventArgs <Button> e)
        {
            base.OnElementChanged(e);

            if ((Control != null) && (e.OldElement == null))
            {
                MyButtonRenderer myButtonRenderer = (MyButtonRenderer)Element;
                GradientDrawable gradientDrawable = new GradientDrawable();
                if (myButtonRenderer.IsGradient)
                {
                    Color StartColor = ColorInterpolator.InterpolateBetween(myButtonRenderer.EndColor,
                                                                            myButtonRenderer.StartColor,
                                                                            myButtonRenderer.NumOfGradientStart);
                    Color EndColor = ColorInterpolator.InterpolateBetween(myButtonRenderer.StartColor,
                                                                          myButtonRenderer.EndColor,
                                                                          myButtonRenderer.NumOfGradientEnd);
                    if (!myButtonRenderer.IsEnabled)
                    {
                        StartColor = System.Drawing.Color.FromArgb(70, StartColor);
                        EndColor   = System.Drawing.Color.FromArgb(70, EndColor);
                    }
                    gradientDrawable = new GradientDrawable(
                        GradientDrawable.Orientation.LeftRight,
                        new int[] { StartColor.ToAndroid(),
                                    EndColor.ToAndroid() }
                        );
                }
                else
                {
                    Color staticColor = myButtonRenderer.StaticColor;
                    if (!myButtonRenderer.IsEnabled)
                    {
                        staticColor = System.Drawing.Color.FromArgb(70, myButtonRenderer.StaticColor);
                    }
                    gradientDrawable.SetColor(staticColor.ToAndroid());
                }
                switch (myButtonRenderer.TypeBorder)
                {
                case TypeBorder.All:
                    gradientDrawable.SetCornerRadii(new float[] { 80, 80, 80, 80,
                                                                  80, 80, 80, 80 });
                    break;

                case TypeBorder.Left:
                    gradientDrawable.SetCornerRadii(new float[] { 80, 80, 0, 0,
                                                                  0, 0, 80, 80 });
                    break;

                case TypeBorder.Right:
                    gradientDrawable.SetCornerRadii(new float[] { 0, 0, 80, 80,
                                                                  80, 80, 0, 0 });
                    break;

                case TypeBorder.Down:
                    gradientDrawable.SetCornerRadii(new float[] { 0, 0, 0, 0,
                                                                  80, 80, 80, 80 });
                    break;

                case TypeBorder.Up:
                    gradientDrawable.SetCornerRadii(new float[] { 80, 80, 80, 80,
                                                                  0, 0, 0, 0 });
                    break;
                }
                Control.StateListAnimator = new StateListAnimator();
                Control.SetBackground(gradientDrawable);
            }
        }