void DrawBackground(ACanvas canvas, int width, int height, CornerRadius cornerRadius, bool pressed) { using (Paint paint = new Paint { AntiAlias = true }) using (Path.Direction direction = Path.Direction.Cw) using (Paint.Style style = Paint.Style.Fill) { Path path = new Path(); using (RectF rect = new RectF(0, 0, width, height)) { float topLeft = this._convertToPixels(cornerRadius.TopLeft); float topRight = this._convertToPixels(cornerRadius.TopRight); float bottomRight = this._convertToPixels(cornerRadius.BottomRight); float bottomLeft = this._convertToPixels(cornerRadius.BottomLeft); if (!this._corneredContentView.HasShadow) { path.AddRoundRect(rect, new float[] { topLeft, topLeft, topRight, topRight, bottomRight, bottomRight, bottomLeft, bottomLeft }, direction); } else { path.AddRoundRect(rect, new float[] { topLeft, topLeft, topLeft, topLeft, topLeft, topLeft, topLeft, topLeft }, direction); } } global::Android.Graphics.Color color = this._corneredContentView.BackgroundColor.ToAndroid(); paint.SetStyle(style); paint.Color = color; canvas.DrawPath(path, paint); } }
void DrawBackground(ACanvas canvas, int width, int height, float cornerRadius, bool pressed) { using (var paint = new Paint { AntiAlias = true }) using (var path = new APath()) using (APath.Direction direction = APath.Direction.Cw) using (Paint.Style style = Paint.Style.Fill) using (var rect = new RectF(0, 0, width, height)) { float rx = _convertToPixels(cornerRadius); float ry = _convertToPixels(cornerRadius); path.AddRoundRect(rect, rx, ry, direction); paint.SetStyle(style); if (!Brush.IsNullOrEmpty(_frame.Background)) { Brush background = _frame.Background; paint.UpdateBackground(background, height, width); } else { global::Android.Graphics.Color color = _frame.BackgroundColor.ToAndroid(); paint.Color = color; } canvas.DrawPath(path, paint); } }
void DrawBackground(ACanvas canvas, int width, int height, float cornerRadius, bool pressed) { using (var paint = new Paint { AntiAlias = true }) using (var path = new Path()) using (Path.Direction direction = Path.Direction.Cw) using (Paint.Style style = Paint.Style.Fill) using (var rect = new RectF( _bounds + _boundsDiff + _contentBounds, _bounds + _boundsDiff + _contentBounds, width - (_bounds + _boundsDiff + _contentBounds), height - (_bounds + _boundsDiff + _contentBounds))) { float rx = _convertToPixels(cornerRadius); float ry = _convertToPixels(cornerRadius); path.AddRoundRect(rect, rx, ry, direction); global::Android.Graphics.Color color = _frame.BackgroundColor.ToAndroid(); paint.SetStyle(style); paint.Color = color; canvas.DrawPath(path, paint); } }
private void DrawBorder(ACanvas canvas, CorneredContentView control) { if (control.BorderThickness > 0) { float borderThickness = this.Context.ToPixels(control.BorderThickness); float halfBorderThickness = borderThickness / 2; using (Paint paint = new Paint { AntiAlias = true }) using (Path.Direction direction = Path.Direction.Cw) using (Paint.Style style = Paint.Style.Stroke) using (RectF rect = new RectF(!control.HasShadow ? -halfBorderThickness : halfBorderThickness, !control.HasShadow ? -halfBorderThickness : halfBorderThickness, !control.HasShadow ? canvas.Width + halfBorderThickness : canvas.Width - halfBorderThickness, !control.HasShadow ? canvas.Height + halfBorderThickness : canvas.Height - halfBorderThickness)) { Path path = new Path(); path.AddRoundRect(rect, this.GetRadii(control), direction); paint.Color = control.BorderColor.ToAndroid(); paint.StrokeCap = Paint.Cap.Square; paint.StrokeWidth = borderThickness; paint.SetStyle(style); canvas.DrawPath(path, paint); } } }
void DrawOutline(Android.Graphics.Canvas canvas, int width, int height) { if (this.Element is FrameApp) { int strokeWidth = (int)TypedValue.ApplyDimension(ComplexUnitType.Dip, 1, Context.Resources.DisplayMetrics); using (var paint = new Paint { AntiAlias = true }) using (var path = new Path()) using (Path.Direction direction = Path.Direction.Cw) using (Paint.Style style = Paint.Style.Stroke) using (var rect = new RectF(strokeWidth + strokeWidth / 2, strokeWidth + strokeWidth / 2, width - strokeWidth - strokeWidth / 2, height - strokeWidth - strokeWidth / 2)) { if (this.Element.CornerRadius > 0) { float rx = this.Context.ToPixels(this.Element.CornerRadius); float ry = this.Context.ToPixels(this.Element.CornerRadius); path.AddRoundRect(rect, rx, ry, direction); } else { path.AddRect(rect, direction); } paint.StrokeWidth = strokeWidth; //set outline stroke paint.SetStyle(style); paint.Color = (this.Element as FrameApp).BorderColor.ToAndroid(); canvas.DrawPath(path, paint); } } }
void DrawOutline(ACanvas canvas, int width, int height, float cornerRadius) { using (var paint = new Paint { AntiAlias = true }) using (var path = new Path()) using (Path.Direction direction = Path.Direction.Cw) using (Paint.Style style = Paint.Style.Stroke) using (var rect = new RectF(left: _bounds, top: _bounds, right: width - _bounds, bottom: height - _bounds)) using (var innerRect = new RectF( left: _bounds + _boundsDiff, top: _bounds + _boundsDiff, right: width - (_bounds + _boundsDiff), bottom: height - (_bounds + _boundsDiff))) { float rx = _convertToPixels(cornerRadius); float ry = _convertToPixels(cornerRadius); path.AddRoundRect(rect, rx, ry, direction); path.AddRoundRect(innerRect, rx, ry, direction); paint.StrokeWidth = _frameThickness; paint.SetStyle(style); paint.Color = _frame.BorderColor.ToAndroid(); canvas.DrawPath(path, paint); } }
void DrawOutline(ACanvas canvas, int width, int height, Thickness cornerRadius) { var borderThickness = _convertToPixels(_pancake.BorderThickness); var halfBorderThickness = borderThickness / 2; // TODO: This doesn't look entirely right yet when using it with rounded corners. using (var paint = new Paint { AntiAlias = true }) using (var path = new Path()) using (Path.Direction direction = Path.Direction.Cw) using (Paint.Style style = Paint.Style.Stroke) using (var rect = new RectF(halfBorderThickness, halfBorderThickness, width - halfBorderThickness, height - halfBorderThickness)) { float topLeft = _convertToPixels(cornerRadius.Left); float topRight = _convertToPixels(cornerRadius.Top); float bottomRight = _convertToPixels(cornerRadius.Right); float bottomLeft = _convertToPixels(cornerRadius.Bottom); path.AddRoundRect(rect, new float[] { topLeft, topLeft, topRight, topRight, bottomRight, bottomRight, bottomLeft, bottomLeft }, direction); if (_pancake.BorderIsDashed) { paint.SetPathEffect(new DashPathEffect(new float[] { 10, 20 }, 0)); } paint.StrokeCap = Paint.Cap.Round; paint.StrokeWidth = borderThickness; paint.SetStyle(style); paint.Color = _pancake.BorderColor.ToAndroid(); canvas.DrawPath(path, paint); } }
private void DrawBorder(ACanvas canvas, PancakeView control) { var borderThickness = Context.ToPixels(control.BorderThickness); var halfBorderThickness = borderThickness / 2; // TODO: This doesn't look entirely right yet when using it with rounded corners. using (var paint = new Paint { AntiAlias = true }) using (var path = new Path()) using (Path.Direction direction = Path.Direction.Cw) using (Paint.Style style = Paint.Style.Stroke) using (var rect = new RectF(control.DrawBorderOnOutside && !control.HasShadow ? -halfBorderThickness : halfBorderThickness, control.DrawBorderOnOutside && !control.HasShadow ? -halfBorderThickness : halfBorderThickness, control.DrawBorderOnOutside && !control.HasShadow ? canvas.Width + halfBorderThickness : canvas.Width - halfBorderThickness, control.DrawBorderOnOutside && !control.HasShadow ? canvas.Height + halfBorderThickness : canvas.Height - halfBorderThickness)) { path.AddRoundRect(rect, GetRadii(control), direction); if (control.BorderIsDashed) { paint.SetPathEffect(new DashPathEffect(new float[] { 10, 20 }, 0)); } paint.StrokeCap = Paint.Cap.Square; paint.StrokeWidth = borderThickness; paint.SetStyle(style); paint.Color = control.BorderColor.ToAndroid(); canvas.DrawPath(path, paint); } }
public override void Draw(Canvas canvas) { base.Draw(canvas); if (Element == null || Element.BorderColor.A <= 0) { return; } using (var paint = new Paint { AntiAlias = true }) using (var path = new Path()) using (Path.Direction direction = Path.Direction.Cw) using (Paint.Style style = Paint.Style.Stroke) using (var rect = new RectF(0, 0, canvas.Width, canvas.Height)) { var raduis = Android.App.Application.Context.ToPixels(Element.CornerRadius); path.AddRoundRect(rect, raduis, raduis, direction); //paint.StrokeWidth = Context.Resources.DisplayMetrics.Density * 2; paint.SetStyle(style); paint.Color = Element.BorderColor.ToAndroid(); canvas.DrawPath(path, paint); } }
public static Paint GetDefaultPaint(Paint.Style style, int strokeWidth) { var paint = new Paint(); paint.SetStyle(style); paint.StrokeWidth = strokeWidth; return(paint); }
static public Paint Create(Color color, Paint.Style style, PaintFlags paint_flags) { Paint paint = new Paint(paint_flags); paint.Color = color; paint.SetStyle(style); return(paint); }
private void InitializeSignatureSettings() { _strokeColor = Color.Black; CanvasColor = Color.ParseColor("#ffffff"); _strokeWidth = (int)10f; _strokeStyle = Paint.Style.Stroke; _strokeAntiAlias = true; _strokeJoin = Paint.Join.Round; }
public static Paint paintFromColor(int color, Paint.Style style) { Paint p = new Paint(); p.AntiAlias = true; p.Dither = true; p.Color = color; p.Style = style; return(p); }
//创建画笔 private Paint CreatePaint(Color color, Paint.Style style, int strokeWidth = 3) { Paint paint = new Paint(); paint.Color = color; paint.SetStyle(style); paint.StrokeWidth = strokeWidth; return(paint); }
public void resetPaint(Paint.Style pStyle) { if (isStancil) { return; } this.mPaint.Reset(); this.mPaint.AntiAlias = true; // TODO AntiAliasing could be made optional through some SVGOptions object. this.mPaint.SetStyle(pStyle); }
public static Paint CreateFigurePaintStyle(Paint.Style style) { var figureStyle = new Paint { Color = Color.Red, StrokeWidth = 30 }; figureStyle.SetStyle(style); return(figureStyle); }
private static void DrawAndPaint(Canvas c, Rect rec, Paint.Style paintStyle, float strokeWidth, Color color) { using (Paint p = new Paint()) { p.AntiAlias = true; p.Dither = true; p.SetStyle(paintStyle); p.StrokeWidth = strokeWidth; p.Color = color; c.DrawRect(rec, p); } }
void DrawBackground(ACanvas canvas, int width, int height, CornerRadius cornerRadius) { using (var paint = new Paint { AntiAlias = true }) using (Path.Direction direction = Path.Direction.Cw) using (Paint.Style style = Paint.Style.Fill) { var path = new Path(); if (_pancake.Sides != 4) { path = DrawingExtensions.CreatePolygonPath(width, height, _pancake.Sides, _pancake.CornerRadius.TopLeft, _pancake.OffsetAngle); } else { float topLeft = _convertToPixels(cornerRadius.TopLeft); float topRight = _convertToPixels(cornerRadius.TopRight); float bottomRight = _convertToPixels(cornerRadius.BottomRight); float bottomLeft = _convertToPixels(cornerRadius.BottomLeft); path = DrawingExtensions.CreateRoundedRectPath(width, height, topLeft, topRight, bottomRight, bottomLeft); } if (_pancake.BackgroundGradientStops != null && _pancake.BackgroundGradientStops.Any()) { // A range of colors is given. Let's add them. var orderedStops = _pancake.BackgroundGradientStops.OrderBy(x => x.Offset).ToList(); var colors = orderedStops.Select(x => x.Color.ToAndroid().ToArgb()).ToArray(); var locations = orderedStops.Select(x => x.Offset).ToArray(); var shader = new LinearGradient((float)(canvas.Width * _pancake.BackgroundGradientStartPoint.X), (float)(canvas.Height * _pancake.BackgroundGradientStartPoint.Y), (float)(canvas.Width * _pancake.BackgroundGradientEndPoint.X), (float)(canvas.Height * _pancake.BackgroundGradientEndPoint.Y), colors, locations, Shader.TileMode.Clamp); paint.SetShader(shader); } else { global::Android.Graphics.Color color = _pancake.BackgroundColor.ToAndroid(); paint.SetStyle(style); paint.Color = color; } canvas.DrawPath(path, paint); } }
void DrawOutline(ACanvas canvas, Path path) { using (var paint = new Paint { AntiAlias = true }) using (Path.Direction direction = Path.Direction.Cw) using (Paint.Style style = Paint.Style.Stroke) { paint.StrokeWidth = 1; paint.SetStyle(style); paint.Color = _frame.OutlineColor.ToAndroid(); canvas.DrawPath(path, paint); } }
private void DrawBackground(ACanvas canvas, int width, int height, CornerRadius cornerRadius, bool pressed) { using (var paint = new Paint { AntiAlias = true }) using (var path = PolygonUitls.RegularPolygonPath(width, height, _polygonFrame.Sides, _polygonFrame.CornerRadius, 0, _polygonFrame.OffsetAngle)) using (Paint.Style style = Paint.Style.Fill) { global::Android.Graphics.Color color = _polygonFrame.BackgroundColor.ToAndroid(); paint.SetStyle(style); paint.Color = color; canvas.DrawPath(path, paint); } }
void DrawBackground(ACanvas canvas, Path path, bool pressed) { using (var paint = new Paint { AntiAlias = true }) using (Path.Direction direction = Path.Direction.Cw) using (Paint.Style style = Paint.Style.Fill) { global::Android.Graphics.Color color = _frame.InnerBackground.ToAndroid(); paint.SetStyle(style); paint.Color = color; canvas.DrawPath(path, paint); } }
void DrawBackground(ACanvas canvas, int width, int height, CornerRadius cornerRadius, bool pressed) { using (var paint = new Paint { AntiAlias = true }) using (var path = new Path()) using (Path.Direction direction = Path.Direction.Cw) using (Paint.Style style = Paint.Style.Fill) using (var rect = new RectF(0, 0, width, height)) { float topLeft = _convertToPixels(cornerRadius.TopLeft); float topRight = _convertToPixels(cornerRadius.TopRight); float bottomRight = _convertToPixels(cornerRadius.BottomRight); float bottomLeft = _convertToPixels(cornerRadius.BottomLeft); if (!_pancake.HasShadow) { path.AddRoundRect(rect, new float[] { topLeft, topLeft, topRight, topRight, bottomRight, bottomRight, bottomLeft, bottomLeft }, direction); } else { path.AddRoundRect(rect, new float[] { topLeft, topLeft, topLeft, topLeft, topLeft, topLeft, topLeft, topLeft }, direction); } if (_pancake.BackgroundGradientStartColor != default(Xamarin.Forms.Color) && _pancake.BackgroundGradientEndColor != default(Xamarin.Forms.Color)) { var angle = _pancake.BackgroundGradientAngle / 360.0; // Calculate the new positions based on angle between 0-360. var a = width * Math.Pow(Math.Sin(2 * Math.PI * ((angle + 0.75) / 2)), 2); var b = height * Math.Pow(Math.Sin(2 * Math.PI * ((angle + 0.0) / 2)), 2); var c = width * Math.Pow(Math.Sin(2 * Math.PI * ((angle + 0.25) / 2)), 2); var d = height * Math.Pow(Math.Sin(2 * Math.PI * ((angle + 0.5) / 2)), 2); var shader = new LinearGradient(width - (float)a, (float)b, width - (float)c, (float)d, _pancake.BackgroundGradientStartColor.ToAndroid(), _pancake.BackgroundGradientEndColor.ToAndroid(), Shader.TileMode.Clamp); paint.SetShader(shader); } else { global::Android.Graphics.Color color = _pancake.BackgroundColor.ToAndroid(); paint.SetStyle(style); paint.Color = color; } canvas.DrawPath(path, paint); } }
void DrawOutline(ACanvas canvas, int width, int height, float cornerRadius) { using (var paint = new Paint { AntiAlias = true }) using (var path = new Path()) using (Path.Direction direction = Path.Direction.Cw) using (Paint.Style style = Paint.Style.Stroke) using (var rect = new RectF(0, 0, width, height)) { float rx = Forms.Context.ToPixels(cornerRadius); float ry = Forms.Context.ToPixels(cornerRadius); path.AddRoundRect(rect, rx, ry, direction); paint.StrokeWidth = 2f; paint.SetStyle(style); paint.Color = Color.ParseColor("#4CAF50"); canvas.DrawPath(path, paint); } }
void DrawOutline(ACanvas canvas, int width, int height) { using (var paint = new Paint { AntiAlias = true }) using (var path = new Path()) using (Path.Direction direction = Path.Direction.Cw) using (Paint.Style style = Paint.Style.Stroke) using (var rect = new RectF(0, 0, width, height)) { float rx = Forms.Context.ToPixels(_frame.CornerRadius); float ry = Forms.Context.ToPixels(_frame.CornerRadius); path.AddRoundRect(rect, rx, ry, direction); paint.StrokeWidth = Forms.Context.ToPixels(_frame.BorderWidth); paint.SetStyle(style); paint.Color = _frame.BorderColor.ToAndroid(); canvas.DrawPath(path, paint); } }
void DrawOutline(ACanvas canvas, int width, int height, float cornerRadius) { using (var paint = new Paint { AntiAlias = true }) using (var path = new Path()) using (Path.Direction direction = Path.Direction.Cw) using (Paint.Style style = Paint.Style.Stroke) using (var rect = new RectF(0, 0, width, height)) { float rx = Forms.Context.ToPixels(cornerRadius); float ry = Forms.Context.ToPixels(cornerRadius); path.AddRoundRect(rect, rx, ry, direction); paint.StrokeWidth = element.OutlineWidth; //set outline stroke paint.SetStyle(style); paint.Color = element.OutlineColor.ToAndroid(); //paint.Color = Color.ParseColor("#A7AE22");//set outline color //_frame.OutlineColor.ToAndroid(); canvas.DrawPath(path, paint); } }
void DrawBackground(ACanvas canvas, int width, int height, bool pressed) { using (var paint = new Paint { AntiAlias = true }) using (var path = new Path()) using (Path.Direction direction = Path.Direction.Cw) using (Paint.Style style = Paint.Style.Fill) using (var rect = new RectF(0, 0, width, height)) { float rx = Forms.Context.ToPixels(_frame.CornerRadius); float ry = Forms.Context.ToPixels(_frame.CornerRadius); path.AddRoundRect(rect, rx, ry, direction); global::Android.Graphics.Color color = _frame.BackgroundColor.ToAndroid(); paint.SetStyle(style); paint.Color = color; canvas.DrawPath(path, paint); } }
private void DrawBorder(ACanvas canvas, PancakeView pancake) { if (pancake.Border != null && pancake.Border.Thickness != default) { var borderThickness = Context.ToPixels(pancake.Border.Thickness); var halfBorderThickness = borderThickness / 2; bool hasShadowOrElevation = pancake.Shadow != null && Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop; // TODO: This doesn't look entirely right yet when using it with rounded corners. using (var paint = new Paint { AntiAlias = true }) using (Path.Direction direction = Path.Direction.Cw) using (Paint.Style style = Paint.Style.Stroke) using (var rect = new RectF(pancake.Border.DrawingStyle == BorderDrawingStyle.Outside && !hasShadowOrElevation ? -halfBorderThickness : halfBorderThickness, pancake.Border.DrawingStyle == BorderDrawingStyle.Outside && !hasShadowOrElevation ? -halfBorderThickness : halfBorderThickness, pancake.Border.DrawingStyle == BorderDrawingStyle.Outside && !hasShadowOrElevation ? canvas.Width + halfBorderThickness : canvas.Width - halfBorderThickness, pancake.Border.DrawingStyle == BorderDrawingStyle.Outside && !hasShadowOrElevation ? canvas.Height + halfBorderThickness : canvas.Height - halfBorderThickness)) { Path path = null; if (pancake.Sides != 4) { path = DrawingExtensions.CreatePolygonPath(Width, Height, pancake.Sides, pancake.CornerRadius.TopLeft, pancake.OffsetAngle); } else { path = DrawingExtensions.CreateRoundedRectPath(Width, Height, Context.ToPixels(pancake.CornerRadius.TopLeft), Context.ToPixels(pancake.CornerRadius.TopRight), Context.ToPixels(pancake.CornerRadius.BottomRight), Context.ToPixels(pancake.CornerRadius.BottomLeft)); } if (pancake.Border.DashPattern.Pattern != null && pancake.Border.DashPattern.Pattern.Length > 0) { var items = pancake.Border.DashPattern.Pattern.Select(x => Context.ToPixels(Convert.ToSingle(x))).ToArray(); // dashes merge when thickness is increased // off-distance should be scaled according to thickness for (int i = 0; i < items.Count(); i++) { if (i % 2 != 0) { items[i] = items[i] * ((float)pancake.Border.Thickness * 0.5f); } } paint.SetPathEffect(new DashPathEffect(items, 0)); } if (pancake.Border.GradientStops != null && pancake.Border.GradientStops.Any()) { // A range of colors is given. Let's add them. var orderedStops = pancake.Border.GradientStops.OrderBy(x => x.Offset).ToList(); var colors = orderedStops.Select(x => x.Color.ToAndroid().ToArgb()).ToArray(); var locations = orderedStops.Select(x => x.Offset).ToArray(); var shader = new LinearGradient((float)(canvas.Width * pancake.Border.GradientStartPoint.X), (float)(canvas.Height * pancake.Border.GradientStartPoint.Y), (float)(canvas.Width * pancake.Border.GradientEndPoint.X), (float)(canvas.Height * pancake.Border.GradientEndPoint.Y), colors, locations, Shader.TileMode.Clamp); paint.SetShader(shader); } else { paint.Color = pancake.Border.Color.ToAndroid(); } paint.StrokeCap = Paint.Cap.Square; paint.StrokeWidth = borderThickness; paint.SetStyle(style); canvas.DrawPath(path, paint); } } }
void DrawBackground(ACanvas canvas, int width, int height, CornerRadius cornerRadius, bool pressed) { using (var paint = new Paint { AntiAlias = true }) using (Path.Direction direction = Path.Direction.Cw) using (Paint.Style style = Paint.Style.Fill) { var path = new Path(); if (_pancake.Sides != 4) { path = ShapeUtils.CreatePolygonPath(width, height, _pancake.Sides, _pancake.CornerRadius.TopLeft, _pancake.OffsetAngle); } else { float topLeft = _convertToPixels(cornerRadius.TopLeft); float topRight = _convertToPixels(cornerRadius.TopRight); float bottomRight = _convertToPixels(cornerRadius.BottomRight); float bottomLeft = _convertToPixels(cornerRadius.BottomLeft); path = ShapeUtils.CreateRoundedRectPath(width, height, topLeft, topRight, bottomRight, bottomLeft); } if ((_pancake.BackgroundGradientStartColor != default(Xamarin.Forms.Color) && _pancake.BackgroundGradientEndColor != default(Xamarin.Forms.Color)) || (_pancake.BackgroundGradientStops != null && _pancake.BackgroundGradientStops.Any())) { var angle = _pancake.BackgroundGradientAngle / 360.0; // Calculate the new positions based on angle between 0-360. var a = width * Math.Pow(Math.Sin(2 * Math.PI * ((angle + 0.75) / 2)), 2); var b = height * Math.Pow(Math.Sin(2 * Math.PI * ((angle + 0.0) / 2)), 2); var c = width * Math.Pow(Math.Sin(2 * Math.PI * ((angle + 0.25) / 2)), 2); var d = height * Math.Pow(Math.Sin(2 * Math.PI * ((angle + 0.5) / 2)), 2); if (_pancake.BackgroundGradientStops != null && _pancake.BackgroundGradientStops.Count > 0) { // A range of colors is given. Let's add them. var orderedStops = _pancake.BackgroundGradientStops.OrderBy(x => x.Offset).ToList(); var colors = orderedStops.Select(x => x.Color.ToAndroid().ToArgb()).ToArray(); var locations = orderedStops.Select(x => x.Offset).ToArray(); var shader = new LinearGradient(width - (float)a, (float)b, width - (float)c, (float)d, colors, locations, Shader.TileMode.Clamp); paint.SetShader(shader); } else { // Only two colors provided, use that. var shader = new LinearGradient(width - (float)a, (float)b, width - (float)c, (float)d, _pancake.BackgroundGradientStartColor.ToAndroid(), _pancake.BackgroundGradientEndColor.ToAndroid(), Shader.TileMode.Clamp); paint.SetShader(shader); } } else { global::Android.Graphics.Color color = _pancake.BackgroundColor.ToAndroid(); paint.SetStyle(style); paint.Color = color; } canvas.DrawPath(path, paint); } }
private void DrawBorder(ACanvas canvas, PancakeView control) { var borderThickness = Context.ToPixels(control.BorderThickness); var halfBorderThickness = borderThickness / 2; bool hasShadowOrElevation = control.HasShadow || control.Elevation > 0; // TODO: This doesn't look entirely right yet when using it with rounded corners. using (var paint = new Paint { AntiAlias = true }) using (var path = new Path()) using (Path.Direction direction = Path.Direction.Cw) using (Paint.Style style = Paint.Style.Stroke) using (var rect = new RectF(control.BorderDrawingStyle == BorderDrawingStyle.Outside && !hasShadowOrElevation ? -halfBorderThickness : halfBorderThickness, control.BorderDrawingStyle == BorderDrawingStyle.Outside && !hasShadowOrElevation ? -halfBorderThickness : halfBorderThickness, control.BorderDrawingStyle == BorderDrawingStyle.Outside && !hasShadowOrElevation ? canvas.Width + halfBorderThickness : canvas.Width - halfBorderThickness, control.BorderDrawingStyle == BorderDrawingStyle.Outside && !hasShadowOrElevation ? canvas.Height + halfBorderThickness : canvas.Height - halfBorderThickness)) { path.AddRoundRect(rect, GetRadii(control), direction); if (control.BorderIsDashed) { paint.SetPathEffect(new DashPathEffect(new float[] { 10, 20 }, 0)); } if ((control.BorderGradientStartColor != default(Color) && control.BorderGradientEndColor != default(Color)) || (control.BorderGradientStops != null && control.BorderGradientStops.Any())) { var angle = control.BorderGradientAngle / 360.0; // Calculate the new positions based on angle between 0-360. var a = canvas.Width * Math.Pow(Math.Sin(2 * Math.PI * ((angle + 0.75) / 2)), 2); var b = canvas.Height * Math.Pow(Math.Sin(2 * Math.PI * ((angle + 0.0) / 2)), 2); var c = canvas.Width * Math.Pow(Math.Sin(2 * Math.PI * ((angle + 0.25) / 2)), 2); var d = canvas.Height * Math.Pow(Math.Sin(2 * Math.PI * ((angle + 0.5) / 2)), 2); if (control.BorderGradientStops != null) { // A range of colors is given. Let's add them. var orderedStops = control.BorderGradientStops.OrderBy(x => x.Offset).ToList(); var colors = orderedStops.Select(x => x.Color.ToAndroid().ToArgb()).ToArray(); var locations = orderedStops.Select(x => x.Offset).ToArray(); var shader = new LinearGradient(canvas.Width - (float)a, (float)b, canvas.Width - (float)c, (float)d, colors, locations, Shader.TileMode.Clamp); paint.SetShader(shader); } else { // Only two colors provided, use that. var shader = new LinearGradient(canvas.Width - (float)a, (float)b, canvas.Width - (float)c, (float)d, control.BorderGradientStartColor.ToAndroid(), control.BorderGradientEndColor.ToAndroid(), Shader.TileMode.Clamp); paint.SetShader(shader); } } else { paint.Color = control.BorderColor.ToAndroid(); } paint.StrokeCap = Paint.Cap.Square; paint.StrokeWidth = borderThickness; paint.SetStyle(style); canvas.DrawPath(path, paint); } }
private void DrawBorder(ACanvas canvas, PancakeView control) { if (control.BorderThickness > 0) { var borderThickness = Context.ToPixels(control.BorderThickness); var halfBorderThickness = borderThickness / 2; bool hasShadowOrElevation = control.HasShadow || (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop && control.Elevation > 0); // TODO: This doesn't look entirely right yet when using it with rounded corners. using (var paint = new Paint { AntiAlias = true }) using (Path.Direction direction = Path.Direction.Cw) using (Paint.Style style = Paint.Style.Stroke) using (var rect = new RectF(control.BorderDrawingStyle == BorderDrawingStyle.Outside && !hasShadowOrElevation ? -halfBorderThickness : halfBorderThickness, control.BorderDrawingStyle == BorderDrawingStyle.Outside && !hasShadowOrElevation ? -halfBorderThickness : halfBorderThickness, control.BorderDrawingStyle == BorderDrawingStyle.Outside && !hasShadowOrElevation ? canvas.Width + halfBorderThickness : canvas.Width - halfBorderThickness, control.BorderDrawingStyle == BorderDrawingStyle.Outside && !hasShadowOrElevation ? canvas.Height + halfBorderThickness : canvas.Height - halfBorderThickness)) { Path path = null; if (control.Sides != 4) { path = ShapeUtils.CreatePolygonPath(Width, Height, control.Sides, control.CornerRadius.TopLeft, control.OffsetAngle); } else { path = ShapeUtils.CreateRoundedRectPath(Width, Height, Context.ToPixels(control.CornerRadius.TopLeft), Context.ToPixels(control.CornerRadius.TopRight), Context.ToPixels(control.CornerRadius.BottomRight), Context.ToPixels(control.CornerRadius.BottomLeft)); } if (control.BorderIsDashed) { // dashes merge when thickness is increased // off-distance should be scaled according to thickness paint.SetPathEffect(new DashPathEffect(new float[] { 10, 5 * control.BorderThickness }, 0)); } if ((control.BorderGradientStartColor != default(Color) && control.BorderGradientEndColor != default(Color)) || (control.BorderGradientStops != null && control.BorderGradientStops.Any())) { var angle = control.BorderGradientAngle / 360.0; // Calculate the new positions based on angle between 0-360. var a = canvas.Width * Math.Pow(Math.Sin(2 * Math.PI * ((angle + 0.75) / 2)), 2); var b = canvas.Height * Math.Pow(Math.Sin(2 * Math.PI * ((angle + 0.0) / 2)), 2); var c = canvas.Width * Math.Pow(Math.Sin(2 * Math.PI * ((angle + 0.25) / 2)), 2); var d = canvas.Height * Math.Pow(Math.Sin(2 * Math.PI * ((angle + 0.5) / 2)), 2); if (control.BorderGradientStops != null && control.BorderGradientStops.Count > 0) { // A range of colors is given. Let's add them. var orderedStops = control.BorderGradientStops.OrderBy(x => x.Offset).ToList(); var colors = orderedStops.Select(x => x.Color.ToAndroid().ToArgb()).ToArray(); var locations = orderedStops.Select(x => x.Offset).ToArray(); var shader = new LinearGradient(canvas.Width - (float)a, (float)b, canvas.Width - (float)c, (float)d, colors, locations, Shader.TileMode.Clamp); paint.SetShader(shader); } else { // Only two colors provided, use that. var shader = new LinearGradient(canvas.Width - (float)a, (float)b, canvas.Width - (float)c, (float)d, control.BorderGradientStartColor.ToAndroid(), control.BorderGradientEndColor.ToAndroid(), Shader.TileMode.Clamp); paint.SetShader(shader); } } else { paint.Color = control.BorderColor.ToAndroid(); } paint.StrokeCap = Paint.Cap.Square; paint.StrokeWidth = borderThickness; paint.SetStyle(style); canvas.DrawPath(path, paint); } } }