[英]Android convert complete view to bitmap
can i get the whole linearlayout
or other layouts converted to bitmap. 我可以将整个
linearlayout
或其他布局转换为位图linearlayout
? my code is this : 我的代码是这样的:
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);
the problem is if layout is larger than the screen size only i get whats on screen & not the whole contents eg textview etc. which are currently not on screen but are part of layout. 问题是,如果布局大于屏幕尺寸,那么只有我在屏幕上得到内容,而不是整个内容(例如textview等),这些内容当前不在屏幕上,而是布局的一部分。
Any suggestions geeks out there. 任何建议都是极客。 this is way to urgent and important.
这是紧急而重要的方法。
You can try setting a size before getting the Bitmap. 您可以在获取位图之前尝试设置大小。 Use the following code to get Bitmap and try.
使用以下代码获取位图并尝试。
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
well that will be Easy tho. 好吧,这将很容易。 assuming that you trying to Save the view to a bitmap.
假设您尝试将视图保存到位图。 well here is my full saving method check it out and see how it works.
好了,这是我完整的保存方法,请查看并查看其工作原理。
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);
}
here is the folder name that the image will be saved to. 这是图像将保存到的文件夹名称。
String folder = "/sdcard/Pictures/StyleMe";
you can change StyleMe to become your app name or anything u like. 您可以将StyleMe更改为您的应用名称或您喜欢的任何名称。 and here is my file just declare both in under your class name.
这是我的文件,只需在您的班级名称下声明两者即可。 or just add them inside the method
static File file;
或者只是将它们添加到方法
static File file;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.