2D Graphics Rendering in Xamarin.Forms with SkiaSharp

21
2D Graphics Rendering in Xamarin.Forms with SkiaSharp - Udara Alwis -

Transcript of 2D Graphics Rendering in Xamarin.Forms with SkiaSharp

Page 1: 2D Graphics Rendering in Xamarin.Forms with SkiaSharp

2D Graphics Rendering in Xamarin.Forms with

SkiaSharp

- Udara Alwis -

Page 2: 2D Graphics Rendering in Xamarin.Forms with SkiaSharp

I am Udara Alwis

• Xamarin Developer / Enthusiast

• Mad Love for Mobile Development / UX

• Microsoft Dot Net FTW!

• 24 Years of Awesomeness…

• Nature, Adventures, Running, Skateboarding, Cycling, Parkour

• Occasional Blogger and Youtube-er?!

linkedin.com/in/UdaraAlwis

Page 3: 2D Graphics Rendering in Xamarin.Forms with SkiaSharp

Graphics in Mobile Applications

- Apps just built with default Buttons, Labels, Layouts… could

become boring!

- Graphics are important!

- Interactive and Delightful user experience!

- Super fun to play around as a Developer! (conditional. lol)

Page 4: 2D Graphics Rendering in Xamarin.Forms with SkiaSharp

How to Graphics?

1. Immediate Mode Graphics Rendering- On demand rendering, handle drawing manually, 2D

stuff, vector graphics!

2. Retain Mode Graphic Rendering- Persistent rendering, Graphical Objects, Self maintained

& managed objects, cool 3D stuff.

Page 5: 2D Graphics Rendering in Xamarin.Forms with SkiaSharp

Graphics Rendering in Xamarin.Forms?

- CocosSharp (2D – 2D games)

- SkiaSharp (2D – graphics rendering)

- UrhoSharp (3D – pure 3D gaming engine)

Page 6: 2D Graphics Rendering in Xamarin.Forms with SkiaSharp

behold, SkiaSharp

- Origins: Project Skia / Google Open Sourced / C++

- Immediate mode 2D vector graphics system

- 2D graphics, bitmaps, and text

- A lot of cool stuff!

- C# and DotNet wrapper of Skia: SkiaSharp

- Github.com/mono/SkiaSharp

Page 7: 2D Graphics Rendering in Xamarin.Forms with SkiaSharp

SkiaSharp basics…

- SKCanvasView, derives from Xamarin.Forms.View

- PaintSurfaceEvent, executes all the 2D drawing functionality

- InvalidateSurface() for repainting

- Draw Bitmap or SVGs

Page 8: 2D Graphics Rendering in Xamarin.Forms with SkiaSharp

SkiaSharp basics…

- Demo

- Install SkiaSharp

- Add SKCanvasView

- PaintSurface Event Handling

- Initialize SKCanvas

Page 9: 2D Graphics Rendering in Xamarin.Forms with SkiaSharp

SkiaSharp 2D Graphics Programming

- Just a thought…

- Mathematics is the limit in any kind of Graphics Rendering!

- No need of an advanced math degree, but a solid Math

knowledge surely helps a lot….

Page 10: 2D Graphics Rendering in Xamarin.Forms with SkiaSharp

SkiaSharp 2D Graphics Programming

- SKCanvasView, hosts the paint surface

- SKCanvas, the drawing surface that perform the actual

drawing

- Drawing methods, DrawCircle() DrawRect() DrawLine()

DrawText() DrawArc()… etc.

Page 11: 2D Graphics Rendering in Xamarin.Forms with SkiaSharp

SkiaSharp 2D Graphics Programming

- Transform operations such as Translate, Scale, Rotate, Skew

- SKPaint object to specify color and other drawing

attributes. Lightweight!

- Style (Stroke / Fill / StrokeAndFill)

- Color

- StrokeWidth

- IsAntialias, etc…

Page 12: 2D Graphics Rendering in Xamarin.Forms with SkiaSharp

- Demo

- Canvas Width / Height

- SKPaint Objects

- Drawing Circle / Stroke Line / Eclipse / Arc / Text

- Translations and Scaling

SkiaSharp 2D Graphics Programming

Page 13: 2D Graphics Rendering in Xamarin.Forms with SkiaSharp

SkiaSharp 2D Graphics Programming

- User Interactions

- SKCavnasView touch handling event

- Prefer to use a secondary touch handler

- handle touch operations and push updates back to

SKCanvas on demand

Page 14: 2D Graphics Rendering in Xamarin.Forms with SkiaSharp

SkiaSharp 2D Graphics Programming

- Demo

- User Interactions

Page 15: 2D Graphics Rendering in Xamarin.Forms with SkiaSharp

SkiaSharp 2D Graphics Programming

- Bitmap Image handling

- Loading images as usual Xamarin.Forms manner, native

resource or embedded resources

- Decode to SKBitmap for drawing

- DrawBitmap() method

- ImageFilers, built-in, oh yeah!

Page 16: 2D Graphics Rendering in Xamarin.Forms with SkiaSharp

SkiaSharp 2D Graphics Programming

- Demo

- Bitmap Image Handling

Page 17: 2D Graphics Rendering in Xamarin.Forms with SkiaSharp

SkiaSharp 2D Graphics Programming

- Animations Rendering

- Not so straight forward!

- Handle manually.

- Draw every single frame manually.

Page 18: 2D Graphics Rendering in Xamarin.Forms with SkiaSharp

SkiaSharp 2D Graphics Programming

- Demo

- Animations Rendering

Page 19: 2D Graphics Rendering in Xamarin.Forms with SkiaSharp

SkiaSharp 2D Graphics Programming

- More awesome stuff at,

- Github samples repo: SkiaSharpFormsDemos

Page 20: 2D Graphics Rendering in Xamarin.Forms with SkiaSharp

…well that’s it!

Now go build something cool with

awesome Xamarin with SkiaSharp!

Good luck and enjoy!

Page 21: 2D Graphics Rendering in Xamarin.Forms with SkiaSharp

Thank You!

For Weekly Xamarin Articles check out my blog, https://theconfuzedsourcecode.wordpress.com

-Udara Alwis

CODENAME: [ÇøŋfuzëÐ SøurcëÇødë]