Here you can find the source of convertPX2DIP(Context context, float px)
public static int convertPX2DIP(Context context, float px)
//package com.java2s; import android.content.Context; import android.content.res.Resources; public class Main { public static int convertPX2DIP(Context context, float px) { Resources resources = context.getResources(); float scale = resources.getDisplayMetrics().density; resources = null;//from ww w. j a v a 2 s .com return (int) (px / scale + 0.5f * (px >= 0 ? 1 : -1)); } }