Calculate apps density pixel width & height using Titanium.Platform.DisplayCaps.dpi?
When getting the height and width of the application using: Titanium.Platform.DisplayCaps.platformWidth/platformHeight it returns the values in pixels. My app is using density pixel width's and height's so it looks consistent across various devices. Using pixel values results in unexpected layout.
Is it possible to caclulate the density pixel width & height using the Titanium.Platform.DisplayCaps.dpi value?
Thanks
4 Answers
-
Accepted Answer
Jimmy, here are the functions for converting pixels to DPs and vice versa:
function PixelsToDPUnits(ThePixels) { return (ThePixels / (Titanium.Platform.displayCaps.dpi / 160)); } function DPUnitsToPixels(TheDPUnits) { return (TheDPUnits * (Titanium.Platform.displayCaps.dpi / 160)); }
-
Please be careful people,
Shawn's answer is wrong. It will seem to work perfectly for iPhone and Android, but then it will fail for the older iPads.
The reason is that the reference point he has taken for converting between density independent pixels and real pixels (160) is not in fact universal. If you read the docs here, you will find that there is no universal reference point for density independent pixels. On android the reference point is indeed 160, but for iPhone it varies with device.
Quoting from the docs:
"On iOS, a DIP corresponds to one pixel on a non-Retina display, which is 163DPI for iPhone/iPod touch and 132DPI for the iPad. A DIP corresponds to 2 pixels of width or height on a Retina display."So the functions need to include some kind of check based on the device and the value of
Titanium.Platform.displayCaps.dpi
A quick work around would be:
function PixelsToDPUnits(ThePixels) { if ( Titanium.Platform.displayCaps.dpi > 160 ) return (ThePixels / (Titanium.Platform.displayCaps.dpi / 160)); else return ThePixels; } function DPUnitsToPixels(TheDPUnits) { if ( Titanium.Platform.displayCaps.dpi > 160 ) return (TheDPUnits * (Titanium.Platform.displayCaps.dpi / 160)); else return TheDPUnits; }
But there could be more exceptions.
-
Check out this link.
It will tell you how density is calculated. -
According to this Android Density Independece the following is a better version of all the above code -for any future use… :)
function dipUnitsToPixels(dipUnits) { var DENSITY_SMALL_LDPI = 120; var DENSITY_BASELINE_MDPI = 160; var DENSITY_HIGH_HDPI = 240; var DENSITY_EXTRA_HIGH_XHDPI = 320; var ret; switch (Ti.Platform.displayCaps.dpi) { case DENSITY_SMALL_LDPI: ret = (dipUnits * (Ti.Platform.displayCaps.dpi / DENSITY_SMALL_LDPI)); break; case DENSITY_BASELINE_MDPI: ret = (dipUnits * (Ti.Platform.displayCaps.dpi / DENSITY_BASELINE_MDPI)); break; case DENSITY_HIGH_HDPI: ret = (dipUnits * (Ti.Platform.displayCaps.dpi / DENSITY_HIGH_HDPI)); break; case DENSITY_EXTRA_HIGH_XHDPI: ret = (dipUnits * (Ti.Platform.displayCaps.dpi / DENSITY_EXTRA_HIGH_XHDPI)); break; default: ret = dipUnits; } return ret; } function pixelsToDipUnits(pixels) { var DENSITY_SMALL_LDPI = 120; var DENSITY_BASELINE_MDPI = 160; var DENSITY_HIGH_HDPI = 240; var DENSITY_EXTRA_HIGH_XHDPI = 320; var ret; switch (Ti.Platform.displayCaps.dpi) { case DENSITY_SMALL_LDPI: ret = (dipUnits / (Ti.Platform.displayCaps.dpi / DENSITY_SMALL_LDPI)); break; case DENSITY_BASELINE_MDPI: ret = (dipUnits / (Ti.Platform.displayCaps.dpi / DENSITY_BASELINE_MDPI)); break; case DENSITY_HIGH_HDPI: ret = (dipUnits / (Ti.Platform.displayCaps.dpi / DENSITY_HIGH_HDPI)); break; case DENSITY_EXTRA_HIGH_XHDPI: ret = (dipUnits / (Ti.Platform.displayCaps.dpi / DENSITY_EXTRA_HIGH_XHDPI)); break; default: ret = dipUnits; } return ret; }