Skip to content

Instantly share code, notes, and snippets.

@nhancv
Created November 14, 2017 04:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nhancv/af6fec205b2f09f1a54ec6090023a19f to your computer and use it in GitHub Desktop.
Save nhancv/af6fec205b2f09f1a54ec6090023a19f to your computer and use it in GitHub Desktop.
genBitmapFile for view offscreen
public static void genBitmapFile(Context context, View targetView, int num) {
int sizePixels = 1080;
Bitmap result = Bitmap.createBitmap(sizePixels, sizePixels, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(result);
// measure view first
int sizeSpec = View.MeasureSpec.makeMeasureSpec(sizePixels, View.MeasureSpec.EXACTLY);
targetView.measure(sizeSpec, sizeSpec);
// then layout
int width = targetView.getMeasuredWidth();
int height = targetView.getMeasuredHeight();
targetView.layout(0, 0, width, height);
// now you can draw it
targetView.draw(c);
saveBitmapToFile(context, "testbm_" + num + ".png", result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment