如何捕捉整个布局当布局超过屏幕大小android
问题描述:
在我的应用程序中,我放大我的布局,当我放大我的布局我想捕捉我的布局。我尝试了下面的代码,但它只捕获在屏幕上可见的内容请帮助我如何捕捉布局。如何捕捉整个布局当布局超过屏幕大小android
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);
答
下面是抓取布局屏幕截图的另一种方法。我正在写这篇文章,因此代码可能包含一些错误(目前无法验证);
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);
现在我不确定这个工作是否更好,但至少应该根据文档绘制一些东西。
+0
好吧谢谢你我会尝试 – user2252192 2013-04-20 06:36:39
+0
嗨我试过你的代码它也采取布局,这是可见的屏幕上,但不是整个屏幕 – user2252192 2013-04-20 07:36:52
也许你应该使用''View.draw''方法而不是绘制缓存? – harism 2013-04-20 06:17:20
你可以给我代码的功能 – user2252192 2013-04-20 06:27:59