Monodroid DensityDpi to Int

时间:2014-08-25 13:41:05

标签: c# xamarin xamarin.android

我需要将DensityDpi转换为Int。我试过这样:

int dens = dm.DensityDpi;

并且诅咒它不起作用。完整的代码是:

DisplayMetrics dm = new DisplayMetrics();
WindowManager.DefaultDisplay.GetMetrics(dm);
int width = dm.WidthPixels;
int height = dm.HeightPixels;
int dens = dm.DensityDpi;
double wi = (double)width / (double)dens;
double hi = (double)height/(double)dens;
double x = Math.Pow(wi,2);
double y = Math.Pow(hi,2);
double screenInches = Math.Sqrt(x+y);

我试图找到屏幕的一寸。

考虑到表明常量是int,我不知道为什么它不起作用。 (http://developer.android.com/reference/android/util/DisplayMetrics.html) 我认为这些都是一样的。

2 个答案:

答案 0 :(得分:0)

对于Android:

var metrics = new DisplayMetrics();
this.WindowManager.DefaultDisplay.GetMetrics(metrics);

private const float INCH = 25.4f;

var ydpi = metrics.Ydpi; // dpi in vertical direction
var xdpi = metric.Xdpi; // dpi in horizontal direction
var pHeight = metrics.HeightPixels; // total pixels in vertical direction
var pWidth = metrics.WidthPixels; // total pixels in horizontal direction
var iHeight = (pHeight / ydpi); // screenheight in inches
var iWidth = (pWidth / xdpi); // screenwidth in inches
var mHeight = (pHeight / ydpi) * INCH; // screenheight in millimeters
var mWidth = (pWidth / xdpi) * INCH; // screenwidth in millimeters

Afaik没有办法直接向iOS设备查询它的DPI。

然而,您可以使用一些常量来计算它。 iPhone的亮度为163 ppi,iPhone Retina的亮度是其两倍。

private const float INCH = 25.4f;

var ppi = 163 * UIScreen.MainScreen.Scale; // pixels per inch for an iPhone or iPhone Retina
var ppmm = ppi / INCH;                // pixels per millimeter
var pHeight = UIScreen.MainScreen.ApplicationFrame.Height; // screen height in pixels
var pWidth =  UIScreen.MainScreen.ApplicationFrame.Width; // screen width in pixels
var iHeight =  (pHeight / ppi); // screen height in millimeters
var iWidth =   (pWidth / ppi); // screen height in millimeters
var mHeight = (pHeight / ppi) * INCH; // screen height in millimeters
var mWidth =  (pWidth / ppi) * INCH; // screen height in millimeters

答案 1 :(得分:0)

我在Xamarin.Forms.Labs项目中对显示属性实现了抽象。虽然它的目标是Xamarin.Forms,但它也可以在没有它的情况下使用。对于Android和WP8,会计算属性,但对于iOS,它们由设备的硬件版本决定。您可以从NuGet(Xamarin.Forms.Labs)为每个平台(iOS,Android,WP8)安装软件包,以获得功能齐全,经过测试且易于使用的类。

以下是Android显示的完整源代码的链接:

https://github.com/XForms/Xamarin-Forms-Labs/blob/master/src/Xamarin.Forms.Labs/Xamarin.Forms.Labs.Droid/Device/Display.cs

    public Display()
    {
        var dm = Metrics;
        this.Height = dm.HeightPixels;
        this.Width = dm.WidthPixels;
        this.Xdpi = dm.Xdpi;
        this.Ydpi = dm.Ydpi;

        this.FontManager = new FontManager(this);
    }

    public static Android.Util.DisplayMetrics Metrics
    {
        get
        {
            return Application.Context.Resources.DisplayMetrics;
        }
    }

根据显示密度,使用扩展方法计算物理大小(因此可以由所有平台重用):

https://github.com/XForms/Xamarin-Forms-Labs/blob/master/src/Xamarin.Forms.Labs/Xamarin.Forms.Labs/Device/DisplayExtensions.cs

    public static double ScreenSizeInches(this IDisplay screen)
    {
        return Math.Sqrt(Math.Pow(screen.ScreenWidthInches(), 2) + Math.Pow(screen.ScreenHeightInches(), 2));
    }

    public static double ScreenWidthInches(this IDisplay screen)
    {
        return screen.Width / screen.Xdpi;
    }

    public static double ScreenHeightInches(this IDisplay screen)
    {
        return screen.Height / screen.Ydpi;
    }

以下是Android信息的屏幕截图:

enter image description here

也适用于iOS和& WP8:

enter image description here enter image description here