This post was originally published at Approximating Density Independence on Android

In a previous post, I discussed physical (real-world) screen dimensions and how Apple applied this principle to its line of iOS devices. More specifically, Apple ensured that these devices share the same virtual pixel (point) density — roughly 163 virtual-pixels-per-inch (VPPI).

In contrast to Apple, Google explicitly spells out the principle of physical length for Android developers. They call this density independence in their guide:

Your application achieves “density independence” when it preserves the physical size (from the user’s point of view) of user interface elements when displayed on screens with different densities.

The density-independent pixel, or dp for short, is Android’s equivalent of an iOS point, a platform-specific term for a virtual pixel. They’ve explicitly defined these dp units so that 160 fit in an inch, i.e. 160 VPPI.

Luckily, that’s means Android and iOS have almost identical virtual pixel densities.

One challenge for Google is that it cannot control the actual hardware pixel densities of the screens used by Android device manufacturers, nor define precise virtual screen dimensions. Instead, they define general screen types:

  • ldpi (low) = ~120 PPI
  • mdpi (medium) = ~160 PPI
  • hdpi (high) = ~240 PPI
  • xhdpi (extra-high) = ~320 PPI
  • xxhdpi (extra-extra-high) = ~480 PPI
  • xxxhdpi (extra-extra-extra-high) = ~640 PPI

Every Android device falls into of these generalized screen types and has a corresponding real pixel density that’s approximately the value listed above. With this scheme, Android is able to know how many real hardware pixels go in a single virtual pixel, or in Android nomenclature, a single dp.

Android has normalized these devices and the real-to-virtual scales to whole number and simple fractions. For example, there’s a 1:1 mapping of real pixels to virtual pixels on an mdpi device because the PPI is the same as the 160 VPPI of dp pixels. Likewise, there’s a 2:1 mapping (2 real pixels for every single dp pixel) on an xhdpi device.

This being said, it all comes at the price of precision. The ruler length of 100 virtual pixels on one Android device is not guaranteed to be the same on another. It’s only approximately the same length. So, unlike iOS, Android can only approximate consistency when it comes to the physical size of UI elements. This is because the real PPIs differ for the same class device. For example, on an xhdpi device, Android requires that these devices be around 320 PPI, but not exactly. A Samsung Galaxy S3 and a Google Galaxy Nexus are both xhdpi devices, but their PPI’s are different (306 and 316, respectively), so they are only roughly 160 VPPI.

To summarize, we saw that there’s a fairly consistent size for a virtual pixel on iOS, but on Android, the size is only approximately the same across devices. Still, in practice, this is good enough.

In the final post, we’ll review Corona’s existing content scaling system and then talk about a new mode that allows you to take advantage of the principle of physical length for UI-oriented apps.

View this article: 

This post was originally published at Approximating Density Independence on Android