Skip to content

Instantly share code, notes, and snippets.

@xalexchen
Created March 16, 2014 08:07
Show Gist options
  • Save xalexchen/9579993 to your computer and use it in GitHub Desktop.
Save xalexchen/9579993 to your computer and use it in GitHub Desktop.
Android ScreenUtils
public class ScreenUtils {
public static float dpToPx(Context context, float dp) {
if (context == null) {
return -1;
}
return dp * context.getResources().getDisplayMetrics().density;
}
public static float pxToDp(Context context, float px) {
if (context == null) {
return -1;
}
return px / context.getResources().getDisplayMetrics().density;
}
public static float dpToPxInt(Context context, float dp) {
return (int)(dpToPx(context, dp) + 0.5f);
}
public static float pxToDpCeilInt(Context context, float px) {
return (int)(pxToDp(context, px) + 0.5f);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment