private void DrawBorder(SKCanvas canvas) { var pancake = Element as PancakeView; if (pancake.Border != null && pancake.Border.Thickness != default) { using (var paint = new SKPaint()) { var border = pancake.Border; paint.Style = SKPaintStyle.Stroke; paint.Color = border.Color.ToNative().ToSKColor(); paint.StrokeWidth = Forms.ConvertToScaledPixel(border.Thickness); paint.IsAntialias = true; SKPath path; if (pancake.Sides != 4) { path = DrawingExtensions.CreatePolygonPath(Control.Geometry.Width, Control.Geometry.Height, pancake.Sides, pancake.CornerRadius.TopLeft, pancake.OffsetAngle); } else { var left = Control.Geometry.Left - _skCanvasView.Geometry.Left; var top = Control.Geometry.Top - _skCanvasView.Geometry.Top; path = DrawingExtensions.CreateRoundedRectPath(left, top, left + Control.Geometry.Width, top + Control.Geometry.Height, pancake.CornerRadius); } if (border.DashPattern.Pattern != null && border.DashPattern.Pattern.Length > 0 && (border.DashPattern.Pattern.Length % 2 == 0 || border.DashPattern.Pattern.Length == 1)) { var items = border.DashPattern.Pattern.Select(x => Convert.ToSingle(x)).ToList(); if (items.Count == 1) { items.Add(items[0]); } paint.PathEffect = SKPathEffect.CreateDash(items.ToArray(), 0); } if (border.GradientStops != null && border.GradientStops.Any()) { var startPoint = new SKPoint((float)(border.GradientStartPoint.X * Control.Geometry.Width), (float)(border.GradientStartPoint.Y * Control.Geometry.Height)); var endPoint = new SKPoint((float)(border.GradientEndPoint.X * Control.Geometry.Width), (float)(border.GradientEndPoint.Y * Control.Geometry.Height)); var orderedStops = border.GradientStops.OrderBy(x => x.Offset).ToList(); var gradientColors = orderedStops.Select(x => x.Color.ToNative().ToSKColor()).ToArray(); var gradientColorPos = orderedStops.Select(x => x.Offset).ToArray(); paint.Shader = SKShader.CreateLinearGradient(startPoint, endPoint, gradientColors, gradientColorPos, SKShaderTileMode.Clamp); } canvas.ClipPath(path, SKClipOperation.Intersect, true); canvas.DrawPath(path, paint); } } }
private void DrawShadow(SKCanvas canvas) { var pancake = Element as PancakeView; if (pancake.Shadow != null) { SKPath path; if (pancake.Sides != 4) { path = DrawingExtensions.CreatePolygonPath(Control.Geometry.Width, Control.Geometry.Height, pancake.Sides, pancake.CornerRadius.TopLeft, pancake.OffsetAngle); } else { var left = Control.Geometry.Left - _skCanvasView.Geometry.Left; var top = Control.Geometry.Top - _skCanvasView.Geometry.Top; path = DrawingExtensions.CreateRoundedRectPath(left, top, left + Control.Geometry.Width, top + Control.Geometry.Height, pancake.CornerRadius); } using (var paint = new SKPaint()) { paint.IsAntialias = true; paint.Style = SKPaintStyle.StrokeAndFill; var shadow = pancake.Shadow; var scaledOffsetX = Forms.ConvertToScaledPixel(shadow.Offset.X); var scaledOffsetY = Forms.ConvertToScaledPixel(shadow.Offset.Y); var scaledBlurRadius = Forms.ConvertToScaledPixel(shadow.BlurRadius); canvas.Save(); canvas.ClipPath(path, SKClipOperation.Difference, true); paint.ImageFilter = SKImageFilter.CreateDropShadow( scaledOffsetX, scaledOffsetY, scaledBlurRadius, scaledBlurRadius, shadow.Color.MultiplyAlpha(shadow.Opacity).ToNative().ToSKColor(), SKDropShadowImageFilterShadowMode.DrawShadowOnly); canvas.DrawPath(path, paint); canvas.Restore(); canvas.Save(); canvas.ClipPath(path, SKClipOperation.Intersect, true); canvas.DrawPath(path, paint); canvas.Restore(); } } }
private void DrawBackground(SKCanvas canvas) { using (var paint = new SKPaint()) { paint.Style = SKPaintStyle.Fill; paint.IsAntialias = true; var pancake = Element as PancakeView; SKPath path; if (pancake.Sides != 4) { path = DrawingExtensions.CreatePolygonPath(Control.Geometry.Width, Control.Geometry.Height, pancake.Sides, pancake.CornerRadius.TopLeft, pancake.OffsetAngle); } else { var left = Control.Geometry.Left - _skCanvasView.Geometry.Left; var top = Control.Geometry.Top - _skCanvasView.Geometry.Top; path = DrawingExtensions.CreateRoundedRectPath(left, top, left + Control.Geometry.Width, top + Control.Geometry.Height, pancake.CornerRadius); } if (pancake.BackgroundGradientStops != null && pancake.BackgroundGradientStops.Any()) { var orderedStops = pancake.BackgroundGradientStops.OrderBy(x => x.Offset).ToList(); var gradientColors = orderedStops.Select(x => x.Color.ToNative().ToSKColor()).ToArray(); var gradientColorPos = orderedStops.Select(x => x.Offset).ToArray(); var startPoint = new SKPoint((float)(pancake.BackgroundGradientStartPoint.X * Control.Geometry.Width), (float)(pancake.BackgroundGradientStartPoint.Y * Control.Geometry.Height)); var endPoint = new SKPoint((float)(pancake.BackgroundGradientEndPoint.X * Control.Geometry.Width), (float)(pancake.BackgroundGradientEndPoint.Y * Control.Geometry.Height)); paint.Shader = SKShader.CreateLinearGradient(startPoint, endPoint, gradientColors, gradientColorPos, SKShaderTileMode.Clamp); } else { paint.Color = pancake.BackgroundColor.ToNative().ToSKColor(); } canvas.ClipPath(path, SKClipOperation.Intersect, true); canvas.DrawPath(path, paint); } }