Skip to content

Instantly share code, notes, and snippets.

@manumaticx
Created December 17, 2013 12:57
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save manumaticx/8004519 to your computer and use it in GitHub Desktop.
Save manumaticx/8004519 to your computer and use it in GitHub Desktop.
Function to calculate display size as inches on android for Appcelerator Titanium with the help of https://github.com/dbankier/HasMenu
/**
* Calculates the physical display size for android devices
* e.g 4,95 for Nexus 5 or 7,02 for Nexus 7
*
* IMPORTANT: this requires https://github.com/dbankier/HasMenu
*
* @return {Number} size as inches
*/
function getPhysicalSize(){
// some infos we need
var pfWidth = Ti.Platform.displayCaps.platformWidth,
pfHeight = Ti.Platform.displayCaps.platformHeight,
navigationBar = !require('yy.hasmenu').hasMenu,
orientation = Ti.Gesture.orientation;
// adapt value where appropriate
if (navigationBar){
if (orientation == Ti.UI.LANDSCAPE_LEFT || orientation == Ti.UI.LANDSCAPE_RIGHT){
// if in landscape, add navigation bar size to width
pfWidth += 48 * Ti.Platform.displayCaps.logicalDensityFactor;
}else{
// if in portrait, add navigation bar size to height
pfHeight += 48 * Ti.Platform.displayCaps.logicalDensityFactor;
}
}
// calculate physical values
var physicalWidth = pfWidth / Ti.Platform.displayCaps.xdpi;
var physicalHeight = pfHeight / Ti.Platform.displayCaps.ydpi;
// pythagoras stuff
var inch = Math.sqrt(physicalWidth * physicalWidth + physicalHeight * physicalHeight);
return inch;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment