Converting View to Bitmap without displaying it

Here is the Android code for converting a View object to Bitmap without displaying it. This can be useful for displaying complex views in a Gallery widget.

Bitmap bmp = Bitmap.createBitmap(Utils.getPixelForDp(width, this), Utils.getPixelForDp(height, this), Bitmap.Config.ARGB_8888);
View v = getLayoutInflater().inflate(layoutid, null);
v.measure(MeasureSpec.makeMeasureSpec(bmp.getWidth(), MeasureSpec.EXACTLY), 
MeasureSpec.makeMeasureSpec(bmp.getHeight(), MeasureSpec.EXACTLY));
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight()); 
v.draw(new Canvas(bmp));

Best source: Stackoverflow.com

4 thoughts on “Converting View to Bitmap without displaying it

  1. Anonymous

    Thank you so much for this. Other examples left off the “View.measure()” method which resulted in a blank screen. Yours works like a charm!

  2. admin Post author

    Sorry, but thanks to the plugins provided by Google, Eclipse is the easiest way to develop for Android. It’s not like VS, but can be customized. 🙂

Leave a Reply