Created
November 14, 2017 04:10
-
-
Save nhancv/af6fec205b2f09f1a54ec6090023a19f to your computer and use it in GitHub Desktop.
genBitmapFile for view offscreen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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