千锋教育论坛

查看: 1551|回复: 0

Android手机截屏代码

[复制链接]

12

主题

192

帖子

433

积分

牛人

Rank: 3Rank: 3

发表于 16-5-27 13:43:36 | 显示全部楼层 |阅读模式
  Android手机截屏代码
  1. 首先写个工具类:
  2. public class ScreenShotTool {
  3. private Activity activity;
  4. public ScreenShotTool(Activity activity) {
  5. this.activity = activity;
  6. }
  7. public Bitmap getActivityBitmap() {
  8. /**取得DecorView,这个View是你需要截图的界面*/
  9. View view = activity.getWindow().getDecorView();
  10. view.setDrawingCacheEnabled(true);
  11. /**建立图片缓存*/
  12. view.buildDrawingCache();
  13. Bitmap b1 = view.getDrawingCache();
  14. /**获取状态栏高度*/
  15. Rect frame = new Rect();
  16. activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
  17. int statusBarHeight = frame.top;
  18. /**获取屏幕宽和高*/
  19. int width = activity.getWindowManager().getDefaultDisplay().getWidth();
  20. int height = activity.getWindowManager().getDefaultDisplay().getHeight();
  21. /**保存前去掉标题栏*/
  22. Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height - statusBarHeight);
  23. /**清除缓存*/
  24. view.destroyDrawingCache();
  25. return b;
  26. }
  27. /**保存到本地*/
  28. private void compressBitmap(Bitmap b, File filePath) {
  29. FileOutputStream fos = null;
  30. try {
  31. fos = new FileOutputStream(filePath);
  32. if (null != fos) {
  33. /**对图片进行压缩*/
  34. b.compress(Bitmap.CompressFormat.PNG, 100, fos);
  35. fos.flush();
  36. fos.close();
  37. }
  38. }
  39. catch (Exception e) {
  40. e.printStackTrace();
  41. }
  42. }
  43. public void saveScreenshopBitmap(File filePath) {
  44. if (filePath == null) { return; }
  45. if (!filePath.exists()) {
  46. try {
  47. filePath.createNewFile();
  48. }
  49. catch (IOException e) {
  50. // TODO Auto-generated catch block
  51. e.printStackTrace();
  52. }
  53. }
  54. compressBitmap(getActivityBitmap(), filePath);
  55. }
  56. }
  57. --------------在activit中使用-----------
  58. public class ScreenShotActivity extends Activity {
  59. private Button saveBtn;
  60. private Activity act;
  61. @Override
  62. protected void onCreate(Bundle savedInstanceState) {
  63. // TODO Auto-generated method stub
  64. super.onCreate(savedInstanceState);
  65. setContentView(R.layout.activity_photo);
  66. saveBtn=(Button) findViewById(R.id.photo_btn);
  67. act=this;
  68. saveBtn.setOnClickListener(new OnClickListener() {
  69. @Override
  70. public void onClick(View v) {
  71. /**保存路径*/
  72. String savePath = Environment.getExternalStorageDirectory()+"/ldm/myImages";
  73. try {
  74. File filePath = new File(savePath);
  75. if(!filePath.exists()){
  76. filePath.mkdirs();
  77. }
  78. /**文件路径*/
  79. String filepath = savePath + "/today.png";
  80. File file = new File(filepath);
  81. if (!file.exists()) {
  82. file.createNewFile();
  83. }
  84. /**把当前Activity截屏,也可以传入其它Activity*/
  85. new ScreenShotTool(act).saveScreenshopBitmap(file);
  86. }catch(Exception e){
  87. e.printStackTrace();
  88. }
  89. }
  90. });
  91. }
  92. }
复制代码


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

联系我们

电话:400-654-7778
交流群:523516910

点击咨询

学习资料

千锋教育

订阅|小黑屋|手机版|千锋教育论坛 ( 京ICP备12003911号-3 

GMT+8, 20-1-28 15:33 , Processed in 0.279462 second(s), 36 queries .

Powered by 千锋教育 X3.2

© 2001-2015

快速回复 返回顶部 返回列表