Android examples for android.graphics:Pixel
converts dp unit to equivalent pixels, depending on device density
import android.content.Context; import android.content.res.Resources; import android.util.DisplayMetrics; public class Main { /**/*from w w w . ja v a 2s.co m*/ * This method converts dp unit to equivalent pixels, depending on device * density. * * @param dp * A value in dp (density independent pixels) unit. Which we need to * convert into pixels * @param context * Context to get resources and device specific display metrics * @return A float value to represent px equivalent to dp depending on device * density */ public static float convertDpToPixel(float dp, Context context) { Resources resources = context.getResources(); DisplayMetrics metrics = resources.getDisplayMetrics(); float px = dp * (metrics.densityDpi / 160f); return px; } }