List of utility methods to do Dip to Pixel Convert
int | dip2px(float dipValue, float scale) dippx return (int) (dipValue * scale + 0.5f); |
float | dipToPixels(Context context, float dipValue) dip To Pixels DisplayMetrics metrics = context.getResources().getDisplayMetrics();
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
dipValue, metrics);
|
int | dp2px(Context context, float dp) dppx DisplayMetrics metrics = context.getResources().getDisplayMetrics(); return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, metrics); |
int | dpToPixel(Context context, int dp) dp To Pixel if (context == null) { return 0; return (int) (context.getResources().getDisplayMetrics().density * dp); |
int | dpToPx(Context context, int dpValue) Convert dp value to px value DisplayMetrics displayMetrics = context.getResources() .getDisplayMetrics(); return (int) ((dpValue * displayMetrics.density) + 0.5); |
int | dpToPx(Context ctx, int dp) dp To Px DisplayMetrics displayMetrics = ctx.getResources() .getDisplayMetrics(); int px = Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT)); return px; |
int | formatDipToPx(Context context, float dip) format Dip To Px DisplayMetrics dm = new DisplayMetrics(); ((Activity) context).getWindowManager().getDefaultDisplay() .getMetrics(dm); return (int) Math.ceil(dip * dm.density); |
int | sp2px(Context context, float spValue) sppx final float fontScale = context.getResources().getDisplayMetrics().scaledDensity; return (int) (spValue * fontScale + 0.5f); |
int | sp2px(Context context, float spValue) sppx final float scale = getDensity(context); return (int) (spValue * scale + 0.5f); |
int | spToPx(Context context, int spValue) Convert sp value to px value DisplayMetrics displayMetrics = context.getResources() .getDisplayMetrics(); return (int) ((spValue * displayMetrics.scaledDensity) + 0.5); |