Android将完整视图转换为位图
我可以将整个Android将完整视图转换为位图,android,bitmapimage,Android,Bitmapimage,我可以将整个线性布局或其他布局转换为位图吗。 我的代码是: LinearLayout view = (LinearLayout)findViewById(R.id.linear_parent); view.setDrawingCacheEnabled(true); view.buildDrawingCache(); Bitmap bm = view.getDrawingCache(); ByteArrayOutputStream bytes = new ByteArrayOutputStr
线性布局
或其他布局转换为位图吗。
我的代码是:
LinearLayout view = (LinearLayout)findViewById(R.id.linear_parent);
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bm = view.getDrawingCache();
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
问题是,如果版面大于屏幕大小,只有我得到屏幕上的内容&而不是全部内容,例如文本视图等,这些内容当前不在屏幕上,但属于版面的一部分
有什么建议吗。
这是一种紧急且重要的方法。您可以在获取位图之前尝试设置大小。使用以下代码获取位图并重试
LinearLayout view = (LinearLayout)findViewById(R.id.linear_parent);
view.setDrawingCacheEnabled(true);
view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false); // clear drawing cache
嗯,那很容易。假设您正在尝试将视图保存为位图。 这是我的完整保存方法,看看它是如何工作的
void Save() {
if (null != view.getDrawable()) {
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
save = view.getDrawingCache();
final File myDir = new File(folder);
myDir.mkdirs();
final Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
final String fname = "StyleMe-" + n + ".png";
file = new File(myDir, fname);
if (file.exists())
file.delete();
try {
final FileOutputStream out = new FileOutputStream(file);
save.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://"
+ Environment.getExternalStorageDirectory())));
Toast.makeText(getApplication(), "Image Saved",
Toast.LENGTH_SHORT).show();
} catch (final Exception e) {
Toast.makeText(getApplication(),
"Something Went Wrong check if you have Enough Memory",
Toast.LENGTH_LONG).show();
}
} else {
final Toast tst = Toast.makeText(getApplication(),
"Please Select An Image First", Toast.LENGTH_LONG);
tst.setGravity(Gravity.CENTER, 0, 0);
tst.show();
}
view.setDrawingCacheEnabled(false);
}
这是图像将保存到的文件夹名称
String folder = "/sdcard/Pictures/StyleMe";
您可以将StyleMe更改为您的应用程序名称或任何您喜欢的内容。
这是我的文件,在你的类名下声明。或者只需将它们添加到方法静态文件中;
您所说的“大于屏幕大小”是什么意思?我假设您有一个滚动视图
,在这种情况下,您应该在滚动视图
上应用代码,而不是线性布局
视图。getDrawingCache()代码>AFAIK图形缓存仅用于屏幕的可见部分。这样,您将无法获得整个布局的位图。老实说,考虑到屏幕上没有这些东西,整个测量/布局周期都没有发生,所以你不会得到它。谢谢你的建议。bcoz的限制,你提到我现在打印的数据从数据库中获取!!!!!1.