Skip to content

Instantly share code, notes, and snippets.

@JosiasSena
Last active July 14, 2016 18:01
Show Gist options
  • Save JosiasSena/e57ddfab096109a53b451445902341d5 to your computer and use it in GitHub Desktop.
Save JosiasSena/e57ddfab096109a53b451445902341d5 to your computer and use it in GitHub Desktop.
/**
* The calculation (value * scale + 0.5f) is a widely used to convert from dps to pixel units
* based on density scale
* see developer.android.com (Supporting Multiple Screen Sizes)
*/
public static int convertDpToPixels(int dp, DisplayMetrics displayMetrics) {
return Math.round(dp * ((displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT)));
}
Note: to get the display metrics do: getContext().getResources().getDisplayMetrics();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment