Android普通截屏(不包括状态栏内容但有状态栏占位)

来源:互联网 发布:window python编译器 编辑:程序博客网 时间:2024/05/16 17:44
    public static Bitmap normalShot(Activity activity) {        View decorView = activity.getWindow().getDecorView();        decorView.setDrawingCacheEnabled(true);        decorView.buildDrawingCache();        Bitmap bitmap = Bitmap.createBitmap(decorView.getDrawingCache(), 0, 0,                decorView.getMeasuredWidth(), decorView.getMeasuredHeight());        decorView.setDrawingCacheEnabled(false);        decorView.destroyDrawingCache();        return bitmap;    }

首先利用Window的getDecorView()方法获取到屏幕上包含状态栏部分和用户应用程序里的内容。[ Retrieve the top-level window decor view (containing the standard window frame/decorations and the client's content inside of that). ]


然后使用decorView.getDrawingCache()方法返回被缓存的bitmap。[ Returns the bitmap in which this view drawing is cached. ]


效果:



附:保存bitmap到外部存储[ 需要外部存储的读写权限 ]

    public static void saveBitmap(Context context, Bitmap bitmap) {                String fileName = System.currentTimeMillis() + ".png";        String storagePath = Environment.getExternalStorageDirectory().getPath();        if (storagePath == null) {            storagePath = context.getFilesDir().getPath();        }        String filePath = storagePath + "/Pictures/";        String imageFullName = filePath + fileName;        try {            if (bitmap != null) {                File dir = new File(filePath);                if (!dir.exists()) {                    dir.mkdirs();                }                File file = new File(imageFullName);                if (!file.exists()) {                    file.createNewFile();                }                FileOutputStream fos = new FileOutputStream(file);                if (fos != null) {                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);                    fos.flush();                    fos.close();                }            }        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }    }






微信公众号,一起来玩耍,纯属娱乐,哈哈哈~~~