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