Daniele B
Daniele B

Reputation: 20412

Android: how to convert a scrolling layout to Bitmap

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?

Upvotes: 2

Views: 2334

Answers (2)

user13029475
user13029475

Reputation: 11

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);

Upvotes: 1

BVB
BVB

Reputation: 5420

Try this:

Bitmap bitmap = Bitmap.createBitmap(content.getWidth(), content.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
content.draw(canvas);

Upvotes: 1

Related Questions