Skip to content

Instantly share code, notes, and snippets.

@akexorcist
Last active August 29, 2015 14:01
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 akexorcist/d7bce5e5a22857e590c9 to your computer and use it in GitHub Desktop.
Save akexorcist/d7bce5e5a22857e590c9 to your computer and use it in GitHub Desktop.
แปลงหน่วย dp เป็น px และแปลงหน่วย px เป็น dp
public int dpToPx(Context context, int dp) {
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
int px = Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
return px;
}
public int pxToDp(Context context, int px) {
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
int dp = Math.round(px / (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
return dp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment