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

intdipToPixel(int dip)
Converts DIP to pixels.
return (int) (dip * scale + 0.5f);
intdipsToPixels(Context aContext, int aDip)
Converts the given DIPs into current device/resolution pixels.
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
        aDip, aContext.getResources().getDisplayMetrics());
return Math.round(px);
intconvertDIP2PX(Context context, float dip)
convert DIPPX
Resources resources = context.getResources();
float scale = resources.getDisplayMetrics().density;
resources = null;
return (int) (dip * scale + 0.5f * (dip >= 0 ? 1 : -1));
intsp2px(Context context, float spValue)
sppx
final float scale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (spValue * scale + 0.5f);
intscaleDpToPixels(double value, Resources res)
scale Dp To Pixels
final float scale = res.getDisplayMetrics().density;
return (int) Math.round(value * scale);
floatconvertDpToPixel(float dp, Context context)
convert Dp To Pixel
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float px = dp * (metrics.densityDpi / 160f);
return px;
intconvertDpToPixel(float dp, Resources resources)
convert Dp To Pixel
DisplayMetrics metrics = resources.getDisplayMetrics();
float px = dp * (metrics.densityDpi / 160f);
return (int) px;
intdipToPixels(int dip)
dip To Pixels
float density = resources.getDisplayMetrics().density;
int valuePixels = (int) (dip * density + 0.5f);
return valuePixels;
intsp2px(Context context, float spValue)
sppx
Resources resources = context.getResources();
final float fontScale = resources.getDisplayMetrics().scaledDensity;
resources = null;
return (int) (spValue * fontScale + 0.5f);
floatsp2px(float sp, Context context)
sppx
final float scale = context.getResources().getDisplayMetrics().scaledDensity;
return sp * scale;