Last active
March 20, 2018 05:08
-
-
Save li2/ddda3484ef4c212870bf9acb014d8245 to your computer and use it in GitHub Desktop.
dp to pixel
#tags: android-view
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
/** | |
* Convert the dps to pixels, based on density scale | |
* @param dp value expressed in dps | |
* @return value expressed in pixels | |
*/ | |
public int dpToPixel(int dp) { | |
// Get the screen's density scale | |
float scale = getResources().getDisplayMetrics().density; | |
// Add 0.5f to round the figure up to the nearest whole number | |
return (int) (dp * scale + 0.5f); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment