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

intconverSpToPixel(Context context, float spValue)
conver Sp To Pixel
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
return (int) (spValue * metrics.scaledDensity + 0.5f);
floatconvertDpToPixel(Context context, float dpValue)
convert Dp To Pixel
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
return dpValue * (metrics.densityDpi / 160f);
floatconvertDpToPixel(Context ctx, float dp)
This method converts dp unit to equivalent device specific value in pixels.
Resources resources = ctx.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
return dp * (metrics.densityDpi / 160f);
intdip2pixel(Context context, int dip)
http://developer.android.com/guide/practices/screens_support.html#dips-pels
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dip * scale + 0.5f);
intdip2px(Context context, float dipValue)
dippx
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dipValue * scale + 0.5f);
intgetPixels(int dp, Context context)
get Pixels
final float scale = context.getResources().getDisplayMetrics().density;
int px = (int) (dp * scale + 0.5f);
return px;
intdip2Px(Context context, float dipValue)
dip Px
final float scale = getDensity(context);
return (int) (dipValue * scale + 0.5f);
intdip2px(Context context, float dpValue)
dippx
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
intdip2px(Context context, float dpValue)
dip to px
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
intdip2px(Context context, float dpvalue)
dippx
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpvalue * scale + 0.5f);