Skip to content

Instantly share code, notes, and snippets.

@carlosgub
Created June 21, 2019 22:14
Show Gist options
  • Save carlosgub/f809ae550bfdf5f90cb65196f95d779c to your computer and use it in GitHub Desktop.
Save carlosgub/f809ae550bfdf5f90cb65196f95d779c to your computer and use it in GitHub Desktop.
Dp to Int
/**
* This method converts dp unit to equivalent pixels, depending on device density.
*
* @param dp A value in dp (density independent pixels) unit. Which we need to convert into pixels
* @param context Context to get resources and device specific display metrics
* @return A float value to represent px equivalent to dp depending on device density
*/
public float convertDpToPx(Context context, float dp) {
return dp * context.getResources().getDisplayMetrics().density;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment