Android 获取屏幕信息
获取屏幕信息:状态栏高度、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; }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。