1
  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.

maui
  • 23
  • 5
  • where do you get /2 from? 88 / 72 * 160 = 196 pixels – Jason Dec 01 '20 at 01:32
  • 196 should be units? I want to get units in Xamarin by pixels from Photoshop – maui Dec 01 '20 at 02:08
  • @Jason I just want to get units in Xamarin by pixels from Photoshop. – maui Dec 01 '20 at 02:09
  • @maui Maybe this is helpful for you to understand the units in Xamarin Forms.https://stackoverflow.com/questions/29808498/xamarin-forms-widthrequest-value-meaning – Junior Jiang Dec 01 '20 at 02:59
  • @JuniorJiang-MSFT I know the units in each platfform and I only want to know how to get units from pixels – maui Dec 01 '20 at 05:00

1 Answers1

1

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.

Junior Jiang
  • 12,430
  • 1
  • 10
  • 30
  • no it not right. I mean If I have a photoshop design and all in it is pixel and how to get units from it? for example the button is 88px height. how to set the heightrequest in xamarin? – maui Dec 02 '20 at 00:45
  • @maui What do you mean "how to get units from it"? If it is pixel, you can get piexl from it. – Junior Jiang Dec 02 '20 at 01:23
  • @maui I have updated the answer, you can have a look when you have time. – Junior Jiang Dec 02 '20 at 01:41
  • I mean If the boxView is 88px then I should set it's widthRequest according to different device?in different device android or ios one unit is not the same? – maui Dec 02 '20 at 01:59
  • But as I know in one inch there are 160 units.and In one inch there are 72 pixels right? in 72dpi? so it means 1units = 160 /72? – maui Dec 02 '20 at 01:59
  • @maui Okey, one inch there are 160 units that means **160dpi** . It's the units of device screen. You don't care about this. If the BoxView is 88px, you need to calculate the units on runtime by using `Pixels/ScreenDensity`. – Junior Jiang Dec 02 '20 at 02:08
  • @maui However, we can calculate the `ScreenDensity` by the `dpi` of device screen as my answer shared. Such as: `Actual-DPI/160`. I'm not sure whether it works for iOS/UWP device, but it works for Android device. Here `DPI` is not the best important. Because we can use **Xamarin.Essentials** to get the `ScreenDensity` directly. – Junior Jiang Dec 02 '20 at 02:32
  • If so I have to calculate it in every different device but it is not the Xamarin units. unit is to separate it from calculation in different device.you can use units in all device right?you do not need to care the dpi or others just use units is ok? – maui Dec 02 '20 at 02:49
  • @maui Generally, we only set a default `HeightRequest` with a specified units value. It will be converted to diferent pixels in different devices. If you not minding this effect, you can use this. Else if you want to every device seems to be the same effect, we can call that show proportionally. Then you need to calcualte the units on runtime. It depends on you. In addition, the units of `HeightRequest` is designed by Xamarin Forms, we can not modify it. When you setting `HeightRequest` for `BoxView`, it also use the units not the pixels. – Junior Jiang Dec 02 '20 at 03:01
  • But If I want to Set HeightRequest for BoxView,how many units I should set? in design it is 88 pixels in 72dpi. HeightReuqust = ? – maui Dec 02 '20 at 05:07
  • @maui If so, use *72/160* is the `ScreenDensity`, and the units should be `88/ScreenDensity`. But you should know that the UI design of mobile application should follow the rationality of the mobile interface. If you follow other design of platform to convert it, it will not show properly. Usually we design the UI of the mobile application first, and then determine what kind of pixel image to use. – Junior Jiang Dec 02 '20 at 05:48
  • last question is all android width 480 units? – maui Dec 03 '20 at 01:09
  • @maui If you mean the `480 units` is `480 dpi`, because this units is for screen of device. The answer is **No**, the almost of the recent published android phones is `480dpi`, the older android phone may have other dpis,such as `360dpi/240dpi`,etc. – Junior Jiang Dec 03 '20 at 01:42
  • @maui You could have a look at [android document](https://developer.android.com/training/multiscreen/screendensities) to research it. And here is [a helpful disscussion](https://stackoverflow.com/questions/2025282/what-is-the-difference-between-px-dip-dp-and-sp) for it in SO. – Junior Jiang Dec 03 '20 at 01:45