Skip to content

Instantly share code, notes, and snippets.

@Cassie-von-Clausewitz
Last active September 19, 2016 15:57
Show Gist options
  • Save Cassie-von-Clausewitz/672ce68f01cf37e8367406091a1de59f to your computer and use it in GitHub Desktop.
Save Cassie-von-Clausewitz/672ce68f01cf37e8367406091a1de59f to your computer and use it in GitHub Desktop.
public static Bitmap getBitmapFromView(View view) {
//Define a bitmap with the same size as the view
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
//Bind a canvas to it
Canvas canvas = new Canvas(returnedBitmap);
//Get the view's background
Drawable bgDrawable = view.getBackground();
if (bgDrawable != null) {
//has background drawable, then draw it on the canvas
bgDrawable.draw(canvas);
} else {
//does not have background drawable, then draw the window background on the canvas
canvas.drawColor(ContextCompat.getColor(view.getContext(), R.color.window_background));
}
// draw the view on the canvas
view.draw(canvas);
//return the bitmap
return returnedBitmap;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment