如何将像素转换为 xamarin.forms 单位? [英] how to transform a pixel to xamarin.forms units?

查看:47
本文介绍了如何将像素转换为 xamarin.forms 单位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 每英寸有 160 个单位.2.如果我创建了一个 72 dpiPhotoshop 文件,那么每英寸将有 72 个点.3.如果Photoshop中的元素是88px高度,那么我必须在xamarin中设置什么?

如果手机是 360dpi,那么 xamarin 的高度应该是:88/72 * 160/2?但这是不对的.

解决方案

我知道每个平台的单位,我只想知道如何从像素中获取单位

您可以使用

现在我将打印MyBoxView.Width.结果是:

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

如果您使用 var density = mainDisplayInfo.Density; 来获取密度,您将得到 Screen Density::2.625.(我的设备是 Piexl_2_pie_9_0 api 28 模拟器)

您知道屏幕宽度的大小是 1080 像素,但是宽度是 411.428571428571.这应该意味着 WidthRequest 的单位.

如果你输入 411.428571428571 * 2.625 ,你会得到 1080 像素.

  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.

解决方案

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:

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.

这篇关于如何将像素转换为 xamarin.forms 单位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