protected override void OnElementChanged(ElementChangedEventArgs <ColorGradientBox> e) { base.OnElementChanged(e); GradientDrawable.Orientation orientation = (Element.Orientation == StackOrientation.Horizontal ? GradientDrawable.Orientation.LeftRight : GradientDrawable.Orientation.BottomTop); int[] colors = { Element.EndColor.ToAndroid(), Element.StartColor.ToAndroid() }; if (Android.OS.Build.VERSION.SdkInt < Android.OS.BuildVersionCodes.JellyBean) #pragma warning disable CS0618 // Type or member is obsolete { SetBackgroundDrawable(e.NewElement != null ? new GradientDrawable(orientation, colors) : null); } #pragma warning restore CS0618 // Type or member is obsolete else { if (Android.OS.Build.VERSION.SdkInt < Android.OS.BuildVersionCodes.JellyBean) #pragma warning disable CS0618 // Type or member is obsolete { SetBackgroundDrawable(e.NewElement != null ? new GradientDrawable(orientation, colors) : null); } #pragma warning restore CS0618 // Type or member is obsolete else { Background = e.NewElement != null ? new GradientDrawable(orientation, colors) : null; } } }
protected override void OnElementChanged(ElementChangedEventArgs <Xamarin.Forms.Image> e) { base.OnElementChanged(e); int startColor = BaseControl.StartColor.ToAndroid().ToArgb(); int endColor = BaseControl.EndColor.ToAndroid().ToArgb(); int[] colors = { startColor, endColor }; //float[] radii = { 5, 5, 5, 5, 5, 5, 5, 5 }; GradientDrawable.Orientation orientation = null; Android.Graphics.Drawables.GradientType gradientType; switch (BaseControl.Orientation) { case GradientOrientation.BottomTop: orientation = GradientDrawable.Orientation.BottomTop; break; case GradientOrientation.LeftRight: orientation = GradientDrawable.Orientation.LeftRight; break; case GradientOrientation.RightLeft: orientation = GradientDrawable.Orientation.RightLeft; break; case GradientOrientation.TopBottom: orientation = GradientDrawable.Orientation.TopBottom; break; default: orientation = GradientDrawable.Orientation.TopBottom; break; } switch (BaseControl.GradientStyle) { case GradientType.Linear: gradientType = Android.Graphics.Drawables.GradientType.LinearGradient; break; case GradientType.Sweep: gradientType = Android.Graphics.Drawables.GradientType.SweepGradient; break; case GradientType.Radial: gradientType = Android.Graphics.Drawables.GradientType.RadialGradient; break; default: gradientType = gradientType = Android.Graphics.Drawables.GradientType.LinearGradient; break; } GradientDrawable drawable = new GradientDrawable(orientation, colors); drawable.SetGradientType(gradientType); //drawable.SetCornerRadii(radii); //drawable.SetStroke(1, Android.Graphics.Color.ParseColor("#FF000000")); drawable.SetCornerRadius(0f); this.Control.SetBackgroundDrawable(drawable); }
protected override void OnAttached() { GradientEffect gradientEffect = (GradientEffect)Element.Effects.First(e => e is GradientEffect); Android.Graphics.Color startColor = gradientEffect.StartColor.ToAndroid(); Android.Graphics.Color endColor = gradientEffect.EndColor.ToAndroid(); int[] colors = new int[] { startColor, endColor }; GradientDrawable.Orientation orientation = GradientDrawable.Orientation.TopBottom; switch (gradientEffect.Direction) { case GradientDirection.TopLeft_BottomRight: orientation = GradientDrawable.Orientation.TlBr; break; case GradientDirection.TopBottom: orientation = GradientDrawable.Orientation.TopBottom; break; case GradientDirection.TopRight_BottomLeft: orientation = GradientDrawable.Orientation.TrBl; break; case GradientDirection.LeftRight: orientation = GradientDrawable.Orientation.LeftRight; break; case GradientDirection.RightLeft: orientation = GradientDrawable.Orientation.RightLeft; break; case GradientDirection.BottomRight_TopLeft: orientation = GradientDrawable.Orientation.BrTl; break; case GradientDirection.BottomTop: orientation = GradientDrawable.Orientation.BottomTop; break; case GradientDirection.BottomLeft_TopRight: orientation = GradientDrawable.Orientation.BlTr; break; } GradientDrawable drawable = new GradientDrawable(orientation, colors); if (Control == null) { prevBackground = Container.Background; Container.Background = drawable; } else { prevBackground = Control.Background; Control.Background = drawable; } }
public static Drawable CreateBackgroundGradient(Color startColor, Color endColor, Color centerColor, float cornerRadius, GradientDrawable.Orientation orientation) { GradientDrawable gradientDrawable = new GradientDrawable(); gradientDrawable.SetShape(ShapeType.Rectangle); gradientDrawable.SetCornerRadius(cornerRadius); if (centerColor != Xamarin.Forms.Color.Transparent.ToAndroid()) { gradientDrawable.SetColors(new int[] { startColor, centerColor, endColor }); } else { gradientDrawable.SetColors(new int[] { startColor, endColor }); } gradientDrawable.SetGradientType(GradientType.LinearGradient); gradientDrawable.SetOrientation(orientation); return(gradientDrawable); }