Android Utililty Methods Pixel to Dip Convert

List of utility methods to do Pixel to Dip Convert

Description

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

Method

intpixelsToDpi(Resources res, int pixels)
pixels To Dpi
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
        (float) pixels, res.getDisplayMetrics());
intconvertPX2DIP(Context context, float px)
convert PXDIP
Resources resources = context.getResources();
float scale = resources.getDisplayMetrics().density;
resources = null;
return (int) (px / scale + 0.5f * (px >= 0 ? 1 : -1));
intpx2dip(Context context, float px)
pxdip
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (px / scale + 0.5f);
intpx2sp(Context context, float pxValue)
pxsp
final float scale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (pxValue / scale + 0.5f);
floatgetPixelsFromDIP(Context context, int dipValue)
get Pixels From DIP
if (context != null) {
    return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
            dipValue, context.getResources().getDisplayMetrics());
return dipValue;
floatpixel2dip(Context context, float n)
pixeldip
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float dp = n / (metrics.densityDpi / 160f);
return dp;
intconvertPixtoDip(int pixel, Context context)
convert Pixto Dip
float scale = getDensity(context);
return (int) ((pixel - 0.5f) / scale);