2

I have been trying to create a bit map of a list view, where the entire list view is not visible on the screen. I am using

Bitmap mBitmap = fullView.getDrawingCache();

to create bitmap. It works ok for the part of list view that is visible on the screen but, not for the part that isn't.I would like to know if a bitmap of a list view can be created without having to display it completely on the screen. All suggestions and or solutions are appreciated.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490

2 Answers2

0

Try using this method. Hack here is calling the layout and measure method of the view yourself.

public Bitmap loadBitmapFromView(View v) {
    v.setLayoutParams(new ViewGroup.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT));
    v.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
    Bitmap bitmap = Bitmap.createBitmap(v.getMeasuredWidth(),
            v.getMeasuredHeight(),
            Bitmap.Config.ARGB_8888);


    Canvas c = new Canvas(bitmap);
    v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
    v.draw(c);
    return bitmap;
}

Pass the view in this method like

LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View inflatedFrame = inflater.inflate(R.layout.my_view, null);
Bitmap bitmap = loadBitmapFromView(inflatedFrame.findViewById(R.id.id_of_your_view_in_layout));
Farooq Khan
  • 592
  • 4
  • 15
0

As android only draws what is visible, may idea would be to take bitmaps in pieces and then group the bitmaps into a single one. You could first get the bitmap of first n items. Then use the scrollTo method to scroll beyond the previous n items and get the bitmap for the next n items. This way you can get bitmap of the whole list view.

blessanm86
  • 31,439
  • 14
  • 68
  • 79
  • Have you got this to work? I am trying to solve the same problem and the scrollTo doesn't seem to work for me. I'm using scrollTo(0, i*height) where height is listView.getChildAt(0).getHeight() – 8oh8 Oct 05 '12 at 01:28