Skip to content

Instantly share code, notes, and snippets.

View nesterchung's full-sized avatar

Nester Chung nesterchung

View GitHub Profile
@nesterchung
nesterchung / DpToPxAndPxToDp
Created October 14, 2016 04:36 — forked from laaptu/DpToPxAndPxToDp
Android convert dp to px and vice versa
public static float convertPixelsToDp(float px){
DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
float dp = px / (metrics.densityDpi / 160f);
return Math.round(dp);
}
public static float convertDpToPixel(float dp){
DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
float px = dp * (metrics.densityDpi / 160f);
return Math.round(px);