Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

  1. there are 160 units per inch. 2.If I created an Photoshop file that are 72 dpi then there will be 72 points per inch. 3.If the element is 88px height in Photoshop then what I have to set it in xamarin?

If the phone is 360dpi then the height in xamarin should be :88 / 72 * 160 / 2? but it is not right.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
331 views
Welcome To Ask or Share your Answers For Others

1 Answer

I know the units in each platfform and I only want to know how to get units from pixels

You could use Xamarin.Essentials to get the Screen density as follows:

// Get Metrics
var mainDisplayInfo = DeviceDisplay.MainDisplayInfo;
// Screen density
var density = mainDisplayInfo.Density;

If the density is 3 in iOS device and pixels is 88 , then there are 88/3 units in iOS.

If the phone is 360dpi

That means it shoule be a Android device, and screen density also can be calculated by 360/160 = 2.25. Then there are 88/2.25 units to set for HeightRequest.

================================Update==============================

If there is a BoxView in Xaml as follows:

<BoxView x:Name="MyBoxView" BackgroundColor="CadetBlue" HorizontalOptions="Fill"/>

And the effect:

enter image description here

Now I will print the MyBoxView.Width. the result is:

Console.WriteLine("++++MyBoxView++++" + MyBoxView.Width);

++++MyBoxView++++411.428571428571

If you use var density = mainDisplayInfo.Density; to get density, you will get Screen Density::2.625. (My device is Piexl_2_pie_9_0 api 28 emulator)

You know the size of screen width is 1080 pixels, however the width is 411.428571428571. That should means units of WidthRequest.

And if you put 411.428571428571 * 2.625 , you will get 1080 pixels.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...