public class DensityUtil { public static int sp2px(Context context, int spValue) { float pxDimension = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, spValue, context.getResources().getDisplayMetrics()); return (int) pxDimension; } public static int dip2px(Context context, int dp) { float pxDimension = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics()); return (int) pxDimension; } }