Android 获取屏幕相关信息
来源:互联网 发布:无法进入linux系统 编辑:程序博客网 时间:2024/06/02 12:14
为了今后使用更加方便,下面整理一些常用的屏幕相关信息的方法
1. 获取屏幕高度
/** * 获得屏幕高度 * @param context */public static int getScreenWidth(Context context) { WindowManager wm = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(outMetrics); return outMetrics.widthPixels;}
2. 获取屏幕高度
/** * 获得屏幕宽度 * @param context * @return */public static int getScreenHeight(Context context) { WindowManager wm = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(outMetrics); return outMetrics.heightPixels;}
3. 获取状态栏的高度
/** * 获得状态栏的高度 * @param context * @return */public static int getStatusHeight(Context context) { int statusHeight; try { Class<?> clazz = Class.forName("com.android.internal.R$dimen"); Object object = clazz.newInstance(); int height = Integer.parseInt(clazz.getField("status_bar_height") .get(object).toString()); statusHeight = context.getResources().getDimensionPixelSize(height); } catch (Exception e) { e.printStackTrace(); } return statusHeight;}
4.获取导航栏高度
/** * 获取导航栏高度 * @param context * @return */public static int getNavigationBarHeight(Context context) { int resourceId; int rid = context.getResources().getIdentifier("config_showNavigationBar", "bool", "android"); if (rid!=0){ resourceId = context.getResources().getIdentifier("navigation_bar_height", "dimen", "android"); return context.getResources().getDimensionPixelSize(resourceId); }else return 0;}
5.获取当前屏幕截图,包含状态栏部分
/** * 获取当前屏幕截图,包含状态栏 * @param activity * @return */public static Bitmap snapShotWithStatusBar(Activity activity) { View view = activity.getWindow().getDecorView(); view.setDrawingCacheEnabled(true); view.buildDrawingCache(); Bitmap bmp = view.getDrawingCache(); int width = getScreenWidth(activity); int height = getScreenHeight(activity); Bitmap bp = null; bp = Bitmap.createBitmap(bmp, 0, 0, width, height); view.destroyDrawingCache(); return bp;}
6. 获取当前屏幕截图,不包含状态栏部分
/** * 获取当前屏幕截图,不包含状态栏 * @param activity * @return */public static Bitmap snapShotWithoutStatusBar(Activity activity) { View view = activity.getWindow().getDecorView(); view.setDrawingCacheEnabled(true); view.buildDrawingCache(); Bitmap bmp = view.getDrawingCache(); Rect frame = new Rect(); activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); int statusBarHeight = frame.top; int width = getScreenWidth(activity); int height = getScreenHeight(activity); Bitmap bp = null; bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height - statusBarHeight); view.destroyDrawingCache(); return bp;} private void snapshotForActivity() { View view = findViewById(android.R.id.content); boolean drawingCacheEnabled = view.isDrawingCacheEnabled(); view.setDrawingCacheEnabled(true); view.buildDrawingCache();// if (PREV_BACKGROUND != null)// PREV_BACKGROUND.recycle(); Bitmap drawingCache = view.getDrawingCache(); if (drawingCache != null && !drawingCache.isRecycled()) { Bitmap.createBitmap(drawingCache); } view.setDrawingCacheEnabled(drawingCacheEnabled); }
7. 展开、收起状态栏
(1) 收起状态栏
可用于点击Notifacation之后收起状态栏
/** * 收起状态栏 * @param activity * @return */public static final void collapseStatusBar(Context context) { Object sbservice = context.getSystemService("statusbar"); try { Class<?> statusBarManager = Class.forName("android.app.StatusBarManager"); Method collapse; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { collapse = statusBarManager.getMethod("collapsePanels"); } else { collapse = statusBarManager.getMethod("collapse"); } collapse.invoke(sbservice); } catch (Exception e) { e.printStackTrace(); }}
(2) 展开状态栏
/** * 展开状态栏 * @param activity * @return */public static final void expandStatusBar(Context context) { Object sbservice = context.getSystemService("statusbar"); try { Class<?> statusBarManager = Class.forName("android.app.StatusBarManager"); Method expand; if (Build.VERSION.SDK_INT >= 17) { expand = statusBarManager.getMethod("expandNotificationsPanel"); } else { expand = statusBarManager.getMethod("expand"); } expand.invoke(sbservice); } catch (Exception e) { e.printStackTrace(); }}
持续更新…
0 0
- Android 获取屏幕相关信息
- android获取屏幕相关信息
- android中获取屏幕相关信息
- android中获取屏幕相关信息
- 获取屏幕相关信息
- 获取屏幕相关信息
- 获取屏幕android屏幕信息
- android获取屏幕信息
- Android 获取屏幕信息
- Android 获取屏幕信息
- android 获取屏幕信息
- android获取屏幕信息
- Android中获取屏幕相关信息(屏幕大小,状态栏、标题栏高度)
- Android中获取屏幕相关信息(屏幕大小,状态栏、标题栏高度)
- Android中获取屏幕相关信息(屏幕大小,状态栏、标题栏高度)
- Android中获取屏幕相关信息(屏幕大小,状态栏、标题栏高度)
- Android中获取屏幕相关信息(屏幕大小,状态栏、标题栏高度)
- Android中获取屏幕相关信息(屏幕大小,状态栏、标题栏高度)
- Scrapy ImportError: No module named items
- 蓝牙广播数据格式和动态改变
- vc2010编译boost1.63.0
- poj2246 stl
- java 动态代理
- Android 获取屏幕相关信息
- HDU1877 又一版A+B 浙大计算机研究生复试上机考试-2008年
- 事务隔离级别
- Linux相关网络资源收集
- Java中多进程程序优先级的设定与使用
- RSA javax.crypto.BadPaddingException: Blocktype mismatch: 0 错误
- Linux系统程序自动运行三法
- Java-----Socket中read方法什么时候返回-1
- 2017华为暑期实习生招聘真题(3月24日)