Java tutorial
//package com.java2s; import android.content.Context; import android.content.res.Resources; import android.util.DisplayMetrics; public class Main { /** * This method converts dp unit to equivalent pixels, depending on device * density. * * @param context Context to get resources and device specific display metrics * @param dp A value in dp (density independent pixels) unit. Which we need * to convert into pixels * @return Value multiplied by the appropriate metric and truncated to * integer pixels. */ public static int convertDpToPixelSize(Context context, float dp) { float pixels = convertDpToPixel(context, dp); final int res = (int) (pixels + 0.5f); if (res != 0) { return res; } else if (pixels == 0) { return 0; } else if (pixels > 0) { return 1; } return -1; } /** * This method converts dp unit to equivalent pixels, depending on device * density. * * @param context Context to get resources and device specific display metrics * @param dp A value in dp (density independent pixels) unit. Which we need * to convert into pixels * @return A float value to represent px equivalent to dp depending on * device density */ public static float convertDpToPixel(Context context, float dp) { return dp * (getDisplayMetrics(context).densityDpi / 160f); } public static DisplayMetrics getDisplayMetrics(Context context) { Resources resources = context.getResources(); return resources.getDisplayMetrics(); } }