Android对View进行截图并且保存

本文介绍了四种在Android中从View生成Bitmap的方法,包括利用View的DrawingCache,直接创建Bitmap并绘制View,以及使用Canvas进行缩放和裁剪。同时,提供了将Bitmap保存到本地的实用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

View截图生成bitmp:

 //方法1
    public static Bitmap capture(View view, float width, float height, boolean scroll, Bitmap.Config config) {
        if (!view.isDrawingCacheEnabled()) {
            view.setDrawingCacheEnabled(true);
        }
        if(width==0){
            width= ConstantsConfig.width;
        }
        if(height==0){
            height= ConstantsConfig.height;
        }

        Bitmap bitmap = Bitmap.createBitmap((int) width, (int) height, config);
        bitmap.eraseColor(Color.WHITE);
        Canvas canvas = new Canvas(bitmap);
        int left = view.getLeft();
        int top = view.getTop();
        if (scroll) {
            left = view.getScrollX();
            top = view.getScrollY();
        }
        int status = canvas.save();
        canvas.translate(-left, -top);
        float scale = width / view.getWidth();
        canvas.scale(scale, scale, left, top);
        view.draw(canvas);
        canvas.restoreToCount(status);
        Paint alphaPaint = new Paint();
        alphaPaint.setColor(Color.TRANSPARENT);
        canvas.drawRect(0f, 0f, 1f, height, alphaPaint);
        canvas.drawRect(width - 1f, 0f, width, height, alphaPaint);
        canvas.drawRect(0f, 0f, width, 1f, alphaPaint);
        canvas.drawRect(0f, height - 1f, width, height, alphaPaint);
        canvas.setBitmap(null);
        return bitmap;
    }

    //方法2
    public static Bitmap createBitmap3(View v, int width, int height, Bitmap.Config config) {
        Bitmap bmp = Bitmap.createBitmap(v.getWidth(), v.getHeight(), config);
        Canvas c = new Canvas(bmp);
        c.drawColor(Color.WHITE);
        v.draw(c);
        return bmp;
    }
//方法3
    public static Bitmap createBitmap(View view) {
        view.buildDrawingCache();
        Bitmap bitmap = view.getDrawingCache();
        return bitmap;
    }
//方法4
    public static Bitmap createBitmap2(View v) {
        Bitmap bmp = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(bmp);
        c.drawColor(Color.WHITE);
        v.draw(c);
        return bmp;
    }

保存图片到本地方法:

	public static void saveBitmapPng(final Bitmap bmp, final String filepath) {
		new Thread() {
			@Override
			public void run() {
				saveBitmapPngInner(bmp,filepath);
			}
		}.start();
	}

	private static void saveBitmapPngInner(Bitmap bmp, String filepath) {

		if(bmp==null){
			return;
		}

		FileOutputStream out = null;
		try {
			out = new FileOutputStream(filepath);
			bmp.compress(Bitmap.CompressFormat.PNG, 80, out); // bmp is your Bitmap instance
			// PNG is a lossless format, the compression factor (100) is ignored
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (out != null) {
					out.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

保存view截图后的bitmap到本地:

public String getScreenShot(View view) {

        int w = view.getWidth();
        int h = view.getHeight();

        String path = "/storage/emulated/0/pic_"+System.currentTimeMillis() + ".png";


        if (w == 0 || h == 0) {
            return path;
        } else {
            Bitmap bitmap = null;
            if (bitmap == null) {
                bitmap = ScreenShotUtil.capture(view, Math.round(w / 3.0f), Math.round(h / 3.0f), true, Bitmap.Config.RGB_565);
//                bitmap = ScreenShotUtil.createBitmap3(view, Math.round(w / 4.0f), Math.round(h / 4.0f), Bitmap.Config.RGB_565);
//                bitmap = ScreenShotUtil.createBitmap2(view);
//                bitmap = ScreenShotUtil.createBitmap(view);

                ImageSaveUtil.saveBitmapPng(bitmap, path);
            }
            return path;
        }

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值