1 public static int dip2px(Context context, float dipValue){ 2 final float scale = context.getResources().getDisplayMetrics().density; 3 return (int)(dipValue * (scale + 0.5f)); 4 } 5 6 public static int px2dip(Context context, float pxValue){ 7 final float scale = context.getResource().getDisplayMetrics().density; 8 return (int)(pxValue / (scale + 0.5f)); 9 }