废话不多说直接上代码:
1.dip2px dp转px 无context算法
public static int px2dip(int pxValue) { final float scale = Resources.getSystem().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f); } public static float dip2px(float dipValue) { final float scale = Resources.getSystem().getDisplayMetrics().density; return (dipValue * scale + 0.5f); }
2.获取屏幕区域
/** * 获取屏幕区域 */ public static Rect getScreenRect() { DisplayMetrics displayMetric = new DisplayMetrics(); displayMetric = Resources.getSystem().getDisplayMetrics(); Rect rect = new Rect(0, 0, displayMetric.widthPixels, displayMetric.heightPixels); return rect; } /** * 获取屏幕宽度 * */ public static int getScreenWidth() { return getScreenRect().width(); } /** * 获取屏幕高度 * */ public static int getScreenHeight() { return getScreenRect().height(); }
时间: 2024-12-20 15:43:52