Android Utililty Methods Dip to Pixel Convert

List of utility methods to do Dip to Pixel Convert

Description

The list of methods to do Dip to Pixel Convert are organized into topic(s).

Method

intdip2px(float dipValue, float scale)
dippx
return (int) (dipValue * scale + 0.5f);
floatdipToPixels(Context context, float dipValue)
dip To Pixels
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
        dipValue, metrics);
intdp2px(Context context, float dp)
dppx
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
        dp, metrics);
intdpToPixel(Context context, int dp)
dp To Pixel
if (context == null) {
    return 0;
return (int) (context.getResources().getDisplayMetrics().density * dp);
intdpToPx(Context context, int dpValue)
Convert dp value to px value
DisplayMetrics displayMetrics = context.getResources()
        .getDisplayMetrics();
return (int) ((dpValue * displayMetrics.density) + 0.5);
intdpToPx(Context ctx, int dp)
dp To Px
DisplayMetrics displayMetrics = ctx.getResources()
        .getDisplayMetrics();
int px = Math.round(dp
        * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
return px;
intformatDipToPx(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);
intsp2px(Context context, float spValue)
sppx
final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (spValue * fontScale + 0.5f);
intsp2px(Context context, float spValue)
sppx
final float scale = getDensity(context);
return (int) (spValue * scale + 0.5f);
intspToPx(Context context, int spValue)
Convert sp value to px value
DisplayMetrics displayMetrics = context.getResources()
        .getDisplayMetrics();
return (int) ((spValue * displayMetrics.scaledDensity) + 0.5);