Here you can find the source of pxToDp(Context context, int pxValue)
Parameter | Description |
---|---|
context | The context of operation |
pxValue | The px value to be converted to dp |
public static int pxToDp(Context context, int pxValue)
//package com.java2s; import android.content.Context; import android.util.DisplayMetrics; public class Main { /**//w w w .ja v a 2 s . co m * Convert px value to dp value * @param context The context of operation * @param pxValue The px value to be converted to dp * @return The dp value */ public static int pxToDp(Context context, int pxValue) { DisplayMetrics displayMetrics = context.getResources() .getDisplayMetrics(); return (int) ((pxValue / displayMetrics.density) + 0.5); } }