Skip to content

Instantly share code, notes, and snippets.

@tai2
Created January 24, 2012 16:11
Show Gist options
  • Save tai2/1670883 to your computer and use it in GitHub Desktop.
Save tai2/1670883 to your computer and use it in GitHub Desktop.
TypedValue#applyDimension
public static float applyDimension(int unit, float value,
DisplayMetrics metrics)
{
switch (unit) {
case COMPLEX_UNIT_PX:
return value;
case COMPLEX_UNIT_DIP:
return value * metrics.density;
case COMPLEX_UNIT_SP:
return value * metrics.scaledDensity;
case COMPLEX_UNIT_PT:
return value * metrics.xdpi * (1.0f/72);
case COMPLEX_UNIT_IN:
return value * metrics.xdpi;
case COMPLEX_UNIT_MM:
return value * metrics.xdpi * (1.0f/25.4f);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment