Here you can find the source of getPixelFromDp(float dp, Context context)
Parameter | Description |
---|---|
dp | A value in dp (density independent pixels) unit. Which we need to convert into pixels |
context | Context to get resources and device specific display metrics |
public static float getPixelFromDp(float dp, Context context)
//package com.java2s; //License from project: Open Source License import android.content.Context; import android.content.res.Resources; import android.util.DisplayMetrics; public class Main { /**/* ww w . ja v a2s .com*/ * 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 getPixelFromDp(float dp, Context context) { Resources resources = context.getResources(); DisplayMetrics metrics = resources.getDisplayMetrics(); return dp * (metrics.densityDpi / 160f); } }