Can I detect DPI in iPhone OS or Android?

I am doing some research to create a game that can scale its graphic resources in accordance with the DPI of any device.

To do this, I would like to be able to query the DPI of the device in order to scale assets accordingly.

This is a two-dimensional game, and the style style is suitable for arbitrary scaling.

An example of what I would do with res / dpi compilers:

iPhone / iTouch, at 320x480 (163 dpi) - text will be normal size

iPhone4, at 640x960 (326 dpi) - the text will be twice as large

iPad, at 768x1024 (132 dpi) - text is likely to be limited to a certain minimum size to take advantage of larger screen real estate.

So, on iPhone OS, is there a way to request a DPI screen?

(and as an additional note - is this possible on Android devices?)

+4
source share
2 answers

You can get this information on Android with the DisplayMetrics class.

+1
source

I'm not sure if there is a direct way to get DPI, since you don't need to know that at all. The UIScreen class provides a scale factor. For iPhone & 3GS and iPad, it should be 1.0, for iPhone 4 - 2.0.

View compiled:

UIScreen *theScreen = [UIScreen mainScreen]; float scaleFactor = 1.0f; if ([theScreen respondsToSelector:@selector(getScale)]) { scaleFactor = theScreen.scale; } // use scaleFactor to determine size of fonts/whatever 

But the way the new OS is implemented in relation to different screen resolutions means that you usually do not need to adjust the scale depending on the DPI. Things like images will either be enlarged or they will automatically select a specially named image with a higher resolution, and drawing operations will also be processed automatically (for example, drawing a 1point line now draws a 2px line on Iphone 4). Fonts can be handled in a similar way, I don't think I came across any font. Read the new docs for information:

http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/SupportingResolutionIndependence/SupportingResolutionIndependence.html#//apple_ref/doc/uid/TP40007072-CH10

+1
source

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


All Articles