void SetDrawable(MyPickerRenderer myPickerRenderer) { var gradientDrawable = new GradientDrawable(); gradientDrawable.SetColor(myPickerRenderer.StaticColor.ToAndroid()); switch (myPickerRenderer.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.SetBackground(gradientDrawable); }
protected override void OnElementChanged(ElementChangedEventArgs <Editor> e) { base.OnElementChanged(e); if (e.NewElement != null) { var myControl = e.NewElement as CustomEditor; var radii = myControl.CornerRadii; var shape = new GradientDrawable(GradientDrawable.Orientation.TopBottom, new int[] { myControl.BackGroundColor.ToAndroid(), myControl.EndColor == null ? myControl.BackGroundColor.ToAndroid() : myControl.EndColor.ToAndroid() }); var radiuses = new float[8]; for (int i = 0; i < radii.Length; i++) { radiuses[i] = radii[i]; radiuses[i + 1] = radii[i]; } //// /// // shape.SetCornerRadii(radiuses); Control.Background = shape; Control.SetHintTextColor(Color.Transparent.ToAndroid()); } }
void SetCornerRadius(GradientDrawable backgroundGradient) { var cornerRadius = FrameRipple?.CornerRadius; if (!cornerRadius.HasValue) { return; } var topLeftCorner = Context.ToPixels(cornerRadius.Value.Left); var topRightCorner = Context.ToPixels(cornerRadius.Value.Top); var bottomLeftCorner = Context.ToPixels(cornerRadius.Value.Bottom); var bottomRightCorner = Context.ToPixels(cornerRadius.Value.Right); var cornerRadii = new[] { topLeftCorner, topLeftCorner, topRightCorner, topRightCorner, bottomRightCorner, bottomRightCorner, bottomLeftCorner, bottomLeftCorner, }; backgroundGradient?.SetCornerRadii(cornerRadii); }
/// <summary> /// Method triggered when changes occures on corner radius parameters /// </summary> private void UpdateCornerRadius(GradientDrawable gradientDrawable) { var cornerRadius = (Element as CrossRoundedFrame)?.CornerRadius; if (!cornerRadius.HasValue) { return; } var topLeftCorner = Context.ToPixels(cornerRadius.Value.TopLeft); var topRightCorner = Context.ToPixels(cornerRadius.Value.TopRight); var bottomLeftCorner = Context.ToPixels(cornerRadius.Value.BottomLeft); var bottomRightCorner = Context.ToPixels(cornerRadius.Value.BottomRight); var cornerRadii = new[] { topLeftCorner, topLeftCorner, topRightCorner, topRightCorner, bottomRightCorner, bottomRightCorner, bottomLeftCorner, bottomLeftCorner, }; gradientDrawable.SetCornerRadii(cornerRadii); }
protected override void OnElementChanged(ElementChangedEventArgs <CommonBorder> e) { base.OnElementChanged(e); if (Control == null && e.NewElement != null) { _border = new AndroidView(this.Context) { }; GradientDrawable shape = new GradientDrawable(); shape.SetShape(ShapeType.Rectangle); shape.SetCornerRadii(new float[] { (float)Element.CornerRadius, (float)Element.CornerRadius, (float)Element.CornerRadius, (float)Element.CornerRadius, (float)Element.CornerRadius, (float)Element.CornerRadius, (float)Element.CornerRadius, (float)Element.CornerRadius, }); shape.SetColor(Element.Color.ToAndroid()); shape.SetStroke((int)Element.BorderThickness, Element.BorderColor.ToAndroid()); _border.Background = shape; if (!e.NewElement.InputTransparent) { _border.Touch += _border_Touch; } SetNativeControl(_border); } }
void CreateShapeDrawable(MvvmAspire.Controls.Button element) { GradientDrawable shape = new GradientDrawable(); shape.SetShape(ShapeType.Rectangle); if (element.CornerRadius != 0) { shape.SetCornerRadii(new float[] { BaseUIHelper.ConvertDPToPixels(element.Corner.Top), BaseUIHelper.ConvertDPToPixels(element.Corner.Top), BaseUIHelper.ConvertDPToPixels(element.Corner.Right), BaseUIHelper.ConvertDPToPixels(element.Corner.Right), BaseUIHelper.ConvertDPToPixels(element.Corner.Bottom), BaseUIHelper.ConvertDPToPixels(element.Corner.Bottom), BaseUIHelper.ConvertDPToPixels(element.Corner.Left), BaseUIHelper.ConvertDPToPixels(element.Corner.Left) }); } if (Element.BackgroundColor != Xamarin.Forms.Color.Default) { shape.SetColor(Element.BackgroundColor.ToAndroid()); } if (element.BorderColor != Xamarin.Forms.Color.Default) { shape.SetStroke(2, element.BorderColor.ToAndroid()); } Control.Background = shape; }
void ApplyBorder(int width, Color color, int cornerRadius) { var topLeftCorner = Context.ToPixels(cornerRadius); var topRightCorner = Context.ToPixels(cornerRadius); var bottomLeftCorner = Context.ToPixels(cornerRadius); var bottomRightCorner = Context.ToPixels(cornerRadius); var cornerRadii = new[] { topLeftCorner, topLeftCorner, topRightCorner, topRightCorner, bottomRightCorner, bottomRightCorner, bottomLeftCorner, bottomLeftCorner, }; GradientDrawable gd = new GradientDrawable(); gd.SetCornerRadii(cornerRadii); //gd.SetCornerRadius(cornerRadius); gd.SetStroke(width, color.ToAndroid()); this.Control.Background = gd; }
public static void RoundCornerView(View v) { GradientDrawable shape = new GradientDrawable(); shape.SetShape(ShapeType.Rectangle); shape.SetCornerRadii(new float[] { 12, 12, 12, 12, 12, 12, 12, 12 }); v.SetBackgroundDrawable(shape); }
/// <summary> /// This methods creates a Drawable of the main box control shadow /// </summary> /// <param name="shadowColour">Xamarin.Forms.Color: </param> /// <param name="cornerRadius">CornerRadius: Collection of the radii of the corners of the box</param> /// <param name="boxBorderThickness">Double: Thickness of the box border</param> /// <param name="xDeltaPosition">Double: X translation of shadow in relation to box</param> /// <param name="yDeltaPosition">Double: Y translation of shadow in relation to box</param> /// <param name="boxColourAlpha">Double: Alpha (opacity) of the box colour</param> /// <param name="boxBorderAlpha">Double: Alpha (opacity) of the box border colour</param> /// <returns>GradientDrawable</returns> private GradientDrawable CreateShadow(XFColor shadowColour, CornerRadius cornerRadius, double boxBorderThickness, double xDeltaPosition, double yDeltaPosition, double boxColourAlpha, double boxBorderAlpha, ForcedBoxShape forcedBoxShape) { GradientDrawable shadow = new GradientDrawable(); shadow.SetShape(ShapeType.Rectangle); //Adjust the shadow color to account for any transparency in the box. XFColor shadowColor = new XFColor(shadowColour.R, shadowColour.G, shadowColour.B, shadowColour.A * boxColourAlpha); XFColor shadowBorderColor = new XFColor(shadowColour.R, shadowColour.G, shadowColour.B, shadowColour.A * boxBorderAlpha); shadow.SetColor(shadowColor.ToAndroid()); shadow.SetStroke((int)boxBorderThickness, shadowBorderColor.ToAndroid()); // Get Rectangle area to use for the shadow, offset it, and set the bounds of the drawable Rect rectS = new Rect(); GetDrawingRect(rectS); if (forcedBoxShape != ForcedBoxShape.Default) { rectS = GetCenteredSquareRect(rectS); if (forcedBoxShape == ForcedBoxShape.Circle) { // if its a circle, force all the corner radii to be equal to half the width shadow.SetCornerRadius(rectS.Width() / 2); } else { shadow.SetCornerRadii(new float[] { (float)cornerRadius.TopLeft, (float)cornerRadius.TopLeft, (float)cornerRadius.TopRight, (float)cornerRadius.TopRight, (float)cornerRadius.BottomRight, (float)cornerRadius.BottomRight, (float)cornerRadius.BottomLeft, (float)cornerRadius.BottomLeft }); } } else { shadow.SetCornerRadii(new float[] { (float)cornerRadius.TopLeft, (float)cornerRadius.TopLeft, (float)cornerRadius.TopRight, (float)cornerRadius.TopRight, (float)cornerRadius.BottomRight, (float)cornerRadius.BottomRight, (float)cornerRadius.BottomLeft, (float)cornerRadius.BottomLeft }); } rectS.Offset((int)System.Math.Round(xDeltaPosition), (int)System.Math.Round(yDeltaPosition)); shadow.Bounds = rectS; return(shadow); }
public static void CustomView(View v, int borderColor) { GradientDrawable shape = new GradientDrawable(); shape.SetShape(ShapeType.Rectangle); shape.SetCornerRadii(new float[] { 8, 0, 8, 0, 0, 0, 0, 0 }); v.SetBackgroundDrawable(shape); }
protected override void OnElementChanged(ElementChangedEventArgs <Editor> e) { base.OnElementChanged(e); if ((Control != null) && (e.OldElement == null)) { MyEditorRenderer myEditorRenderer = (MyEditorRenderer)Element; var gradientDrawable = new GradientDrawable(); gradientDrawable.SetColor(myEditorRenderer.StaticColor.ToAndroid()); switch (myEditorRenderer.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.SetPadding(40, 20, 40, 20); Control.SetBackground(gradientDrawable); } }
public static void SetBackgroundColorWithRoundedCorners(this View view, float topLeftRadius, float topRightRadius, float bottomLeftRadius, float bottomRightRadius, Android.Graphics.Color backgroundColor) { var sd = new GradientDrawable(); sd.SetColor(backgroundColor); sd.SetCornerRadii(new float[] { topLeftRadius.ToPixels(), topLeftRadius.ToPixels(), topRightRadius.ToPixels(), topRightRadius.ToPixels(), bottomLeftRadius.ToPixels(), bottomLeftRadius.ToPixels(), bottomRightRadius.ToPixels(), bottomRightRadius.ToPixels() }); view.SetBackgroundDrawable(sd); }
/// <summary> /// This methods creates a Drawable of the main box control /// </summary> /// <param name="boxColour">Xamarin.Forms.Color: Colour of the box to be drawn</param> /// <param name="cornerRadius">CornerRadius: Collection of the radii of the corners of the box</param> /// <param name="boxBorderColour">Xamarin.Forms.Color: Xamarin.Forms.Color: Colour of the box border</param> /// <param name="boxBorderThickness">Double: Thickness of the box border</param> /// <returns>GradientDrawable</returns> private GradientDrawable CreateBox(XFColor boxColour, CornerRadius cornerRadius, XFColor boxBorderColour, double boxBorderThickness, ForcedBoxShape forcedBoxShape) { // Create Box GradientDrawable box = new GradientDrawable(); box.SetShape(ShapeType.Rectangle); box.SetColor(boxColour.ToAndroid()); box.SetStroke((int)boxBorderThickness, boxBorderColour.ToAndroid()); // Get Rectangle area to use for the main box and set the bounds of the drawable Rect rect = new Rect(); GetDrawingRect(rect); if (forcedBoxShape != ForcedBoxShape.Default) { rect = GetCenteredSquareRect(rect); if (forcedBoxShape == ForcedBoxShape.Circle) { // if its a circle, force all the corner radii to be equal to half the width box.SetCornerRadius(rect.Width() / 2); } else { box.SetCornerRadii(new float[] { (float)cornerRadius.TopLeft, (float)cornerRadius.TopLeft, (float)cornerRadius.TopRight, (float)cornerRadius.TopRight, (float)cornerRadius.BottomRight, (float)cornerRadius.BottomRight, (float)cornerRadius.BottomLeft, (float)cornerRadius.BottomLeft }); } } else { box.SetCornerRadii(new float[] { (float)cornerRadius.TopLeft, (float)cornerRadius.TopLeft, (float)cornerRadius.TopRight, (float)cornerRadius.TopRight, (float)cornerRadius.BottomRight, (float)cornerRadius.BottomRight, (float)cornerRadius.BottomLeft, (float)cornerRadius.BottomLeft }); } box.Bounds = rect; return(box); }
protected override void OnElementChanged(ElementChangedEventArgs <Frame> e) { base.OnElementChanged(e); DottedFrame customFrame = Element as DottedFrame; float r = customFrame.CornerRadius; GradientDrawable shape = new GradientDrawable(); shape.SetCornerRadii(new float[] { r, r, r, r, r, r, r, r }); shape.SetColor(Android.Graphics.Color.Transparent); shape.SetStroke(2, customFrame.BorderColor.ToAndroid(), 2f, 20f); Control.SetBackground(shape); }
public static void UpdateBackground(Border border, Android.Views.View view) { var strokeThickness = border.StrokeThickness; var context = view.Context; var corners = new float[] { (float)border.CornerRadius.TopLeft, (float)border.CornerRadius.TopLeft, (float)border.CornerRadius.TopRight, (float)border.CornerRadius.TopRight, (float)border.CornerRadius.BottomRight, (float)border.CornerRadius.BottomRight, (float)border.CornerRadius.BottomLeft, (float)border.CornerRadius.BottomLeft }; GradientDrawable dab = null; dab = new GradientDrawable(); if (strokeThickness.HorizontalThickness + strokeThickness.VerticalThickness > 0) { dab.SetColor(border.BackgroundColor.ToAndroid()); dab.SetStroke((int)context.ToPixels(strokeThickness.Max()), border.Stroke.ToAndroid()); } dab.SetCornerRadii(corners); dab.SetColor(border.BackgroundColor.ToAndroid()); dab.SetCornerRadii(corners); view.Background = dab; view.SetPadding( (int)context.ToPixels(strokeThickness.Left + border.Padding.Left), (int)context.ToPixels(strokeThickness.Top + border.Padding.Top), (int)context.ToPixels(strokeThickness.Right + border.Padding.Right), (int)context.ToPixels(strokeThickness.Bottom + border.Padding.Bottom)); }
protected override void OnElementChanged(ElementChangedEventArgs <Label> e) { base.OnElementChanged(e); if (Control != null) { GradientDrawable gd = new GradientDrawable(); float[] radi = { 0, 0, 100, 100, 100, 100, 100, 100 }; gd.SetCornerRadii(radi); gd.SetColor(global::Android.Graphics.Color.Rgb(191, 182, 167)); this.Control.SetBackgroundDrawable(gd); this.Control.SetPaddingRelative(30, 20, 30, 20); } }
private void SetCornerRadius() { var elm = Element as GuruTest.Controls.ChatBubbleView; GradientDrawable gd = new GradientDrawable(); gd.SetCornerRadii(new float[] { 0f, 0f, elm.Radius * 2, elm.Radius * 2, elm.Radius * 2, elm.Radius * 2, elm.Radius * 2, elm.Radius * 2 }); var color = (Background as ColorDrawable)?.Color ?? Android.Graphics.Color.Red; gd.SetColor(color); this.Background = gd; }
protected override void OnElementChanged(ElementChangedEventArgs <Label> e) { base.OnElementChanged(e); if (Control != null) { GradientDrawable gd = new GradientDrawable(); float[] radi = { 0, 0, 100, 100, 100, 100, 100, 100 }; gd.SetCornerRadii(radi); gd.SetColor(global::Android.Graphics.Color.Transparent); gd.SetStroke(3, Android.Graphics.Color.Silver); Control.SetBackgroundDrawable(gd); var dnm = (int)App.ScreenWidth / 30; Control.SetPaddingRelative(dnm, dnm, dnm, dnm); } }
protected override void OnElementChanged(ElementChangedEventArgs <Entry> e) { base.OnElementChanged(e); if (Control != null) { Control.Background = null; GradientDrawable gd = new GradientDrawable(); gd.SetColor(Android.Graphics.Color.White); float[] a = new float[] { 8, 8, 8, 8, 8, 8, 8, 8 }; gd.SetCornerRadii(a); gd.SetStroke(2, Android.Graphics.Color.Rgb(216, 224, 228)); Control.SetBackgroundDrawable(gd); Control.SetPaddingRelative(12, 12, 12, 12); } }
private void UpdateCornerRadius() { if (Control.Background is GradientDrawable backgroundGradient) { var cornerRadius = (Element as CustomFrame)?.CornerRadius; if (!cornerRadius.HasValue) { return; } var topLeftCorner = Context.ToPixels(cornerRadius.Value.TopLeft); var topRightCorner = Context.ToPixels(cornerRadius.Value.TopRight); var bottomLeftCorner = Context.ToPixels(cornerRadius.Value.BottomLeft); var bottomRightCorner = Context.ToPixels(cornerRadius.Value.BottomRight); var cornerRadii = new[] { topLeftCorner, topLeftCorner, topRightCorner, topRightCorner, bottomRightCorner, bottomRightCorner, bottomLeftCorner, bottomLeftCorner, }; //backgroundGradient.SetCornerRadii(cornerRadii); var color1 = (Element as CustomFrame)?.Color1; //#62d248 var color2 = (Element as CustomFrame)?.Color2; //#8eeb41 var color3 = (Element as CustomFrame)?.Color3; //#b0eb41 int[] colors = new[] { Android.Graphics.Color.ParseColor(color1).ToArgb(), Android.Graphics.Color.ParseColor(color2).ToArgb(), Android.Graphics.Color.ParseColor(color3).ToArgb() }; GradientDrawable gradientDrawable = new GradientDrawable( GradientDrawable.Orientation.TopBottom, colors); gradientDrawable.SetCornerRadii(cornerRadii); Control.Background = gradientDrawable; } }
public void CreateGradient() { //Need to convert the colors to Android Color objects int[] androidColors = new int[gradientColors.Count()]; for (int i = 0; i < gradientColors.Count(); i++) { Xamarin.Forms.Color temp = gradientColors[i]; androidColors[i] = temp.ToAndroid(); } GradientDrawable gradient = new GradientDrawable(GradientDrawable.Orientation.LeftRight, androidColors); if (roundCorners) { gradient.SetCornerRadii(new float[] { cornerRadius, cornerRadius, cornerRadius, cornerRadius, cornerRadius, cornerRadius, cornerRadius, cornerRadius }); } layout.SetBackground(gradient); }
private void InitBackgroundColor() { GradientDrawable drawable = new GradientDrawable(); drawable.SetShape(ShapeType.Rectangle); drawable.SetCornerRadii(new float[] { CornerRadius, CornerRadius, CornerRadius, CornerRadius, CornerRadius, CornerRadius, CornerRadius, CornerRadius }); drawable.SetColor(_isSelected ? _selectedBackgroundColor : _backgroundColor); drawable.SetStroke(_strokeSize, _strokeColor); if (Build.VERSION.SdkInt >= BuildVersionCodes.JellyBean) { Background = drawable; } else { #pragma warning disable CS0618 // Type or member is obsolete SetBackgroundDrawable(drawable); #pragma warning restore CS0618 // Type or member is obsolete } }
protected override void OnElementChanged(ElementChangedEventArgs <CustomFrame> e) { //taking the main custom frame (that exists in shared app) CustomFrame customFram = e.NewElement as CustomFrame; // Create a drawable for the button's normal state drawable = new Android.Graphics.Drawables.GradientDrawable(); //taking the BackgroundColor property value that exists in ContentView and give it to the drawable object drawable.SetColor(customFram.BackgroundColor.ToAndroid()); //taking the BorderWidth property value that we add it in CustomFrame and give it to the drawable object drawable.SetStroke(customFram.BorderWidth, customFram.BorderColor.ToAndroid()); //taking the BorderRadious properties values that we add it in CustomFrame and send it to SetCornerRaii method drawable.SetCornerRadii(new float[] { (float)customFram.BorderRadiusTopLeft, (float)customFram.BorderRadiusTopLeft, (float)customFram.BorderRadiusTopRight, (float)customFram.BorderRadiusTopRight, (float)customFram.BorderRadiusBottomLeft, (float)customFram.BorderRadiusBottomLeft, (float)customFram.BorderRadiusBottomRight, (float)customFram.BorderRadiusBottomRight }); //put the drawable object in the content view Background SetBackgroundDrawable(drawable); //call the base OnElementChanged method base.OnElementChanged(e); }
protected LayerDrawable GradientConverter(String Gradient) { var roundRect = new RoundRectShape(CornerRadiusArray, null, null); ShapeDrawable sf = new ShapeDrawable(roundRect); sf.SetIntrinsicHeight(100); sf.SetIntrinsicWidth(200); sf.SetShaderFactory(new GradientShader(Gradient)); ShapeDrawable shadow = new ShapeDrawable(roundRect); shadow.SetIntrinsicHeight(100); shadow.SetIntrinsicWidth(200); shadow.SetShaderFactory(new ShadowShader(BaseControl)); LayerDrawable ld = null; if (Element.BorderColor != Xamarin.Forms.Color.Default) { GradientDrawable stroke = new GradientDrawable(); stroke.SetCornerRadii(CornerRadiusArray); stroke.SetStroke(2, Element.BorderColor.ToAndroid()); ld = new LayerDrawable(new Drawable[] { stroke, shadow, sf }); ld.SetLayerInset(0, 0, 0, 3, 3); ld.SetLayerInset(1, 5, 5, 0, 0); // inset the shadow so it doesn't start right at the left/top ld.SetLayerInset(2, 2, 2, 5, 5); } else { ld = new LayerDrawable(new Drawable[] { shadow, sf }); ld.SetLayerInset(0, 5, 5, 0, 0); // inset the shadow so it doesn't start right at the left/top ld.SetLayerInset(1, 0, 0, 5, 5); } return(ld); }
private void UpdateBorder() { var view = Control ?? Container; if (view == null) { return; } var cornerRadius = view.Context.ToPixels(TotalTechApp.Effects.BackgroundEffect.GetRadius(Element)); var roundCorners = TotalTechApp.Effects.BackgroundEffect.GetRoundCorners(Element); var strokeWidth = (int)view.Context.ToPixels(TotalTechApp.Effects.BackgroundEffect.GetBorderWidth(Element)); var strokeColor = TotalTechApp.Effects.BackgroundEffect.GetBorderColor(Element).ToAndroid(); var color = TotalTechApp.Effects.BackgroundEffect.GetBackgroundColor(Element).ToAndroid(); var drawable = new GradientDrawable(); drawable.SetStroke(strokeWidth, strokeColor); drawable.SetCornerRadii(GetRadii(roundCorners, cornerRadius)); drawable.SetColor(color); view.SetBackground(drawable); }
protected override void OnElementChanged(ElementChangedEventArgs <Frame> e) { base.OnElementChanged(e); if (e.NewElement != null && e.OldElement == null) { GradientDrawable drawable = new GradientDrawable(); var roundedFrame = e.NewElement as RoundedFrame; float[] radius = new float[8]; radius[0] = roundedFrame.RoundTopLeft ? Context.ToPixels(roundedFrame.CornerRadius) : 0; //Top Left corner radius[1] = roundedFrame.RoundTopLeft ? Context.ToPixels(roundedFrame.CornerRadius) : 0; //Top Left corner radius[2] = roundedFrame.RoundTopRight ? Context.ToPixels(roundedFrame.CornerRadius) : 0; //Top Right corner radius[3] = roundedFrame.RoundTopRight ? Context.ToPixels(roundedFrame.CornerRadius) : 0; //Top Right corner radius[4] = roundedFrame.RoundBottomRight ? Context.ToPixels(roundedFrame.CornerRadius) : 0; //Bottom Right corner radius[5] = roundedFrame.RoundBottomRight ? Context.ToPixels(roundedFrame.CornerRadius) : 0; //Bottom Right corner radius[6] = roundedFrame.RoundBottomLeft ? Context.ToPixels(roundedFrame.CornerRadius) : 0; //Bottom Left corner radius[7] = roundedFrame.RoundBottomLeft ? Context.ToPixels(roundedFrame.CornerRadius) : 0; //Bottom Left corner drawable.SetCornerRadii(radius); drawable.SetColor(roundedFrame.BackgroundColor.ToAndroid()); this.SetBackground(drawable); } }
private void UpdateRipple() { if (_nativeView == null || _backgroundElement == null) { return; } if (_nativeView is MaterialCardView || _nativeView is Chip || _nativeView is MaterialButton) { return; } switch (_nativeView?.Foreground) { case RippleDrawable _ when !_backgroundElement.IsRippleEnabled: _nativeView.Foreground?.Dispose(); _nativeView.Foreground = null; _nativeView.Clickable = _defaultClickable; _nativeView.Focusable = _defaultFocusable; break; case RippleDrawable oldRippleDrawable: oldRippleDrawable.SetColor(ColorStateList.ValueOf(_backgroundElement.RippleColor.ToAndroid())); break; case null when _backgroundElement.IsRippleEnabled: var maskDrawable = new GradientDrawable(); maskDrawable.SetShape(ShapeType.Rectangle); maskDrawable.SetColor(new AColor(0, 0, 0, 255)); maskDrawable.SetCornerRadii(_backgroundElement.CornerRadius.ToRadii( _context.Resources.DisplayMetrics.Density)); var rippleColorStateList = ColorStateList.ValueOf(_backgroundElement.RippleColor.ToAndroid()); _nativeView.Foreground = new RippleDrawable(rippleColorStateList, null, maskDrawable); _nativeView.Clickable = true; _nativeView.Focusable = true; break; } }
void CreateShapeDrawable() { GradientDrawable shape = new GradientDrawable(); shape.SetShape(ShapeType.Rectangle); float[] cornerRadii = new float[] { 10, 10, 10, 10, 10, 10, 10, 10 }; if (Base.CornerRadius != "-1") { var cornerRad = Base.CornerRadius.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries); cornerRadii = new float[] { BaseUIHelper.ConvertDPToPixels(float.Parse(cornerRad[0])), BaseUIHelper.ConvertDPToPixels(float.Parse(cornerRad[1])), BaseUIHelper.ConvertDPToPixels(float.Parse(cornerRad[2])), BaseUIHelper.ConvertDPToPixels(float.Parse(cornerRad[3])), BaseUIHelper.ConvertDPToPixels(float.Parse(cornerRad[4])), BaseUIHelper.ConvertDPToPixels(float.Parse(cornerRad[5])), BaseUIHelper.ConvertDPToPixels(float.Parse(cornerRad[6])), BaseUIHelper.ConvertDPToPixels(float.Parse(cornerRad[7])) }; } shape.SetCornerRadii(cornerRadii); var element = Element as RoundedFrame; if (element.BackgroundColor != Xamarin.Forms.Color.Default) { shape.SetColor(element.BackgroundColor.ToAndroid()); } if (Base.HasBorder) { shape.SetStroke(1, Base.BorderColor.ToAndroid()); } Control.Background = shape; }
private void UpdateGradientColors() { var extendedButton = Element as ExtendedButton; var gradientColors = extendedButton?.GradientColors; if (gradientColors == null || !gradientColors.Any() || extendedButton.Width <= 0 || extendedButton.Height <= 0) { return; } /* ================================================================================================== * convert the colors to Android Color objects * ================================================================================================*/ var androidColors = gradientColors.Select(arg => (int)arg.ToAndroid()).ToArray(); /* ================================================================================================== * create the gradient * ================================================================================================*/ _gradient = _gradient ?? new GradientDrawable(GradientDrawable.Orientation.LeftRight, androidColors); /* ================================================================================================== * set the flow * ================================================================================================*/ switch (extendedButton.GradientFlow) { case ExtendedButton.Flows.LeftToRight: _gradient.SetOrientation(GradientDrawable.Orientation.LeftRight); break; case ExtendedButton.Flows.TopDown: _gradient.SetOrientation(GradientDrawable.Orientation.TopBottom); break; default: throw new ArgumentOutOfRangeException($"The {nameof(ExtendedButton.GradientFlow)}: {extendedButton?.GradientFlow.ToString() ?? "-"} is not supported yet!"); } /* ================================================================================================== * update conrner radius * ================================================================================================*/ var cornerRadius = extendedButton.CornerRadius; _gradient.SetCornerRadii(new float[] { cornerRadius, cornerRadius, cornerRadius, cornerRadius, cornerRadius, cornerRadius, cornerRadius, cornerRadius }); /* ================================================================================================== * finish up * ================================================================================================*/ if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.M) { /* ================================================================================================== * keep the built-in effect of button * ================================================================================================*/ var bg = Control.Background as RippleDrawable; int[][] states = { new int[] { Android.Resource.Attribute.StateEnabled }, // enabled new int[] { Android.Resource.Attribute.OnClick }, // hover new int[] { -Android.Resource.Attribute.StateEnabled }, // Disabled }; var colors = new int[] { Android.Graphics.Color.LightGray, Android.Graphics.Color.DarkGray, Android.Graphics.Color.DarkGray }; bg.SetColor(new ColorStateList(states, colors)); if (bg.NumberOfLayers < 2) { bg?.AddLayer(_gradient); } Control.SetBackground(bg); } else { // todo: keep base hightlight effect Control?.SetBackground(_gradient); lock (_lockObj) { if (!_isEventSet) { //Control.Touch += Control_Touch; _isEventSet = true; } } } }
public static void SetCornerRadius(this AView view, Context context, VisualElement element, CornerRadius cornerRadius, Color?color) { if (view == null || cornerRadius == new CornerRadius(0d)) { return; } var isUniform = cornerRadius.IsAllRadius() && !cornerRadius.IsEmpty(); var uniformCornerRadius = context.ToPixels(cornerRadius.TopLeft); var cornerRadii = cornerRadius.ToRadii(context.Resources.DisplayMetrics.Density); switch (view.Background) { case GradientDrawable gradientDrawable: if (isUniform) { gradientDrawable.SetCornerRadius(uniformCornerRadius); } else { gradientDrawable.SetCornerRadii(cornerRadii); } break; case PaintDrawable paintDrawable: if (isUniform) { paintDrawable.SetCornerRadius(uniformCornerRadius); } else { paintDrawable.SetCornerRadii(cornerRadii); } break; default: view.Background?.Dispose(); var newGradientDrawable = new GradientDrawable(); if (isUniform) { newGradientDrawable.SetCornerRadius(uniformCornerRadius); } else { newGradientDrawable.SetCornerRadii(cornerRadii); } if (color != null) { newGradientDrawable.SetColor(color.Value.ToAndroid()); } else if (element?.BackgroundColor != null) { newGradientDrawable.SetColor(element.BackgroundColor.ToAndroid()); } view.Background = newGradientDrawable; break; } }