Skip to content

Instantly share code, notes, and snippets.

@vbarthel-fr
Created January 28, 2015 13:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vbarthel-fr/343b10d9d748f0b267c9 to your computer and use it in GitHub Desktop.
Save vbarthel-fr/343b10d9d748f0b267c9 to your computer and use it in GitHub Desktop.
View utils
/**
* Convert a value from dip to pixel.
*
* @param displayMetrics the {@link android.util.DisplayMetrics} used to convert.
* @param dip the value in dip to convert.
* @return a value in pixel.
*/
public static int dipToPx(DisplayMetrics displayMetrics, float dip) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, displayMetrics);
}
/**
* Convert a value from sp to pixel.
*
* @param displayMetrics the {@link android.util.DisplayMetrics} used to convert.
* @param sp the value in sp to convert.
* @return a value in pixel.
*/
public static float spToPx(DisplayMetrics displayMetrics, float sp) {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, displayMetrics);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment