From a795dcbd1f1f2b3935c387affc4a48edefa0c831 Mon Sep 17 00:00:00 2001 From: Matthew Leibowitz Date: Sat, 4 Feb 2017 14:08:38 -0600 Subject: [PATCH] Improved the blur sample --- .../Samples/BlurMaskFilterSample.cs | 52 ++++++++++++++++++- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/samples/SkiaSharpSample.Shared/Samples/BlurMaskFilterSample.cs b/samples/SkiaSharpSample.Shared/Samples/BlurMaskFilterSample.cs index 71c1682ad..12d16908e 100644 --- a/samples/SkiaSharpSample.Shared/Samples/BlurMaskFilterSample.cs +++ b/samples/SkiaSharpSample.Shared/Samples/BlurMaskFilterSample.cs @@ -19,15 +19,63 @@ namespace SkiaSharpSample.Samples canvas.DrawColor(SKColors.White); using (var paint = new SKPaint()) - using (var filter = SKMaskFilter.CreateBlur(SKBlurStyle.Normal, 5.0f)) + using (var filter = SKMaskFilter.CreateBlur(SKBlurStyle.Normal, 5.0f, SKBlurMaskFilterFlags.HighQuality)) { paint.IsAntialias = true; paint.TextSize = 120; paint.TextAlign = SKTextAlign.Center; paint.MaskFilter = filter; - canvas.DrawText("SkiaSharp", width / 2f, height / 2f, paint); + canvas.DrawText("SkiaSharp", width / 2f, height / 4f, paint); } + + // scale up so we don't have to change the values in the method + var scaling = 3; + canvas.Scale(scaling, scaling); + var rect = SKRect.Create(width / (3f * scaling), height / (2f * scaling), width / (3f * scaling), height / (4f * scaling)); + + DrawInnerBlurRectangle(canvas, rect); + } + + private void DrawInnerBlurRectangle(SKCanvas canvas, SKRect rect) + { + // create the rounded rectangle + var roundedRect = new SKPath(); + roundedRect.AddRoundedRect(rect, 10, 10); + + // draw the white background + var p = new SKPaint + { + IsAntialias = true, + Style = SKPaintStyle.Fill, + Color = SKColors.White + }; + canvas.DrawPath(roundedRect, p); + + using (new SKAutoCanvasRestore(canvas)) + { + // clip the canvas to stop the blur from appearing outside + canvas.ClipPath(roundedRect, SKClipOperation.Intersect, true); + + // draw the wide blur all around + p.Color = SKColors.Black; + p.Style = SKPaintStyle.Stroke; + p.StrokeWidth = 2; + p.MaskFilter = SKMaskFilter.CreateBlur(SKBlurStyle.Normal, 2, SKBlurMaskFilterFlags.HighQuality); + canvas.Translate(0.5f, 1.5f); + canvas.DrawPath(roundedRect, p); + + // draw the narrow blur at the top + p.StrokeWidth = 1; + p.MaskFilter = SKMaskFilter.CreateBlur(SKBlurStyle.Normal, 1, SKBlurMaskFilterFlags.HighQuality); + canvas.DrawPath(roundedRect, p); + } + + // draw the border + p.StrokeWidth = 2; + p.MaskFilter = null; + p.Color = SampleMedia.Colors.XamarinGreen; + canvas.DrawPath(roundedRect, p); } } }