Created
March 16, 2014 08:07
-
-
Save xalexchen/9579993 to your computer and use it in GitHub Desktop.
Android ScreenUtils
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
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