Skip to content

Instantly share code, notes, and snippets.

@Amokrane
Created January 26, 2012 12:52
Show Gist options
  • Save Amokrane/1682636 to your computer and use it in GitHub Desktop.
Save Amokrane/1682636 to your computer and use it in GitHub Desktop.
Copy a view into a Bitmap (useful for taking screenshots)
int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNDEFINED);
view.measure(spec, spec);
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
Bitmap b = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
c.translate(-view.getScrollX(), -view.getScrollY());
view.draw(c);
// OR
view.setDrawingCacheEnabled(true);
Bitmap b = view.getDrawingCache();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment