Skip to content

Instantly share code, notes, and snippets.

@twiceyuan
Last active January 25, 2016 08:31
Show Gist options
  • Save twiceyuan/38a54a5679b9a034d135 to your computer and use it in GitHub Desktop.
Save twiceyuan/38a54a5679b9a034d135 to your computer and use it in GitHub Desktop.
将 View 显示出来的图像转换为 Bitmap
/**
* 转换 View 为 Bitmap
*/
public static Bitmap viewToBitmap(View v) {
if (v.getMeasuredHeight() <= 0) {
v.measure(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
Bitmap b = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
v.draw(c);
return b;
}
Bitmap b = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
v.draw(c);
return b;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment