获取屏幕信息:状态栏高度、content大小
public void getScreenInfo(final Activity activity){ final View v = activity.getWindow().findViewById(Window.ID_ANDROID_CONTENT); v.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { @Override public boolean onPreDraw() { Rect frame = new Rect(); activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); int statusBarHeight = frame.top; v.getViewTreeObserver().removeOnPreDrawListener(this); Log.e("", "ScreenInfo===ContentVIew===Height:" + v.getHeight() + " Width:" + v.getWidth() + " Top:" + v.getTop() + " Left:" + v.getLeft()+" Bottom:"+v.getBottom()+" 状态栏高度:"+statusBarHeight); return true; } }); }
其他的一些工具类:获取屏幕大小、密度、横屏竖屏
private static int widthPixels = 0; private static int heightPixels = 0; private static float density = 0; public static DisplayMetrics getMetrics(Context context) { DisplayMetrics metrics = new DisplayMetrics(); getWindowManager(context).getDefaultDisplay().getMetrics(metrics); return metrics; } public static String getOrientation(Context context) { switch (getWindowManager(context).getDefaultDisplay().getRotation()) { case Surface.ROTATION_0: return "portrait"; case Surface.ROTATION_90: return "landscape"; case Surface.ROTATION_180: return "reverse portrait"; default: return "reverse landscape"; } } public static WindowManager getWindowManager(Context context) { return (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); } public static int getWidth(Context context) { if (widthPixels == 0) { widthPixels = getMetrics(context).widthPixels; } return widthPixels; } public static int getHeight(Context context) { if (heightPixels == 0) { ViewConfiguration.get(context).hasPermanentMenuKey(); heightPixels = getMetrics(context).heightPixels; } return heightPixels; } public static float getDensity(Context context) { if (density == 0) { density = getMetrics(context).density; } return density; }
时间: 2024-10-12 20:20:18