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

floatconvertPixelsToDp(Context context, float pxValue)
convert Pixels To Dp
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
return pxValue / (metrics.densityDpi / 160f);
floatconvertPixelsToDp(Context ctx, float px)
This method converts device specific pixels to device independent pixels.
Resources resources = ctx.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
return px / (metrics.densityDpi / 160f);
intconvertPixelsToSp(Context context, float pxValue)
convert Pixels To Sp
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
return (int) (pxValue / metrics.scaledDensity + 0.5f);
intpx2dip(Context context, float pxValue)
pxdip
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
intpx2dip(Context context, float pxValue)
pxdip
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
intpx2dip(Context context, float pxValue)
pxdip
final float scale = getDensity(context);
return (int) (pxValue / scale + 0.5f);
intpx2dip(Context context, float pxValue)
px to dip
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
intpx2dip(Context context, float pxvalue)
pxdip
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxvalue / scale + 0.5f);
intpx2dip(float pxValue, float scale)
pxdip
return (int) (pxValue / scale + 0.5f);
intpx2dp(Context context, int px)
pxdp
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX,
        px, metrics);