如何在Android中布局超过屏幕尺寸时捕获整个布局

时间:2013-04-20 06:15:52

标签: android android-layout

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

       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);

1 个答案:

答案 0 :(得分: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);

现在我不确定这是否有效,但至少应根据文档提取一些内容。