Skip to content

Instantly share code, notes, and snippets.

@li2
Last active March 20, 2018 05:08
Show Gist options
  • Save li2/ddda3484ef4c212870bf9acb014d8245 to your computer and use it in GitHub Desktop.
Save li2/ddda3484ef4c212870bf9acb014d8245 to your computer and use it in GitHub Desktop.
dp to pixel #tags: android-view
/**
* 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