Skip to content

Instantly share code, notes, and snippets.

@mogya
Created September 25, 2012 12:24
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mogya/3781468 to your computer and use it in GitHub Desktop.
Save mogya/3781468 to your computer and use it in GitHub Desktop.
An idea to convert dp to/from pixel on TitaniumMobile Android
// convert dp to pixel.
function dpToPixel(dp) {
return ( parseInt(dp) * (Titanium.Platform.displayCaps.dpi / 160));
}
// convert pixel to dp.
function pixelToDp(px) {
return ( parseInt(px) / (Titanium.Platform.displayCaps.dpi / 160))+'dp';
}
Ti.API.debug('dpToPixel test 100px:'+dpToPixel('100dp'));
Ti.API.debug('dpToPixel test s:100:'+dpToPixel('100'));
Ti.API.debug('dpToPixel test i:100:'+dpToPixel(100));
Ti.API.debug('pixelToDp test 100px:'+pixelToDp('100px'));
Ti.API.debug('pixelToDp test s:100:'+pixelToDp('100'));
Ti.API.debug('pixelToDp test i:100:'+pixelToDp(100));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment