03 | import java.io.FileNotFoundException; |
04 | import java.io.FileOutputStream; |
05 | import java.io.IOException; |
07 | import android.app.Activity; |
08 | import android.graphics.Bitmap; |
09 | import android.graphics.Rect; |
10 | import android.view.View; |
12 | public class ScreenShot
{ |
13 | //
获取指定Activity的截屏,保存到png文件 |
14 | private static Bitmap
takeScreenShot(Activity activity){ |
16 | View
view = activity.getWindow().getDecorView(); |
17 | view.setDrawingCacheEnabled( true ); |
18 | view.buildDrawingCache(); |
19 | Bitmap b1 = view.getDrawingCache(); |
22 | Rect
frame = new Rect(); |
23 | activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); |
24 | int statusBarHeight
= frame.top; |
25 | System.out.println(statusBarHeight); |
28 | int width = activity.getWindowManager().getDefaultDisplay().getWidth(); |
29 | int height = activity.getWindowManager().getDefaultDisplay().getHeight(); |
31 | //Bitmap
b = Bitmap.createBitmap(b1,0, 25, 320, 455); |
32 | Bitmap
b = Bitmap.createBitmap(b1, 0 , statusBarHeight, width, height - statusBarHeight); |
33 | view.destroyDrawingCache(); |
38 | private static void savePic(Bitmap
b,String strFileName){ |
39 | FileOutputStream
fos = null ; |
41 | fos
= new FileOutputStream(strFileName); |
44 | b.compress(Bitmap.CompressFormat.PNG, 90 , fos); |
48 | } catch (FileNotFoundException e) { |
50 | } catch (IOException e) { |
56 | public static void shoot(Activity a){ |
57 | ScreenShot.savePic(ScreenShot.takeScreenShot(a), "sdcard/xx.png" ); |
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理