I have wrote a method to convert any layout Liner,Relative,Frame etc into Bitmap but I want to make this method generic so that it accepts android.view.ViewGroup as a parameter rather than specific Relative or LinearLayout.
Here is my method below:
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;
}
Both Layout’s inherit from
ViewGroup, so you can useViewGroupas method parameter and all those statements will works fine.Also you should use
ViewGroup.LayoutParamsinstead ofRelativeLayout.LayoutParamsto make it generic.