android截取屏幕图

Java代码 复制代码  收藏代码
  1. import java.io.FileNotFoundException;   
  2. import java.io.FileOutputStream;   
  3. import java.io.IOException;   
  4. import android.app.Activity;   
  5. import android.graphics.Bitmap;   
  6. import android.graphics.Rect;   
  7. import android.os.Environment;   
  8. import android.view.View;   
  9. //获取指定Activity的截屏,保存到png文件   
  10. public class ScreenShot {   
  11.        
  12.     private static Bitmap takeScreenShot(Activity activity) {   
  13.         // View是你需要截图的View   
  14.         View view = activity.getWindow().getDecorView();   
  15.         view.setDrawingCacheEnabled(true);   
  16.         view.buildDrawingCache();   
  17.         Bitmap b1 = view.getDrawingCache();   
  18.         // 获取状态栏高度   
  19.         Rect frame = new Rect();   
  20.         activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);   
  21.         int statusBarHeight = frame.top;   
  22.         System.out.println(statusBarHeight);   
  23.         // 获取屏幕长和高   
  24.         int width = activity.getWindowManager().getDefaultDisplay().getWidth();   
  25.         int height = activity.getWindowManager().getDefaultDisplay()   
  26.                 .getHeight();   
  27.         // 去掉标题栏 //Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);   
  28.         Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height   
  29.                 - statusBarHeight);   
  30.         view.destroyDrawingCache();   
  31.         return b;   
  32.     } // 保存到sdcard   
  33.   
  34.     private static void savePic(Bitmap b, String strFileName) {   
  35.   
  36.         FileOutputStream fos = null;   
  37.         try {   
  38.             fos = new FileOutputStream(strFileName);   
  39.             if (null != fos) {   
  40.                 b.compress(Bitmap.CompressFormat.PNG, 90, fos);   
  41.                 fos.flush();   
  42.                 fos.close();   
  43.             }   
  44.         } catch (FileNotFoundException e) {   
  45.             e.printStackTrace();   
  46.         } catch (IOException e) {   
  47.             e.printStackTrace();   
  48.         }   
  49.     }   
  50.   
  51.     // 程序入口   
  52.     public static void shoot(Activity a) {   
  53.         ScreenShot.savePic(ScreenShot.takeScreenShot(a), Environment.getExternalStorageDirectory()+"/xx.png");   
  54.     }   
  55. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值