2

I would like to convert the whole scrolling layout inside a ScrollView to a Bitmap

<ScrollView...>

<RelativeLayout android:id="@+id/content"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    ...>

</RelativeLayout>

</ScrollView>

If the content doesn't scroll, this code works great:

RelativeLayout content = (RelativeLayout) findViewById(R.id.content);
content.setDrawingCacheEnabled(true);
content.buildDrawingCache();
Bitmap bm = content.getDrawingCache();

but if the content is scrolling, getDrawingCache() returns null.

Any suggestion on how I can export a scrolling view to a bitmap?

Daniele B
  • 19,801
  • 29
  • 115
  • 173
  • I can see why you're having a problem... it isn't drawing what's off of the screen. An alternative is creating a bitmap and drawing everything onto it -- but that's rather complex. Your question intrigues me (as I don't have the answer), but I am wondering what you are trying to achieve. –  Jul 11 '14 at 03:32
  • 1
    Hi Jeremy, I would like to convert the layout content to PNG (after converting it to Bitmap), so that I can simply share it as an image via the ACTION_SEND Intent (Email, Facebook, Whatsapp, etc. etc.) – Daniele B Jul 11 '14 at 03:51
  • Ah, that makes sense. I look forward to an answer as it's not an answer it my toolbox. Thank you for the clarification. –  Jul 11 '14 at 03:53

2 Answers2

1

Try this:

Bitmap bitmap = Bitmap.createBitmap(content.getWidth(), content.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
content.draw(canvas);
BVB
  • 5,380
  • 8
  • 41
  • 62
1

I tried this but it didn't achieve the expected result - had the width of the scrollview, but the height was only part of the scrollview.

What worked instead for me was to do the following:

Bitmap bitmap = Bitmap.createBitmap(content.getChildAt(0).getWidth(), 
content.getChildAt(0).getHeight(), Bitmap.Config.ARGB_8888);

Canvas canvas = new Canvas(bitmap);
content.draw(canvas);
B. Go
  • 1,436
  • 4
  • 15
  • 22