2

There are few thread about save layout as image here and here. What if I have a list view, and it is impossible to make them display on one screen.

Community
  • 1
  • 1
Ziwei Zeng
  • 691
  • 5
  • 21

2 Answers2

1

Try with this:

First enable the drawing cache in your ListView

vListView.setDrawingCacheEnabled(true);

Then adjust the size of the ListView to make every item visible.

ViewGroup.LayoutParams params = (ViewGroup.LayoutParams)  vListView.getLayoutParams();
params.height = (int) (mAdapter.getItemCount() * getResources().getDimension(R.dimen.max_item_height));
vListView.setLayoutParams(params);

Finally you can use either a callback or do a postDelayed with a Handler and get the bitmap.

vListView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                drawingCache = vListView.getDrawingCache();
            }
        });

Let me know if it did help you.

BTW Remember that in Android Studio, in debug mode, you can check the bitmap variables contents.

4gus71n
  • 3,717
  • 3
  • 39
  • 66
0

You have to add it in hierarchy or it won't compute its layout. But no one says you have to actually show it on screen - create layout in which you can place ListView outside of screen bounds and use drawing cache to construct widget image.

weaknespase
  • 1,014
  • 8
  • 15