public static void UpdateBackground(this GradientDrawable gradientDrawable, Brush brush, int height, int width) { if (gradientDrawable == null || brush == null || brush.IsEmpty) { return; } if (brush is SolidColorBrush solidColorBrush) { Color bgColor = solidColorBrush.Color; gradientDrawable.SetColor(bgColor.IsDefault ? Color.Default.ToAndroid() : bgColor.ToAndroid()); } if (brush is LinearGradientBrush linearGradientBrush) { var p1 = linearGradientBrush.StartPoint; var x1 = (float)p1.X; var y1 = (float)p1.Y; var p2 = linearGradientBrush.EndPoint; var x2 = (float)p2.X; var y2 = (float)p2.Y; const double Rad2Deg = 180.0 / Math.PI; var angle = Math.Atan2(y2 - y1, x2 - x1) * Rad2Deg; var gradientBrushData = linearGradientBrush.GetGradientBrushData(); var colors = gradientBrushData.Item1; if (colors.Length < 2) { return; } gradientDrawable.SetGradientType(GradientType.LinearGradient); gradientDrawable.SetColors(colors); gradientDrawable.SetGradientOrientation(angle); } if (brush is RadialGradientBrush radialGradientBrush) { var center = radialGradientBrush.Center; float centerX = (float)center.X; float centerY = (float)center.Y; float radius = (float)radialGradientBrush.Radius; var gradientBrushData = radialGradientBrush.GetGradientBrushData(); var colors = gradientBrushData.Item1; if (colors.Length < 2) { return; } gradientDrawable.SetGradientType(GradientType.RadialGradient); gradientDrawable.SetGradientCenter(centerX, centerY); gradientDrawable.SetGradientRadius(Math.Max(height, width) * radius); gradientDrawable.SetColors(colors); } }