Screen Density in iOS and Universal WIndows

Hey. I need to know the screen density in windows 10/8 / 8.1 and iOS. I got screen density in Android using DisplayMetrics, but I didn't find such an option / property in UWP and iOS. So, is there any property thanks to which I can get screen density in UWP and IOS.

+5
source share
2 answers

Below I store specific OS screen data in static vars located in the Xamarin Forms App class

They all work in my testing.

(copy / paste from my github page - https://github.com/mattregul/Xamarin_Screensize )

Android (MainActivity.cs) - Go to the Github page

 // Store off the device sizes, so we can access them within Xamarin Forms // Screen Width = WidthPixels / Density // Screen Height = HeightPixels / Density App.DisplayScreenWidth = (double)Resources.DisplayMetrics.WidthPixels / (double)Resources.DisplayMetrics.Density; App.DisplayScreenHeight = (double)Resources.DisplayMetrics.HeightPixels / (double)Resources.DisplayMetrics.Density; App.DisplayScaleFactor = (double)Resources.DisplayMetrics.Density; 

iOS (AppDelegate.cs) - Go to the Github page

 // Store off the device sizes, so we can access them within Xamarin Forms App.DisplayScreenWidth = (double)UIScreen.MainScreen.Bounds.Width; App.DisplayScreenHeight = (double)UIScreen.MainScreen.Bounds.Height; App.DisplayScaleFactor = (double)UIScreen.MainScreen.Scale; 

Note. In all Windows examples, screensize is the namespace of the root projects

UWP (App.xaml.cs) - Go to the Github page

 // You decided which is best for you... // Do you want Size of the App View // or // Do you want the Display resolution // ###################################### // Size of App view screensize.App.DisplayScreenHeight = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().VisibleBounds.Height; screensize.App.DisplayScreenWidth = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().VisibleBounds.Width; // Size of screen resolution //screensize.App.DisplayScreenWidth = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().ScreenHeightInRawPixels; //screensize.App.DisplayScreenHeight = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().ScreenWidthInRawPixels; // Pixels per View Pixel // - https://msdn.microsoft.com/en-us/windows/uwp/layout/design-and-ui-intro#effective-pixels-and-scaling // - https://msdn.microsoft.com/en-us/windows/uwp/layout/screen-sizes-and-breakpoints-for-responsive-design screensize.App.DisplayScaleFactor = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel; 

Windows Phone 8.1 (App.xaml.cs) - Go to the Github page

 // Size of App view screensize.App.DisplayScreenHeight = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().VisibleBounds.Height; screensize.App.DisplayScreenWidth = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().VisibleBounds.Width; // Pixels per View Pixel // - https://msdn.microsoft.com/en-us/windows/uwp/layout/design-and-ui-intro#effective-pixels-and-scaling // - https://msdn.microsoft.com/en-us/windows/uwp/layout/screen-sizes-and-breakpoints-for-responsive-design screensize.App.DisplayScaleFactor = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel; 

Windows 8.1 (App.xaml.cs) - Go to the Github page

 // Size of App view screensize.App.DisplayScreenHeight = Window.Current.Bounds.Height; screensize.App.DisplayScreenWidth = Window.Current.Bounds.Width; screensize.App.DisplayScaleFactor = 1; // No scaling here? If you find a scaling for Windows 8.1, please let me know :) 

Xamarin Forms Page (App.cs) - Go to the Github Page

 namespace screensize { public class App : Application { public static double DisplayScreenWidth = 0f; public static double DisplayScreenHeight = 0f; public static double DisplayScaleFactor = 0f; public App() { string ScreenDetails = Device.OS.ToString() + " Device Screen Size:\n" + $"Width: {DisplayScreenWidth}\n" + $"Height: {DisplayScreenHeight}\n" + $"Scale Factor: {DisplayScaleFactor}"; // The root page of your application var content = new ContentPage { Title = "Xamarin_GetDeviceScreensize", Content = new StackLayout { VerticalOptions = LayoutOptions.Center, Children = { new Label { HorizontalTextAlignment = TextAlignment.Center, FontSize = Device.GetNamedSize (NamedSize.Large, typeof(Label)), Text = ScreenDetails } } } }; MainPage = new NavigationPage(content); } } } 

Additional Pros:

jasonCodesAway made a nice Xamarin plugin for determining screen size

+22
source

This list shows ios device consumption data. You can also use the GBDeviceInfo thrid-party tool to get screen density for an iOS device, for example, this is [GBDeviceInfo deviceInfo].displayInfo.pixelsPerInch;

IOS:

  iPhones iPhone 1/3G/3GS 320 x 480 163 ppi iPhone 4/4S 640 x 940 326 ppi iPhone 5/5C/5S 640 x 1136 326 ppi iPhone 6/6S 750 x 1334 326 ppi iPhone 6 Plus/6S Plus 1080 x 1920 401 ppi Without downsampling: 1242 x 2208 461 ppi Except for the 6th generation (= 5th) iPod touch, all the models are equal to their iPhone counterparts iPads iPad 1/2 768 x 1024 132 ppi iPad 3/4/Air/Air 2 1536 x 2048 264 ppi iPad Mini 1 768 x 1024 163 ppi iPad Mini 2/3/4 1536 x 2048 326 ppi iPad Pro 2737 x 2048 264 ppi Apple Watches Apple Watch 38 mm 272 x 340 326 ppi Apple Watch 42 mm 312 x 390 326 ppi 

I found an API in UWP to get device tones. Windows.Graphics.Display.DisplayInformation . You can try.

+1
source

Source: https://habr.com/ru/post/1262372/


All Articles