Skip to content

Instantly share code, notes, and snippets.

@esabook
Created May 14, 2019 03:53
Show Gist options
  • Save esabook/20a6eeea9d5164e390550f50a60df16f to your computer and use it in GitHub Desktop.
Save esabook/20a6eeea9d5164e390550f50a60df16f to your computer and use it in GitHub Desktop.
get bitmap from android view
/**
* get {@link Bitmap} object from {@link View}
*
* @param view
* @return
*/
public 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) {
bgDrawable.draw(canvas);
} else {
canvas.drawColor(Color.WHITE);
}
// draw the view on the canvas
view.draw(canvas);
return returnedBitmap;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment