I was searching for the solution to this too and, in the end, managed to come up with my own solution which is actually quite simple.
In my case, I had a LinearLayout
which contained a number of View
elements, some of which sometimes were not on screen, vertically below the end of the screen bounds. I was able to save the View
as a Bitmap (see loadBitmapFromView()
, below) but had trouble when it extended beyond the bottom of the screen.
My solution was to make the LinearLayout
part of a ScrollView
composite.
e.g.
This:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/my_linear_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<!-- Some Views added here at runtime -->
</LinearLayout>
Became:
Note use of wrap_content
to ensure ScrollView scales to the height of your content.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:id="@+id/my_linear_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<!-- Some Views added here at runtime -->
</LinearLayout>
</ScrollView>
This seemed to ensure that the offscreen items could be manually laid out when I wanted to save the View
as a bitmap. I used the following method to save the bitmap. I found this much earlier on SO but I can't seem to find the question in order to reference it properly (whoever you are - thank you!).
For the following method, I pass in a reference to the LinearLayout
above (my_linear_layout
).
public static Bitmap loadBitmapFromView(View view) {
Bitmap bitmap = null;
// width measure spec
int widthSpec = View.MeasureSpec.makeMeasureSpec(
view.getMeasuredWidth(), View.MeasureSpec.AT_MOST);
// height measure spec
int heightSpec = View.MeasureSpec.makeMeasureSpec(
view.getMeasuredHeight(), View.MeasureSpec.AT_MOST);
// measure the view
view.measure(widthSpec, heightSpec);
// set the layout sizes
int left = view.getLeft();
int top = view.getTop();
int width = view.getMeasuredWidth();
int height = view.getMeasuredHeight();
int scrollX = view.getScrollX();
int scrollY = view.getScrollY();
view.layout(left, top, width + left, height + top);
// create the bitmap
bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),
Bitmap.Config.ARGB_8888);
// create a canvas used to get the view's image and draw it on the
// bitmap
Canvas c = new Canvas(bitmap);
// position the image inside the canvas
c.translate(-view.getScrollX(), -view.getScrollY());
// get the canvas
view.draw(c);
return bitmap;
}