DensityUtil.java package com.aotom.dvr.utils;import android.content.Context;import android.util.TypedValue;/** * * @author ckb58 */@SuppressWarnings({"DefaultFileTemplate", "JavaDoc", "unused"})public class DensityUtil { private DensityUtil
package com.aotom.dvr.utils; import android.content.Context; import android.util.TypedValue; /** * * @author ckb58 */ @SuppressWarnings({"DefaultFileTemplate", "JavaDoc", "unused"}) public class DensityUtil { private DensityUtil() { //不能实例化的的异常 throw new UnsupportedOperationException("cannot be instantiated"); } /** * dp 转 px * * @param context * @param dp * @return */ public static int dpToPx(Context context, float dp) { return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context .getResources().getDisplayMetrics()); } /** * sp 转 px * * @param context * @param sp * @return */ public static int spToPx(Context context, float sp) { return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, context .getResources().getDisplayMetrics()); } /** * px 转 dp * * @param context * @param px * @return */ public static float pxToDp(Context context, float px) { return (px / context.getResources().getDisplayMetrics().density); } /** * px 转 sp * * @param context * @param px * @return */ public static float pxToSp(Context context, float px) { return (px / context.getResources().getDisplayMetrics().scaledDensity); } }