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

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


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