Last active
June 5, 2018 06:22
-
-
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)
This file contains 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
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A class to convert dp to pixel and vice versa