Java tutorial
//package com.java2s; import android.util.DisplayMetrics; import android.util.Log; public class Main { private static DisplayMetrics mMetrics; /** * This method converts dp unit to equivalent pixels, depending on device * density. NEEDS UTILS TO BE INITIALIZED BEFORE USAGE. * * @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(float dp) { if (mMetrics == null) { Log.e("MPChartLib-Utils", "Utils NOT INITIALIZED. You need to call Utils.init(...) at least once before" + " calling Utils.convertDpToPixel(...). Otherwise conversion does not " + "take place."); return dp; // throw new IllegalStateException( // "Utils NOT INITIALIZED. You need to call Utils.init(...) at least once before // calling Utils.convertDpToPixel(...)."); } DisplayMetrics metrics = mMetrics; float px = dp * (metrics.densityDpi / 160f); return px; } }