0

在我的应用程序中,当我缩放我的布局时,我正在缩放我的布局,我想捕获我的布局。我尝试了下面的代码,但它只捕获了屏幕中可见的内容。请帮助我如何捕获布局。

       View content = findViewById(R.id.main_container);
    content.setDrawingCacheEnabled(true);
    Bitmap bitmap = content.getDrawingCache();
    // actual width of the image (img is a Bitmap object)
    // int width = bitmap.getWidth();
    // int height = bitmap.getHeight();

    // recreate the new Bitmap and set it back

        bitmap = Bitmap.createBitmap(Bitmap.createBitmap(
                l1.getWidth() * 2, l1.getHeight() * 2,
                Bitmap.Config.ARGB_8888));


    // Bitmap mb = bitmap.copy(Bitmap.Config.ARGB_8888, true);
    Canvas bitmapCanvas = new Canvas();
    bitmapCanvas.setBitmap(bitmap);
    bitmapCanvas.scale(2.0f, 2.0f);
    content.draw(bitmapCanvas);
    ByteArrayOutputStream out = new ByteArrayOutputStream();

    bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);

    File imagesFolder = new File(Environment.getExternalStorageDirectory(),
            "saved_images");
    imagesFolder.mkdirs();
    String fileName = "final-" + String.valueOf(n) + ".jpg";
    File output = new File(imagesFolder, fileName);
    while (output.exists()) {
        n++;
        fileName = "final-" + String.valueOf(n) + ".jpg";
        output = new File(imagesFolder, fileName);
    }
    Uri uriSavedImage = Uri.fromFile(output);
    OutputStream imageFileOS;
    try {
        imageFileOS = getContentResolver().openOutputStream(uriSavedImage);
        imageFileOS.write(out.toByteArray());
        imageFileOS.flush();
        imageFileOS.close();




    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    content.setDrawingCacheEnabled(false);
4

1 回答 1

0

这是获取布局屏幕截图的另一种方法。我正在写这个,所以代码可能包含一些错误(现在无法验证);

ViewGroup bg = (ViewGroup) findViewById(R.id.your_layout_id);
Bitmap screenshot = Bitmap.createBitmap(vg.getWidth(), vg.getHeight(),
                                        Bitmap.Config.RGBA8888);
Canvas c = new Canvas(screenshot);
vg.draw(c);

现在我不确定这是否更好,但至少应该根据文档绘制一些东西。

于 2013-04-20T06:31:51.000 回答