Android获取屏幕数据的工具类
2015-12-30 15:58
661 查看
直接上代码
package com.example; import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Rect; import android.util.DisplayMetrics; import android.view.View; import android.view.WindowManager; public class ScreenUtils { private ScreenUtils() { throw new UnsupportedOperationException( "---------------error-------------"); } /** * 获得屏幕高度 * * @param context * @return */ 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; } /** * 获得屏幕宽度 * * @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; } /** * 获得状态栏的高度 * * @param context * @return */ public static int getStatusHeight(Context context) { int statusHeight = -1; 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; } /** * 获取当前屏幕截图,包含状态栏 * * @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; } /** * 获取当前屏幕截图,不包含状态栏 * * @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; } }
相关文章推荐
- mac下android环境搭建笔记(android studio)
- android菜单Menu添加Icon无效问题(android4.0)
- Android下修改SeekBar样式
- Android之JavaSe基础--面向对象高级部分(六)
- Android IntentService源码解读
- Android之AlarmManager(全局定时器/闹钟)指定时长或以周期形式执行某项操作
- Android Studio 安装配置详细步骤,以及使用的基本操作
- Android 设置铃声,getContentResolver().insert returns null
- 详解Android中AsyncTask的使用
- android selector介绍
- Android 双进程Service常驻后台,无惧“一键清理”
- 如何在Android上编写高效的Java代码
- android studio install
- Android属性动画完全解析(上),初识属性动画的基本用法
- android的多线程讲解
- Android资源管理框架(Asset Manager)简要介绍
- android 动态改变字体大小
- Android编程获取系统隐藏服务实现锁屏的方法
- Android 自定义View -- 简约的折线图
- Android 自定义View -- 简约的折线图