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

floatconvertToPixel(Context context, float dp)
convert To Pixel
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
        context.getResources().getDisplayMetrics());
floatgetPixelFromDp(float dp, Context context)
This method converts dp unit to equivalent pixels, depending on device density.
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
return dp * (metrics.densityDpi / 160f);
intconvertDptoPx(Context context, int dp)
convert Dpto Px
float scale = context.getResources().getDisplayMetrics().density;
return (int) (dp * scale + 0.5f);
intdip2px(Context context, int dip)
Convert dip to px value
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dip * scale + 0.5f);
intdipValue(Context context, int dip)
dip Value
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
        dip, context.getResources().getDisplayMetrics());
intdip2pixel(Context context, float n)
dippixel
int value = (int) TypedValue.applyDimension(
        TypedValue.COMPLEX_UNIT_DIP, n, context.getResources()
                .getDisplayMetrics());
return value;
intdip2px(Context context, float dipValue)
dippx
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dipValue * scale + 0.5f);
intdip2px(Context context, float dipValue)
dippx
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dipValue * scale + 0.5f);
intdip2px(Context context, float dpValue)
dippx
final float scale = context.getResources().getDisplayMetrics().density;
Log.e("desitiny", scale + "");
return (int) (dpValue * scale / 1.5 + 0.5f);
intdip2px(Context context, float dpValue)
dippx
final float scale = context.getResources().getDisplayMetrics().density;
Log.e("desitiny", scale + "");
return (int) (dpValue * scale / 1.5 + 0.5f);