Skip to content

Instantly share code, notes, and snippets.

@ixiyang
Created August 11, 2014 06:11
Show Gist options
  • Save ixiyang/bd1c416e2f4cdbe5dde6 to your computer and use it in GitHub Desktop.
Save ixiyang/bd1c416e2f4cdbe5dde6 to your computer and use it in GitHub Desktop.
public static Bitmap getViewBitmap(View v) {
v.clearFocus();
v.setPressed(false);
boolean willNotCache = v.willNotCacheDrawing();
v.setWillNotCacheDrawing(false);
// Reset the drawing cache background color to fully transparent
// for the duration of this operation
int color = v.getDrawingCacheBackgroundColor();
v.setDrawingCacheBackgroundColor(0);
if (color != 0) {
v.destroyDrawingCache();
}
v.buildDrawingCache();
Bitmap cacheBitmap = v.getDrawingCache();
if (cacheBitmap == null) {
return null;
}
Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
// Restore the view
v.destroyDrawingCache();
v.setWillNotCacheDrawing(willNotCache);
v.setDrawingCacheBackgroundColor(color);
return bitmap;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment