1. public class ScreenShot {
  2. // 获取指定Activity的截屏,保存到png文件
  3. public static Bitmap takeScreenShot(Activity activity) {
  4. // View是你需要截图的View
  5. View view = activity.getWindow().getDecorView();
  6. view.setDrawingCacheEnabled(true);
  7. view.buildDrawingCache();
  8. Bitmap b1 = view.getDrawingCache();
  9.  
  10. // 获取状态栏高度
  11. Rect frame = new Rect();
  12. activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
  13. int statusBarHeight = frame.top;
  14. System.out.println(statusBarHeight);
  15.  
  16. // 获取屏幕长和高
  17. int width = activity.getWindowManager().getDefaultDisplay().getWidth();
  18. int height = activity.getWindowManager().getDefaultDisplay()
  19. .getHeight();
  20. // 去掉标题栏
  21. // Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);
  22. Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height
  23. - statusBarHeight);
  24. view.destroyDrawingCache();
  25. savePic(b, "/sdcard/screen_test.png");
  26. return b;
  27. }
  28.  
  29. // 保存到sdcard
  30. public static void savePic(Bitmap b, String strFileName) {
  31. FileOutputStream fos = null;
  32. try {
  33. fos = new FileOutputStream(strFileName);
  34. if (null != fos) {
  35. b.compress(Bitmap.CompressFormat.PNG, 90, fos);
  36. fos.flush();
  37. fos.close();
  38. }
  39. } catch (FileNotFoundException e) {
  40. e.printStackTrace();
  41. } catch (IOException e) {
  42. e.printStackTrace();
  43. }
  44. }
  45.  
  46. /**
  47. * 把View对象转换成bitmap
  48. * */
  49. public static Bitmap convertViewToBitmap(View view) {
  50. view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
  51. MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
  52. view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
  53. view.buildDrawingCache();
  54. Bitmap bitmap = view.getDrawingCache();
  55. if (bitmap != null) {
  56. System.out.println("这不是nullde1");
  57. Log.d("nullde1", "nullde1");
  58. } else {
  59. System.out.println("这nullnulllnulnlul");
  60. }
  61. return bitmap;
  62. }
  63.  
  64. // 程序入口1
  65. public static void shoot(Activity a) {
  66. ScreenShot.savePic(ScreenShot.takeScreenShot(a), "/sdcard/screen_test.png");
  67. }
  68.  
  69. // 程序入口2
  70. public static void shootView(View view) {
  71. ScreenShot.savePic(ScreenShot.convertViewToBitmap(view),
  72. "sdcard/xx.png");
  73. }
  74.  
  75. public static Bitmap getViewBitmap(View v) {
  76. v.clearFocus();
  77. v.setPressed(false);
  78.  
  79. boolean willNotCache = v.willNotCacheDrawing();
  80. v.setWillNotCacheDrawing(false);
  81.  
  82. // Reset the drawing cache background color to fully transparent
  83. // for the duration of this operation
  84. int color = v.getDrawingCacheBackgroundColor();
  85. v.setDrawingCacheBackgroundColor(0);
  86.  
  87. if (color != 0) {
  88. v.destroyDrawingCache();
  89. }
  90. v.buildDrawingCache();
  91. Bitmap cacheBitmap = v.getDrawingCache();
  92. if (cacheBitmap == null) {
  93. Log.e("TTTTTTTTActivity", "failed getViewBitmap(" + v + ")",
  94. new RuntimeException());
  95. return null;
  96. }
  97.  
  98. Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
  99.  
  100. // Restore the view
  101. v.destroyDrawingCache();
  102. v.setWillNotCacheDrawing(willNotCache);
  103. v.setDrawingCacheBackgroundColor(color);
  104.  
  105. return bitmap;
  106. }
  107.  
  108. /**
  109. * 截取scrollview的屏幕
  110. * **/
  111. public static Bitmap getBitmapByView(ScrollView scrollView) {
  112. int h = 0;
  113. Bitmap bitmap = null;
  114. // 获取listView实际高度
  115. for (int i = 0; i < scrollView.getChildCount(); i++) {
  116. h += scrollView.getChildAt(i).getHeight();
  117. scrollView.getChildAt(i).setBackgroundResource(R.drawable.bg3);
  118. }
  119. Log.d(TAG, "实际高度:" + h);
  120. Log.d(TAG, " 高度:" + scrollView.getHeight());
  121. // 创建对应大小的bitmap
  122. bitmap = Bitmap.createBitmap(scrollView.getWidth(), h,
  123. Bitmap.Config.ARGB_8888);
  124. final Canvas canvas = new Canvas(bitmap);
  125. scrollView.draw(canvas);
  126. // 测试输出
  127. FileOutputStream out = null;
  128. try {
  129. out = new FileOutputStream("/sdcard/screen_test.png");
  130. } catch (FileNotFoundException e) {
  131. e.printStackTrace();
  132. }
  133. try {
  134. if (null != out) {
  135. bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
  136. out.flush();
  137. out.close();
  138. }
  139. } catch (IOException e) {
  140. // TODO: handle exception
  141. }
  142. return bitmap;
  143. }
  144.  
  145. private static String TAG = "Listview and ScrollView item 截图:";
  146.  
  147. /**
  148. * 截图listview
  149. * **/
  150. public static Bitmap getbBitmap(ListView listView) {
  151. int h = 0;
  152. Bitmap bitmap = null;
  153. // 获取listView实际高度
  154. for (int i = 0; i < listView.getChildCount(); i++) {
  155. h += listView.getChildAt(i).getHeight();
  156. }
  157. Log.d(TAG, "实际高度:" + h);
  158. Log.d(TAG, "list 高度:" + listView.getHeight());
  159. // 创建对应大小的bitmap
  160. bitmap = Bitmap.createBitmap(listView.getWidth(), h,
  161. Bitmap.Config.ARGB_8888);
  162. final Canvas canvas = new Canvas(bitmap);
  163. listView.draw(canvas);
  164. // 测试输出
  165. FileOutputStream out = null;
  166. try {
  167. out = new FileOutputStream("/sdcard/screen_test.png");
  168. } catch (FileNotFoundException e) {
  169. e.printStackTrace();
  170. }
  171. try {
  172. if (null != out) {
  173. bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
  174. out.flush();
  175. out.close();
  176. }
  177. } catch (IOException e) {
  178. // TODO: handle exception
  179. }
  180. return bitmap;
  181. }
  182.  
  183. }

  

