private Drawable CreateNativeBorder(Brush background, Brush borderBrush, Thickness borderThickness, CornerRadius cornerRadius) { var cornerRadiusArray = new[] { (float)cornerRadius.TopLeft, (float)cornerRadius.TopLeft, (float)cornerRadius.TopRight, (float)cornerRadius.TopRight, (float)cornerRadius.BottomRight, (float)cornerRadius.BottomRight, (float)cornerRadius.BottomLeft, (float)cornerRadius.BottomLeft }; Paint backgroundPaint = new Paint(); Paint borderPaint = new Paint(); backgroundPaint.SetStyle(Paint.Style.Fill); borderPaint.SetStyle(Paint.Style.Fill); if (borderBrush == null) { borderPaint.Color = Color.Transparent; } else { if (borderBrush is SolidColorBrush) { var color = ((SolidColorBrush)borderBrush).Color; borderPaint.Color = new Color(color.R, color.G, color.B, color.A); } else { throw new Exception("SolidColorBrush BorderBrush is supported."); } } if (background == null) { backgroundPaint.Color = Color.Transparent; } else { if (background is SolidColorBrush) { var color = ((SolidColorBrush)background).Color; backgroundPaint.Color = new Color(color.R, color.G, color.B, color.A); } else { throw new Exception("SolidColorBrush Background is supported."); } } var rs = new RoundRectShape(cornerRadiusArray, null, null); var noChild = (this.Child == null || this.Child.Height.Equals(0) || this.Child.Width.Equals(0)) && (this.ActualHeight.Equals(0) || this.ActualWidth.Equals(0)); var left = ScreenProperties.ConvertDPIToPixels((float)(borderThickness.Left)); var top = ScreenProperties.ConvertDPIToPixels((float)(borderThickness.Top)); var right = ScreenProperties.ConvertDPIToPixels((float)(borderThickness.Right)); var bottom = ScreenProperties.ConvertDPIToPixels((float)(borderThickness.Bottom)); var pixelBorderThickness = new Thickness(left, top, right, bottom); Bitmap bitmap = null; using (ShapeDrawable sd = new CustomShapeDrawable(rs, backgroundPaint, borderPaint, cornerRadiusArray, pixelBorderThickness, noChild)) { var width = ScreenProperties.ConvertDPIToPixels((float)(this.ActualWidth)); var height = ScreenProperties.ConvertDPIToPixels((float)(this.ActualHeight)); if (height <= 0 || width <= 0) { return(new BitmapDrawable()); } bitmap = Bitmap.CreateBitmap((int)width, (int)height, Bitmap.Config.Argb8888); var canvas = new Android.Graphics.Canvas(bitmap); sd.SetBounds(0, 0, canvas.Width, canvas.Height); sd.Draw(canvas); } return(new BitmapDrawable(bitmap)); }
private Drawable CreateNativeBorder(Brush background, Brush borderBrush, Thickness borderThickness, CornerRadius cornerRadius) { var cornerRadiusArray = new[] { (float) cornerRadius.TopLeft, (float) cornerRadius.TopLeft, (float) cornerRadius.TopRight, (float) cornerRadius.TopRight, (float) cornerRadius.BottomRight, (float) cornerRadius.BottomRight, (float) cornerRadius.BottomLeft, (float) cornerRadius.BottomLeft }; Paint backgroundPaint = new Paint(); Paint borderPaint = new Paint(); backgroundPaint.SetStyle(Paint.Style.Fill); borderPaint.SetStyle(Paint.Style.Fill); if (borderBrush == null) { borderPaint.Color = Color.Transparent; } else { if (borderBrush is SolidColorBrush) { var color = ((SolidColorBrush)borderBrush).Color; borderPaint.Color = new Color(color.R, color.G, color.B, color.A); } else { throw new Exception("SolidColorBrush BorderBrush is supported."); } } if (background == null) { backgroundPaint.Color = Color.Transparent; } else { if (background is SolidColorBrush) { var color = ((SolidColorBrush)background).Color; backgroundPaint.Color = new Color(color.R, color.G, color.B, color.A); } else { throw new Exception("SolidColorBrush Background is supported."); } } var rs = new RoundRectShape(cornerRadiusArray, null, null); var noChild = (this.Child == null || this.Child.Height.Equals(0) || this.Child.Width.Equals(0)) && (this.ActualHeight.Equals(0) || this.ActualWidth.Equals(0)); var left = ScreenProperties.ConvertDPIToPixels((float)(borderThickness.Left)); var top = ScreenProperties.ConvertDPIToPixels((float)(borderThickness.Top)); var right = ScreenProperties.ConvertDPIToPixels((float)(borderThickness.Right)); var bottom = ScreenProperties.ConvertDPIToPixels((float)(borderThickness.Bottom)); var pixelBorderThickness = new Thickness(left, top, right, bottom); Bitmap bitmap = null; using (ShapeDrawable sd = new CustomShapeDrawable(rs, backgroundPaint, borderPaint, cornerRadiusArray, pixelBorderThickness, noChild)) { var width = ScreenProperties.ConvertDPIToPixels((float)(this.ActualWidth)); var height = ScreenProperties.ConvertDPIToPixels((float)(this.ActualHeight)); if (height <= 0 || width <= 0) return new BitmapDrawable(); bitmap = Bitmap.CreateBitmap((int)width, (int)height, Bitmap.Config.Argb8888); var canvas = new Android.Graphics.Canvas(bitmap); sd.SetBounds(0, 0, canvas.Width, canvas.Height); sd.Draw(canvas); } return new BitmapDrawable(bitmap); }