Skip to content

Instantly share code, notes, and snippets.

@Binary-Finery
Created September 8, 2018 20:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Binary-Finery/6303fb7cd4083bbb6ec02b0d93eefca9 to your computer and use it in GitHub Desktop.
Save Binary-Finery/6303fb7cd4083bbb6ec02b0d93eefca9 to your computer and use it in GitHub Desktop.
public Bitmap getWholeListViewItemsToBitmap() {
ListAdapter adapter = listView.getAdapter();
int itemsCount = adapter.getCount();
int allItemsHeight = 0;
List<Bitmap> bitmaps = new ArrayList<Bitmap>();
for (int i = 0; i < itemsCount; i++) {
View childView = adapter.getView(i, null, listView);
childView.measure(View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
childView.layout(0, 0, childView.getMeasuredWidth(), childView.getMeasuredHeight());
childView.setDrawingCacheEnabled(true);
childView.buildDrawingCache();
bitmaps.add(childView.getDrawingCache());
allItemsHeight += childView.getMeasuredHeight();
}
Bitmap bitmap = Bitmap.createBitmap(listView.getMeasuredWidth(), allItemsHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
int iHeight = 0;
for (int i = 0; i < bitmaps.size(); i++) {
Bitmap bmp = bitmaps.get(i);
canvas.drawBitmap(bmp, 0, iHeight, paint);
iHeight += bmp.getHeight();
bmp.recycle();
}
return bitmap;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment