我已经编写了一种方法,可以将任何布局(Liner、Relative、Frame等)转换为位图,但我希望将此方法通用化,以使其接受 android.view.ViewGroup
作为参数,而不是特定的 Relative
或 LinearLayout
。
以下是我的方法:
public Bitmap getBitmapFromView(RelativeLayout v) {
v.setLayoutParams(new LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
Bitmap b = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.draw(c);
return b;
}