android之截屏(包括截取scrollview与listview的)的更多相关文章

  1. Android滚动截屏,ScrollView截屏

    在做分享功能的时候,需要截取全屏内容,一屏展示不完的内容,一般我们会用到 ListView 或 ScrollView 一: 普通截屏的实现 获取当前Window 的 DrawingCache 的方式, ...

  2. Android长截屏-- ScrollView,ListView及RecyclerView截屏

    http://blog.csdn.net/wbwjx/article/details/46674157       Android长截屏-- ScrollView,ListView及RecyclerV ...

  3. Android系统截屏的实现(附代码)

    1.背景                     写博客快两年了,写了100+的文章,最火的文章也是大家最关注的就是如何实现android系统截屏.其实我们google android_screen_ ...

  4. android后台截屏实现(2)--screencap源码修改

    首先找到screencap类在Android源码中的位置,/442/frameworks/base/cmds/screencap/screencap.cpp 源码如下: /* * Copyright ...

  5. Android 长截屏原理

    https://android-notes.github.io/2016/12/03/android%E9%95%BF%E6%88%AA%E5%B1%8F%E5%8E%9F%E7%90%86/   a ...

  6. Android手机截屏

    刚开始打算做一个简单的截屏程序时,以为很轻松就能搞定. 在Activity上放一个按钮,点击完成截屏操作,并将数据以图片形式保存在手机中. 动手之前,自然是看书和网上各种查资料.结果发现了解的知识越多 ...

  7. Android代码截屏

    本文来源:http://myhpu2008.iteye.com/blog/999779 这种方法应该只能对当前Activity本身进行截屏,因而你只能在你应用程序中参照该代码对其应用程序本身截屏. i ...

  8. android后台截屏实现(3)--编译screencap

    修改好之后就要编译了,screencap的编译是要在源码环境中进行的. 将修改后的screencap.cpp文件替换源码中的原始文件,然后修改screencap的Android.mk文件,修改后的文件 ...

  9. android手机截屏、录屏

    1. 手动截屏,通过其他第三方软件发送截图,或者从手机取出截图 2. 使用命令截图,将截图保存到手机,再拉取到电脑 #!/bin/sh #运行 sh screenshot name picName=$ ...

随机推荐

  1. mysql千万级表关联优化(2)

    概述: 交代一下背景,这算是一次项目经验吧,属于公司一个已上线平台的功能,这算是离职人员挖下的坑,随着数据越来越多,原本的SQL查询变得越来越慢,用户体验特别差,因此SQL优化任务交到了我手上. 这个 ...

  2. awk调用shell命令的两种方法:system与print

    from:http://www.oklinux.cn/html/developer/shell/20070626/31550.htmlawk中使用的shell命令,有2种方法: 一.使用所以syste ...

  3. 【51nod】1531 树上的博弈

    题解 我们发现每次决策的时候,我们可以判断某个点的决策,至少小于等于几个点或者至少大于等于几个点 我们求最大值 dp[u][1 / 0] dp[u][1]表示u这个点先手,至少大于等于几个点 dp[u ...

  4. 为mongodb数据库增加用户名密码权限

    加固mongodb建议:修改数据库默认端口,添加数据库访问权限: 启动数据库(裸奔):C:\mongodb\bin>mongod --dbpath C:\MongoDB\data(同时用--db ...

  5. [leetcode tree]98. Validate Binary Search Tree

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  6. HTTP/FTP压力测试工具siege

    HTTP/FTP压力测试工具siege   压力测试可以检测服务器的承载能力.针对HTTP和FTP服务,Kali Linux提供专项工具siege.该工具可以模拟多个用户同时访问同一个网站的多个网页, ...

  7. 关于socket知识整理

    一个完整的计算机系统是由硬件.操作系统.应用软件三者组成,具备了这三个条件,一台计算机系统就可以玩单机游戏.如果你想上网(访问个黄色网站,发个黄色微博啥的),就需要遵守网络协议,即计算机之间交流的标准 ...

  8. python中的super( test, self).__init__()

    python中的super( test, self).__init__() 对继承自父类的属性进行初始化 首先找到test的父类(比如是类A),然后把类test的对象self转换为类A的对象,然后“被 ...

  9. BZOJ 2286: [Sdoi2011]消耗战 虚树 树形dp 动态规划 dfs序

    https://www.lydsy.com/JudgeOnline/problem.php?id=2286 wa了两次因为lca犯了zz错误 这道题如果不多次询问的话就是裸dp. 一棵树上多次询问,且 ...

  10. ngx_lua应用最佳实践

    引子: 以下文字,是UPYUN系统开发工程师timebug在SegmentFault D-Day南京站技术沙龙上所做分享的内容要义提炼,主题为UPYUN系统开发团队在进行业务逻辑由C模块到ngx_lu ...