We can’t extract the bitmap from a ViewGroup in onCreate() of an Activity , so in order to extract bitmap we use the onAttachToWindow()(which is called when our when the window attached to this activity is attached to the window manager) and call our method in this overrided method to extract and return the bitmap.
|
public class DemoActivity extends ActionBarActivity { |
private RelativeLayout mLayout; private Bitmap mBitmap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_demo); mLayout = (RelativeLayout) findViewById(R.id.parent); } /* * This method is called when the window attached to this activity has attached to window manager. * @see android.app.Activity#onAttachedToWindow() */ @Override public void onAttachedToWindow() { mBitmap = extractBitmap(mLayout); super.onAttachedToWindow(); } /** * This method returns the bitmap by drawing it to the canvas. * @param iViewGroup: Layout out of which bitmap is to be extracted. */ private Bitmap extractBitmap(RelativeLayout iViewGroup) { int width = iViewGroup.getWidth(); int height = iViewGroup.getWidth(); Bitmap createBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(createBitmap); iViewGroup.draw(canvas); return createBitmap; } }