Skip to content

Instantly share code, notes, and snippets.

View Rock610's full-sized avatar

Rock Rock610

  • YH
  • shanghai
View GitHub Profile
@Rock610
Rock610 / drawViewToBitmap
Last active January 10, 2023 09:48
Converting a view to Bitmap without displaying it
private Bitmap loadBitmapFromView(View v) {
int specWidth = View.MeasureSpec.makeMeasureSpec(900 /* any */, View.MeasureSpec.EXACTLY);
v.measure(specWidth, specWidth);
int questionWidth = v.getMeasuredWidth();
Bitmap b = Bitmap.createBitmap( questionWidth, questionWidth, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
c.drawColor(Color.WHITE);
v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());