If you are working on a Mobile application (using mobile CSS, native Android SDK or native iOS SDK) the first problem you are going to face is the difference between the various screen sizes. For example, if you work with Android you will notice that different devices have different screen resolutions. Android categorize these devices in 4 different buckets called respectively MDPI, HDPI, XHDPI and XXHDPI
As I usually say, a picture is worth a thousands words:
What is DPI and why we should care?
DPI stands for Dots per Inches, which can be translated in how many pixels can be drawn into a screen for a given inch of screen’s space.
This measure is totally unbind to the screen size and to the pixel resolution so we can state that screens at different size and different resolution may be classified within the same DPI category and screens with same size but different resolution may be classified into different DPI category.
Assuming we are loading on our phone a raster picture of XX px wide, this is the result we will see using different DPI if we keep the image at the same size:
The blurring effect is caused by the fact that on a screen with 165dpi the amount of pixels drawn per inch is way lower (165) than on a 450dpi screen so the first thing that we loose is the sharpness of the image.
How Android works with DPI?
In android you can classify your device’s screens into 4 or more different dpi buckets which are used to classify the device’s screen depending on the amount of dpi and not the pixel resolution of the screen size. The picture below shows the available DPI classifications with a sample device for each category. You can find all the available DPI classification on this lovely website DPI Love.
So for Android specifically, a device of 160 DPI has a ratio of 1:1 with the pixels on the screen while a device with more than 480 DPI has a ratio of 1:3 pixels on the screen compared to the same design for a 160 DPI screen.
Based on this classification we can now easily derive the following formula which can be used to calculate the real DPI resolution of a device based on its DPI classification and pixels resolution:
The Formula can be translated as DP = PX * 160 / DPI. So let’s make a couple of examples.
We want to design on the screen a square that should be 200px * 50px on our MDPI screen which we are using for mocking the UI (_this is what I call default viewport)_
Note: in Android SDK you will refer to DP to define a density independent measure and not DPI, this is why the previous formula has on the left side px (pixels) and dp (density independent pixels).
Considering the previous list of devices (Figure 1) this is the result I come with in order to have the same aspect ration of my rectangle over multiple devices starting from an MDPI viewport:
|GALAXY ACE||MDPI||1:1||200 * 50 px||200 * 50 dp|
|HTC DESIRE||HDPI||1:1.5||200 * 50 px||133 * 33 dp|
|NEXUS 7||XHDPI||1:2||200 * 50 px||100 * 25 dp|
|NEXUS 6||XXHDPI||1:3||200 * 50 px||67 * 16 dp|
Regarding iOS the ratio is exactly the same except for XHDPI (retina) where the ratio is 1:2.25 and not 1:3 like in Android. iOS does not offer a classification for XXHDPI devices.