Java tutorial
//package com.java2s; import android.content.*; public class Main { public static int dip2px(Context context, float px) { final float scale = getScreenDensity(context); return (int) (px * scale + 0.5); } public static float getScreenDensity(Context context) { return context.getResources().getDisplayMetrics().density; } }