// 獲取指定Activity的截屏嫌蚤,保存到png文件
private static Bitmap takeScreenShot(Activity activity) {
// View是你需要截圖的View
? ? View view = activity.getWindow().getDecorView();
? ? view.setDrawingCacheEnabled(true);
? ? view.buildDrawingCache();
? ? Bitmapb1 =view.getDrawingCache();
? ? // 獲取狀態(tài)欄高度
? ? Rectframe =new Rect();
? ? activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
? ? int statusBarHeight =frame.top;
? ? Log.i("TAG", "" +statusBarHeight);
? ? // 獲取屏幕長(zhǎng)和高
? ? int width = activity.getWindowManager().getDefaultDisplay().getWidth();
? ? int height = activity.getWindowManager().getDefaultDisplay().getHeight();
? ? // 去掉標(biāo)題欄
// Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);
? ? Bitmapb =Bitmap.createBitmap(b1, 0, statusBarHeight, width, height
? ? ? ? ? ? -statusBarHeight);
? ? view.destroyDrawingCache();
? ? return b;
}
// 保存指定位置
private static void savePic(Bitmap b, String strFileName) {
try {
FileOutputStream fos =new FileOutputStream(strFileName);
? ? ? ? ? ? b.compress(Bitmap.CompressFormat.PNG, 90, fos);
? ? ? ? ? ? fos.flush();
? ? ? ? ? ? fos.close();
? ? }catch (Exception e) {
e.printStackTrace();
? ? }
}