private void Update(Color color) { innerPaint.Color = color; outerPaint.Color = color; Drawable selector = CreateSelector(color); if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop) { int[][] states = new int[][]{ new int[]{ Android.Resource.Attribute.StatePressed } }; int[] colors = new int[]{ ShiftColorUp(color) }; ColorStateList rippleColors = new ColorStateList(states, colors); Foreground = new RippleDrawable(rippleColors, selector, null); } else { Foreground = selector; } }
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(); }
void RemoveRipple() { _viewOverlay.Foreground = null; _ripple?.Dispose(); _ripple = null; }
private void SetBackgroundCompat (Drawable drawable) { if (HasLollipopApi) { #if __ANDROID_21__ var elevation = 0.0f; if (hasShadow) { elevation = Elevation > 0.0f ? Elevation : Resources.GetDimensionPixelSize (Resource.Dimension.fab_elevation_lollipop); } Elevation = elevation; var states = new int[][] { new int[]{ } }; var rippleDrawable = new RippleDrawable (new Android.Content.Res.ColorStateList (states, new int[]{ colorRipple }), drawable, null); OutlineProvider = new MyOutlineProvider (Resources, size); ClipToOutline = true; Background = rippleDrawable; #endif } else if (HasJellyBeanApi) { #if __ANDROID_16__ Background = drawable; #endif } else { SetBackgroundDrawable (drawable); } }
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; _backgroundDrawable.SetPaddingTop(_nativeButton.PaddingTop); 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(); }