Creating a bitmap from the view makes the view fade out, how to get the canvas view?

I found two ways to create a bitmap from a view. But as soon as I do that, the view disappears and I can no longer use it. How can I redraw the view after generating my bitmap?

the first:

public static Bitmap getBitmapFromView(View view) {
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),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;
}

      

second:

Bitmap viewCapture = null;

theViewYouWantToCapture.setDrawingCacheEnabled(true);

viewCapture = Bitmap.createBitmap(theViewYouWantToCapture.getDrawingCache());

theViewYouWantToCapture.setDrawingCacheEnabled(false);

      

EDIT

So I think I understand what's going on in the first one, we basically remove the view from it with the original canvas and draw it somewhere else associated with that bitmap. Is there some way to keep the original canvas and then set the view to be redrawn there?

+3


source to share


3 answers


Sorry, I'm not very good at this. But I am using the following code:

public Bitmap getBitmapFromView(View view, int width, int height) {
    Bitmap returnedBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(returnedBitmap);
    Drawable bgDrawable = view.getBackground();
    if (view==mainPage.boardView) { 
        canvas.drawColor(BoardView.BOARD_BG_COLOR);
    } else if (bgDrawable!=null) { 
        bgDrawable.draw(canvas);
    } else { 
        canvas.drawColor(Color.WHITE);
    }
    view.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
    view.layout(0, 0, width, height); 
    view.draw(canvas);
    return returnedBitmap;
}

      



which is so similar to yours, I suspect we will copy and edit it from the same place.

I have no problem with the view disappearing from the original drawing tree. Mine is called for ViewGroups, not regular views.

+3


source


Try it.

Getting a bitmap:

// Prepping.
boolean oldWillNotCacheDrawing = view.willNotCacheDrawing();
view.setWillNotCacheDrawing(false); 
view.setDrawingCacheEnabled(true);
// Getting the bitmap
Bitmap bmp = view.getDrawingCache();

      



And make sure to reset the view back to its old self.

view.destroyDrawingCache();
view.setDrawingCacheEnabled(false);
view.setWillNotCacheDrawing(oldWillNotCacheDrawing);    

return bmp; 

      

0


source


Guy's answer works when the view is not yet laid out in the parent view. If the view has already been measured and laid out in the parent view, Guy's answer could mess up your activity layout. If the view hasn't been measured and posted yet, Guy's answer works great.

My answer will work after the one laid out and it won't mess up the Activity layout as it doesn't measure or render the view again.

0


source







All Articles