Android 获取屏幕信息
2015-06-04 14:38
423 查看
获取屏幕信息:状态栏高度、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; }
相关文章推荐
- Android 属性动画简介
- Android换肤技术
- Android设置界面切换动画的两种方式
- Android常用开源组件
- android Canvas drawText 文字居中
- SeAndroid上指纹系统的权限说明
- Android自定义控件实战——仿淘宝商品浏览界面
- android 自定义Adapter
- 抽取android4.4原生浏览器的PieMenu并修改为所需的功能
- android studio下gradle与Git出错解决方法
- android 动态设置控件的高度,使用对应布局中的dp值
- Android手机SD卡文件或目录拷贝、复制、粘贴功能实现代码
- 使用Ant批量打包Android渠道包
- 冷冰:Android核心分析系列
- android collection.sort()根据时间排序list
- Intent scheme URL attack
- Android Studio 导入OSChina客户端
- Android手机SD卡文件或目录拷贝、复制、粘贴功能实现代码
- Android SDK Manager更新版慢解决办法
- Android-Layout 布局属性 & 参数标签 集合