我有一个LinearLayout,我想将该视图的内容保存为图像。我有一半工作。
File imageFile;
// image naming and path to include sd card appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/a.png";
// create bitmap screen capture
View v1 = getWindow().getDecorView().getRootView();
Bitmap bitmap;
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
OutputStream fout = null;
imageFile = new File(mPath);
try {
fout = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.PNG, 10, fout);
fout.flush();
fout.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
使用上面的代码,它将保存视图的副本,但仅在设备屏幕上显示的内容。我有关于我想保存的视图的更多信息。 here和this上面的代码只会将屏幕上显示的信息保存为图像。我希望保存所有信息,甚至是屏幕上没有的信息(您需要滚动查看的位置)。
我怎样才能做到这一点?
答案 0 :(得分:1)
另一种选择是使用:
Bitmap b = Bitmap.createBitmap(width, height....)
v1.setLayoutParams // Full width and height of content
Canvas c = new Canvas(b);
v1.draw(c); // You now have full bitmap
saveBitmap(b);
答案 1 :(得分:0)
在其上运行度量/布局传递并将其绘制到画布上。假设您的父级被称为“视图”并且是一个垂直的LinearLayout:
view.measure(someWidth, MeasureSpec.UNSPECIFIED);
Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.draw(c);