android屏幕截图

android屏幕截图  

  1. import java.io.FileNotFoundException;   
  2. import java.io.FileOutputStream;   
  3. import java.io.IOException;   
  4.   
  5. import android.app.Activity;   
  6. import android.graphics.Bitmap;   
  7. import android.graphics.Rect;   
  8. import android.view.View;   
  9.   
  10. public class ScreenShot {   
  11.     // 获取指定Activity的截屏,保存到png文件   
  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.         //获取状态栏高度   
  20.         Rect frame = new Rect();     
  21.         activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);     
  22.         int statusBarHeight = frame.top;     
  23.         System.out.println(statusBarHeight);   
  24.            
  25.         //获取屏幕长和高   
  26.         int width = activity.getWindowManager().getDefaultDisplay().getWidth();     
  27.         int height = activity.getWindowManager().getDefaultDisplay().getHeight();     
  28.         //去掉标题栏   
  29.         //Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);   
  30.         Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height - statusBarHeight);   
  31.         view.destroyDrawingCache();   
  32.         return b;   
  33.     }   
  34.        
  35.     //保存到sdcard   
  36.     private static void savePic(Bitmap b,String strFileName){   
  37.         FileOutputStream fos = null;   
  38.         try {   
  39.             fos = new FileOutputStream(strFileName);   
  40.             if (null != fos)   
  41.             {   
  42.                 b.compress(Bitmap.CompressFormat.PNG, 90, fos);   
  43.                 fos.flush();   
  44.                 fos.close();   
  45.             }   
  46.         } catch (FileNotFoundException e) {   
  47.             e.printStackTrace();   
  48.         } catch (IOException e) {   
  49.             e.printStackTrace();   
  50.         }   
  51.     }   
  52.        
  53.     //程序入口   
  54.     public static void shoot(Activity a){   
  55.         ScreenShot.savePic(ScreenShot.takeScreenShot(a), "sdcard/xx.png");   
  56.     }   
  57. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值