Convert a View into Image Android

Shahadat Hossain Shaki
1 min readNov 9, 2019

--

Hello, Sometime we need to convert a view into an image to share with the world.

Here I converted a view into an image to share. But I changed the view while converting the view.

We can convert a view, viewgroup. Lets way you have designed a layout and wants the user to share this layout on social media.

I have build a method that takes a view and return bitmap. With the bitmap you can store, share, upload the converted image. Below is my method.

private Bitmap viewToImage(View view) {
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
Drawable bgDrawable = view.getBackground();
if (bgDrawable != null)
bgDrawable.draw(canvas);
else
canvas.drawColor(Color.WHITE);
view.draw(canvas);

return returnedBitmap;
}

That's it, You are all set for the have fun with the bitmap.

**NB: If you want to add some other view only on the bitmap then you need to design accordingly and make them visible and just before calling this method make those views visible and after converting image hide them again.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Shahadat Hossain Shaki
Shahadat Hossain Shaki

Written by Shahadat Hossain Shaki

Co-Founder and Android developer of Happihub. Loves to develop things.

No responses yet

Write a response