Convert Xamarin.Forms measure units to pixels

Viewed 731

How can I convert, for example, view.WidthRequest value to platform pixels? I'm looking for method like Device.ConvertToPixels(10).

I want to use it for SkiaSharp drawing. For example I want to draw circle with stroke = 10 in (xamarin.forms units) which will be converted to pixels on draw.

3 Answers

You can also do a scale at the top of the draw and then use whatever units you like.

For example, on a 2x display, you can do a canvas.Scale(2) and then draw as if it was a 1x.

In the case of a Xamarin.Forms paint event, there is the event args that has all the info you need:

canvas.Scale(e.Info.Width / view.Width);

Also, you can have a look at this blog post where I show off some things: https://dotnetdevaddict.co.za/2020/01/12/who-cares-about-the-view-anyway/

Related