Created
December 17, 2013 12:57
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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