public void UpdateDrawable() { if (_button == null || _nativeButton == null) { return; } bool cornerRadiusIsDefault = !_button.IsSet(Button.CornerRadiusProperty) || (_button.CornerRadius == (int)Button.CornerRadiusProperty.DefaultValue || _button.CornerRadius == ButtonDrawable.DefaultCornerRadius); bool backgroundColorIsDefault = !_button.IsSet(VisualElement.BackgroundColorProperty) || _button.BackgroundColor == (Color)VisualElement.BackgroundColorProperty.DefaultValue; bool borderColorIsDefault = !_button.IsSet(Button.BorderColorProperty) || _button.BorderColor == (Color)Button.BorderColorProperty.DefaultValue; bool borderWidthIsDefault = !_button.IsSet(Button.BorderWidthProperty) || _button.BorderWidth == (double)Button.BorderWidthProperty.DefaultValue; if (backgroundColorIsDefault && cornerRadiusIsDefault && borderColorIsDefault && borderWidthIsDefault) { if (!_drawableEnabled) { return; } if (_defaultDrawable != null) { _nativeButton.SetBackground(_defaultDrawable); } _drawableEnabled = false; } else { if (_backgroundDrawable == null) { _backgroundDrawable = new ButtonDrawable(_nativeButton.Context.ToPixels, Forms.GetColorButtonNormal(_nativeButton.Context)); } _backgroundDrawable.Button = _button; var useDefaultPadding = _button.OnThisPlatform().UseDefaultPadding(); int paddingTop = useDefaultPadding ? _nativeButton.PaddingTop : 0; int paddingLeft = useDefaultPadding ? _nativeButton.PaddingLeft : 0; var useDefaultShadow = _button.OnThisPlatform().UseDefaultShadow(); float shadowRadius = useDefaultShadow ? 2 : _nativeButton.ShadowRadius; float shadowDx = useDefaultShadow ? 0 : _nativeButton.ShadowDx; float shadowDy = useDefaultShadow ? 4 : _nativeButton.ShadowDy; AColor shadowColor = useDefaultShadow ? _backgroundDrawable.PressedBackgroundColor.ToAndroid() : _nativeButton.ShadowColor; _backgroundDrawable.SetPadding(paddingTop, paddingLeft) .SetShadow(shadowDy, shadowDx, shadowColor, shadowRadius); if (_drawableEnabled) { return; } if (_defaultDrawable == null) { _defaultDrawable = _nativeButton.Background; } if (Forms.IsLollipopOrNewer) { var rippleColor = _backgroundDrawable.PressedBackgroundColor.ToAndroid(); _rippleDrawable = new RippleDrawable(ColorStateList.ValueOf(rippleColor), _backgroundDrawable, null); _nativeButton.SetBackground(_rippleDrawable); } else { _nativeButton.SetBackground(_backgroundDrawable); } _drawableEnabled = true; } _nativeButton.Invalidate(); }
public void UpdateDrawable() { if (_button == null || _nativeButton == null) { return; } bool cornerRadiusIsDefault = !_button.IsSet(Button.CornerRadiusProperty) || (_button.CornerRadius == (int)Button.CornerRadiusProperty.DefaultValue || _button.CornerRadius == ButtonDrawable.DefaultCornerRadius); bool backgroundColorIsDefault = !_button.IsSet(VisualElement.BackgroundColorProperty) || _button.BackgroundColor == (Color)VisualElement.BackgroundColorProperty.DefaultValue; bool borderColorIsDefault = !_button.IsSet(Button.BorderColorProperty) || _button.BorderColor == (Color)Button.BorderColorProperty.DefaultValue; bool borderWidthIsDefault = !_button.IsSet(Button.BorderWidthProperty) || _button.BorderWidth == (double)Button.BorderWidthProperty.DefaultValue; if (backgroundColorIsDefault && cornerRadiusIsDefault && borderColorIsDefault && borderWidthIsDefault) { if (!_drawableEnabled) { return; } if (_defaultDrawable != null) { _nativeButton.SetBackground(_defaultDrawable); } _drawableEnabled = false; } else { if (_backgroundDrawable == null) { _backgroundDrawable = new ButtonDrawable(_nativeButton.Context.ToPixels, Forms.GetColorButtonNormal(_nativeButton.Context)); } _backgroundDrawable.Button = _button; var useDefaultPadding = _button.OnThisPlatform().UseDefaultPadding(); int paddingTop = useDefaultPadding ? _nativeButton.PaddingTop : 0; int paddingLeft = useDefaultPadding ? _nativeButton.PaddingLeft : 0; var useDefaultShadow = _button.OnThisPlatform().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 = _nativeButton.ShadowRadius; shadowDy = _nativeButton.ShadowDy; shadowDx = _nativeButton.ShadowDx; shadowColor = _nativeButton.ShadowColor; } _backgroundDrawable.SetPadding(paddingTop, paddingLeft) .SetShadow(shadowDy, shadowDx, shadowColor, shadowRadius); if (_drawableEnabled) { return; } if (_defaultDrawable == null) { _defaultDrawable = _nativeButton.Background; } if (Forms.IsLollipopOrNewer) { var rippleColor = _backgroundDrawable.PressedBackgroundColor.ToAndroid(); _rippleDrawable = new RippleDrawable(ColorStateList.ValueOf(rippleColor), _backgroundDrawable, null); _nativeButton.SetBackground(_rippleDrawable); } else { _nativeButton.SetBackground(_backgroundDrawable); } _drawableEnabled = true; } _nativeButton.Invalidate(); }