Created
March 21, 2013 18:46
-
-
Save kingargyle/5215590 to your computer and use it in GitHub Desktop.
Get the DPI size to use based on Screen Density and the Pixel size. This allows for calculation of image width and image height based on the pixel width and pixel height. Pass in the pixel size and it returns the dpi size to be used in Layouts.
This file contains hidden or 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
| /** | |
| * Takes a value in pixels and converts it to a dpi value. It adjusts | |
| * the dpi size based on the screen density that is returned by | |
| * android. | |
| * | |
| * @param originalHeight | |
| * @param context The activity context | |
| * @return | |
| */ | |
| public static int getDPI(int pixelsize, Activity context) { | |
| DisplayMetrics metrics = new DisplayMetrics(); | |
| context.getWindowManager().getDefaultDisplay().getMetrics(metrics); | |
| float sizeMultiplier = 1; | |
| switch (metrics.densityDpi) { | |
| case DisplayMetrics.DENSITY_LOW: { | |
| break; | |
| } | |
| case DisplayMetrics.DENSITY_MEDIUM: { | |
| break; | |
| } | |
| case DisplayMetrics.DENSITY_TV: { | |
| sizeMultiplier = 1.5f; | |
| break; | |
| } | |
| case DisplayMetrics.DENSITY_XHIGH: { | |
| sizeMultiplier = 3; | |
| break; | |
| } | |
| default: { | |
| sizeMultiplier = 3; | |
| break; | |
| } | |
| } | |
| int dpi = Math.round((pixelsize * sizeMultiplier) / metrics.density); | |
| return dpi; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment