D
D
dedmarazmon2016-06-01 10:49:23
Android
dedmarazmon, 2016-06-01 10:49:23

How to save a fully rendered ScrollView to a Bitmap?

Here is the code:

public static Bitmap getBitmapFromView(View view, int totalHeight, int totalWidth) {
        Bitmap returnedBitmap = Bitmap.createBitmap(totalWidth,totalHeight , Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(returnedBitmap);
        Drawable bgDrawable = view.getBackground();
        if (bgDrawable != null)
            bgDrawable.draw(canvas);
        else
            canvas.drawColor(Color.WHITE);
        view.draw(canvas);
        return returnedBitmap;
    }

If I pass a ScrollView I get:
d18a73e18b1345709b4a806de5749965.png
I pass the ViewGroup in which the ScrollView is contained I get:
b4bc1f1c5e444382adb33c58001f3faf.png
Is there an option to save the ViewGroup with the ToolBar and the fully expanded ScrollView?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lazard105, 2016-06-01
@dedmarazmon

You forgot to call a method measure
Fix it like this:

public static Bitmap getBitmapFromView(View view, int totalHeight, int totalWidth) {
        Bitmap returnedBitmap = Bitmap.createBitmap(totalWidth,totalHeight , Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(returnedBitmap);
        Drawable bgDrawable = view.getBackground();
        if (bgDrawable != null)
            bgDrawable.draw(canvas);
        else
            canvas.drawColor(Color.WHITE);

        view.measure( View.MeasureSpec.makeMeasureSpec(totalWidth,View.MeasureSpec.EXACTLY),
                View.MeasureSpec.makeMeasureSpec(totalHeight,View.MeasureSpec.EXACTLY));
        view.layout(0,0,totalWidth,totalHeight);

        view.draw(canvas);
        return returnedBitmap;
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question