点击按钮自动截取当前View图片

来源:互联网 发布:mac怎样整理文件 编辑:程序博客网 时间:2024/06/07 12:23

截取的是个bitmap图片

 /**     * 获取屏幕截图     *     * @return     */    public Bitmap getScreenShot() {        // 获取windows中最顶层的view        View view = getWindow().getDecorView();        view.buildDrawingCache();        // 获取状态栏高度        Rect rect = new Rect();        view.getWindowVisibleDisplayFrame(rect);        int statusBarHeights = rect.top;        Display display = getWindowManager().getDefaultDisplay();        // 获取屏幕宽和高        int widths = display.getWidth();        int heights = display.getHeight();        // 允许当前窗口保存缓存信息        view.setDrawingCacheEnabled(true);        // 去掉状态栏        Bitmap bmp = Bitmap.createBitmap(view.getDrawingCache(), 0,                statusBarHeights, widths, heights - statusBarHeights);        ByteArrayOutputStream bos = new ByteArrayOutputStream();        bmp.compress(Bitmap.CompressFormat.JPEG, 10, bos);        byte[] bytes = bos.toByteArray();        bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);        // 销毁缓存信息        view.destroyDrawingCache();        return bmp;    }
原创粉丝点击