Tag Archives: view

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