首页 > 系统 > Android > 正文

Android编程之截屏实现方法(包括scrollview与listview)

2019-10-24 20:34:13
字体:大 中 小
来源:转载
供稿:网友

这篇文章主要介绍了Android编程之截屏实现方法,包括截取scrollview与listview屏幕的相关技巧,以及截屏图片的生成与保存技巧,需要的朋友可以参考下

本文实例讲述了Android编程之截屏实现方法。分享给大家供大家参考,具体如下:

 

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

希望本文所述对大家Android程序设计有所帮助。


注:相关教程知识阅读请移步到Android开发频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表