Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
338 views
in Technique[技术] by (71.8m points)

how to transform a pixel to xamarin.forms units?

  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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...