package cn.trinea.android.common.util; import android.content.Context; /** * ScreenUtils * <ul> * <strong>Convert between dp and sp</strong> * <li>{@link cn.trinea.android.common.util.ScreenUtils#dpToPx(android.content.Context, float)}</li> * <li>{@link cn.trinea.android.common.util.ScreenUtils#pxToDp(android.content.Context, float)}</li> * </ul> * * @author <a href="http://www.trinea.cn" target="_blank">Trinea</a> 2014-2-14 */ public class ScreenUtils { private ScreenUtils() { throw new AssertionError(); } public static float dpToPx(Context context, float dp) { if (context == null) { return -1; } return dp * context.getResources().getDisplayMetrics().density; } public static float pxToDp(Context context, float px) { if (context == null) { return -1; } return px / context.getResources().getDisplayMetrics().density; } public static float dpToPxInt(Context context, float dp) { return (int)(dpToPx(context, dp) + 0.5f); } public static float pxToDpCeilInt(Context context, float px) { return (int)(pxToDp(context, px) + 0.5f); } }