获取当前屏幕截图的Bitmap对象,然后就可以拿去做你想做的事情,代码很简单。

    public static Bitmap captureScreen(Activity activity) {activity.getWindow().getDecorView().setDrawingCacheEnabled(true);Bitmap bmp=activity.getWindow().getDecorView().getDrawingCache();return bmp;}

通过API文档来解释一下。

    public Window getWindow ()                      Added in API level 1Retrieve the current Window for the activity. This can be used to directly access parts of the Window API that are not available through Activity Scre    en.ReturnsWindow The current window, or null if the activity is not visual.

Activity的getWindow()方法,得到activity的当前窗口,返回一个Window对象

 public abstract View getDecorView ()                 Added in API level 1Retrieve the top-level window decor view (containing the standard window frame/decorations and the client's content inside of that), which can be added   as a window to the window manager.Note that calling this function for the first time "locks in" various window characteristics as described in setContentView(View, android.view.ViewGroup. LayoutParams).Returns the top-level window decor view.

Window的getDecorView()方法,得到当前Window的最顶层View,返回一个View对象

 public void setDrawingCacheEnabled (boolean enabled)               Added in API level 1Enables or disables the drawing cache. When the drawing cache is enabled, the next call to getDrawingCache() or buildDrawingCache() will draw the view in a bitmap. Calling draw(android.graphics.Canvas) will not draw from the cache when the cache is enabled. To benefit from the cache, you must request the d rawing cache by calling getDrawingCache() and draw it on screen if the returned bitmap is not null.Enabling the drawing cache is similar to setting a layer when hardware acceleration is turned off. When hardware acceleration is turned on, enabling the  drawing cache has no effect on rendering because the system uses a different mechanism for acceleration which ignores the flag. If you want to use a Bitm ap for the view, even when hardware acceleration is enabled, see setLayerType(int, android.graphics.Paint) for information on how to enable software and  hardware layers.This API can be used to manually generate a bitmap copy of this view, by setting the flag to true and calling getDrawingCache().Parametersenabled   true to enable the drawing cache, false otherwise

View的setDrawingCacheEnabled()方法,设置为true,开启了cache,意思就是generate a bitmap copy of this view,允许生成对当前view的一个bitmap形式的复 制,这 样才可以调用getDrawingCache()方法。

 public Bitmap getDrawingCache ()                                 Added in API level 1Calling this method is equivalent to calling getDrawingCache(false).ReturnsA non-scaled bitmap representing this view or null if cache is disabled.

View的getDrawingCache()方法,在设置setDrawingCacheEnabled(true)之后,会对这个View进行保存生成bitmap,这个方法会将这个bitmap返回。

这样就完成了对当前屏幕的截图操作,拿到bitmap之后,我们就可以进行显示或者保存等操作了。

有任何疑问,欢迎加群讨论:261386924

Android获取当前屏幕截图,简单粗暴!相关推荐

  1. android获取ro._Android 简单的设备信息获取

    在用python写自动化测试脚本的脚本的时候通常回需要获取一些android设备的硬件信息.网上给的很多方案都是通常通过/system/build.prop中去获取,但是回遇到permission d ...

  2. linux卸载android studio,mac上完整卸载删除.简单粗暴无脑:androidstudio删除方案

    如果你是mac  ,你删除as ,删不干净也正常,你会发现安装的时候,前面的东西也在.配置文件在,会导致你以前的错误不想要的东西都在. 废话不多说,复制粘贴就是干!!!!~~~~~~~~ 第一步: 复 ...

  3. Android Studio 基础 之 简单获取手机端所有图片

    Android Studio 基础 之 简单获取手机端所有图片 目录 Android Studio 基础 之 简单获取手机端所有图片 一.简单介绍 二.实现原理 三.注意实现 四.预览效果 五.实现步 ...

  4. android 适配简单粗暴,Android10填坑适配指南(实际经验代码)

    Android10填坑适配指南(实际经验代码) 发布时间:2019-12-03 09:40 来源:互联网 当前栏目:web技术类 今天看到一篇好的文章,分享给大家,膜拜大佬. Android10填坑适 ...

  5. jq获取表格里的checkbox_Python读写Excel表格,就是这么简单粗暴又好用

    最近在做一些数据处理和计算的工作,因为数据是以.CSV格式保存的,因此刚开始直接用Excel来处理. 但是做着做着发现重复的劳动,其实并没有多大的意义,于是就想着写个小工具帮着处理. 以前正好在一本书 ...

  6. android获取海拔代码,android获取经度、维度、海拔简单代码

    android获取经度.维度.海拔 import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.uti ...

  7. 简单粗暴上手TensorFlow 2.0,北大学霸力作,必须人手一册!

    (图片付费下载自视觉中国) 整理 | 夕颜 出品 | AI科技大本营(ID:rgznai100) [导读] TensorFlow 2.0 于近期正式发布后,立即受到学术界与科研界的广泛关注与好评.此前 ...

  8. Android PC投屏简单尝试- 自定义协议章(Socket+Bitmap)

    代码地址 :https://github.com/deepsadness/MediaProjectionDemo 效果预览 投屏效果预览 简单说明: 使用Android MediaProjection ...

  9. android 获取屏幕的宽高

    今天,讲讲android如何获取屏幕的宽高. // 通过WindowManager获取 DisplayMetrics dm = new DisplayMetrics(); getWindowManag ...

最新文章

  1. python⾯向对象学员管理系统
  2. java 白皮书_java第三天学习内容回顾——java白皮书第四章
  3. linux ftp 怎么验证,Linux编译安装vsftpd服务器(本地用户验证方式)
  4. PHP获取IP地址的方法,防止伪造IP地址注入攻击
  5. mysql声明declare_mysql8 参考手册--DECLARE ... CONDITION声明
  6. PHP设置按钮不可用,css怎么控制按钮不可用,button按钮css样式
  7. Ubuntu 下 libev编译安装
  8. poj 1679(次小生成树)
  9. 转速开环恒压频比异步电动机调速系统仿真
  10. FAT32 文件系统详解
  11. bat 脚本操作注册表
  12. Linu中的网络配置(设置ip,网关(路由功能),dns),虚拟机上网
  13. NLP入门学习3——句法分析(基于LTP4)
  14. C++程序运算时间计算
  15. 2015阿里移动推荐算法大赛总结
  16. php获取文件夹下所有文件名(php遍历目录)
  17. 关于在POI以SAX方式解析,会导出拼音(音标)的问题解决
  18. 【vue】生成二维码
  19. Word·去掉复制粘贴自动添加的空格
  20. python模拟太阳系_用Unity3D实现太阳系仿真

热门文章

  1. 如何用人工智能秒杀超级马里奥游戏
  2. 亚马逊新近搜索类图书印象(上)(转)
  3. SD卡照片删除怎么恢复?教程全解
  4. Oracle 12warehouse,warehouse提供的免费oracle学习视频
  5. 3d文本样式cssjs特效代码
  6. MSN Messenger无法登陆
  7. 各城市城市编码_精致的城市
  8. 【Idea】JUnit单元测试%MODULE_WORKING_DIR%' does not exist
  9. Diskgenius专业版英文破解(绝对能用)
  10. windows安装 node-sass sass-loader