package com.gh.common.util; import android.content.Context; import android.content.res.Resources; import com.halo.assistant.HaloApp; public class DisplayUtils { /** * 根据手机的分辨率从 dip(像素) 的单位 转成为 px */ public static int dip2px(Context context, float dpValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } /** * 根据手机的分辨率从 px(像素) 的单位 转成为 dip */ public static int px2dip(Context context, float pxValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f); } /** * 根据手机的分辨率从 dip(像素) 的单位 转成为 px */ public static int dip2px(float dpValue) { final float scale = HaloApp.getInstance().getApplication().getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } /** * 将px值转换为sp值,保证文字大小不变 * * @param pxValue * @param pxValue (DisplayMetrics类中属性scaledDensity) * @return */ public static int px2sp(Context context, float pxValue) { final float fontScale = context.getResources().getDisplayMetrics().scaledDensity; return (int) (pxValue / fontScale + 0.5f); } /** * 将sp值转换为px值,保证文字大小不变 * * @param spValue * @param spValue (DisplayMetrics类中属性scaledDensity) * @return */ public static int sp2px(Context context, float spValue) { final float fontScale = context.getResources().getDisplayMetrics().scaledDensity; return (int) (spValue * fontScale + 0.5f); } /** * 获取状态栏的高度 * * @param resources 资源 * @return height */ public static int getStatusBarHeight(Resources resources) { return getInternalDimensionSize(resources, "status_bar_height"); } public static int getInternalDimensionSize(Resources res, String key) { int result = 0; int resourceId = res.getIdentifier(key, "dimen", "android"); if (resourceId > 0) { result = res.getDimensionPixelSize(resourceId); } return result; } }