现在无论是应用,还是游戏中,都经常会有分享的功能。分享,不仅要分享文字,也要分享应用或者游戏的屏幕截图,这样才能做到图文并茂,吸引到更多的用户。

想要做图片的分享功能,首先就需要抓屏,将当前屏幕保存为一个图片文件。

下面就是一个将当前的Activity直接保存为一个图片文件的类库,可以直接使用。需要的,直接拿来主义就好了。


1. package
2.
3. import
4. import
5. import
6. import
7.
8. import
9. import
10. import
11. import
12.
13. public class
14.
15. private static
16. // View是你需要截图的View
17. View view = activity.getWindow().getDecorView();
18. true);
19. view.buildDrawingCache();
20. Bitmap b1 = view.getDrawingCache();
21.
22. // 获取状态栏高度
23. new
24. activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
25. int
26.
27. // 获取屏幕长和高
28. int
29. int
30. .getHeight();
31. // 去掉标题栏
32. 0, statusBarHeight, width, height
33. - statusBarHeight);
34. view.destroyDrawingCache();
35. return
36. }
37.
38. private static void
39. null;
40. try
41. new
42. if (null
43. 100, fos);
44. fos.flush();
45. fos.close();
46. }
47. catch
48. // e.printStackTrace();
49. catch
50. // e.printStackTrace();
51. }
52. }
53.
54. public static void
55. if (filePath == null) {
56. return;
57. }
58. if
59. filePath.getParentFile().mkdirs();
60. }
61. ScreenShot.savePic(ScreenShot.takeScreenShot(a), filePath);
62. }
63. }