Android开发如何将view转化成图片

时间:2021-12-12 06:39:43
// 获取指定Activity的截屏,保存到png文件
    public static Bitmap takeScreenShot(Activity activity) {
        // View是你需要截图的View
        View view = activity.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap b1 = view.getDrawingCache();

        // 获取状态栏高度
        Rect frame = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        int statusBarHeight = frame.top;
//        L.e(""+statusBarHeight);

        // 获取屏幕长和高
        int width = activity.getWindowManager().getDefaultDisplay().getWidth();
        int height = activity.getWindowManager().getDefaultDisplay().getHeight();
        // 去掉标题栏
        // Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);
        int hh = statusBarHeight+50;
        L.e("截取的高---"+statusBarHeight+"屏幕的高--"+height);
        Bitmap b = Bitmap.createBitmap(b1, 0, hh, width, height -hh);
        view.destroyDrawingCache();
        L.e("Thresh"+ "takeScreenShot:            截图成功");
        return b;
    }