Open itgoyo opened 7 years ago
import android.app.Activity; import android.content.Context; import android.util.DisplayMetrics; /** * dp、sp 转换为 px 的工具类 * * @author fxsky 2012.11.12 */ public class DisplayUtil { /** * 将px值转换为dip或dp值,保证尺寸大小不变 * <p/> * (DisplayMetrics类中属性density) * * @return */ public static int px2dip(Context context, float pxValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f); } /** * 将dip或dp值转换为px值,保证尺寸大小不变 * <p/> * (DisplayMetrics类中属性density) * * @return */ public static int dip2px(Context context, float dipValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dipValue * scale + 0.5f); } /** * 将px值转换为sp值,保证文字大小不变 * <p/> * (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值,保证文字大小不变 * <p/> * (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 context */ public static int getWindowWidth(Activity context){ DisplayMetrics metric = new DisplayMetrics(); context.getWindowManager().getDefaultDisplay().getMetrics(metric); return metric.widthPixels; } /** * 屏幕高度(像素) * @param context */ public static int getWindowHeight(Activity context){ DisplayMetrics metric = new DisplayMetrics(); context.getWindowManager().getDefaultDisplay().getMetrics(metric); return metric.heightPixels; } }