Skip to content

Instantly share code, notes, and snippets.

@pfieffer
Last active June 5, 2018 06:22
Show Gist options
  • Save pfieffer/23345cfe2f9c07dd3f7b5b79f02655b1 to your computer and use it in GitHub Desktop.
Save pfieffer/23345cfe2f9c07dd3f7b5b79f02655b1 to your computer and use it in GitHub Desktop.
A utility class to convert DP(Desnsity independent Pixel to Pixel and vice versa)
public class DpPixelConversion {
public static int dpToPx(Context context, float dp) {
return (int) (dp * ((float) context.getResources().getDisplayMetrics().densityDpi / DisplayMetrics.DENSITY_DEFAULT));
}
public static float pxToDp(Context context, float px) {
return px / ((float) context.getResources().getDisplayMetrics().densityDpi / DisplayMetrics.DENSITY_DEFAULT);
}
}
@pfieffer
Copy link
Author

pfieffer commented Jun 5, 2018

A class to convert dp to pixel and vice versa

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment