Android View to Bitmap
Today we will learn how to convert the android view or XML to a bitmap. During android development sometimes we need to convert the view or xml to bitmap for different purposes. One use case could be to share the the bitmap using sharing intent provided by google. You can find the tutorial here how can you share the bitmap without saving it to external memory. Now let's get started with the tutorial to convert the xml to bitmap.
Implementation
Actually it is very easy to convert the xml to a bitmap. You can use the drawing class provided by the google android.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Bitmap createBitmapFromView(View v) { | |
v.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, | |
RelativeLayout.LayoutParams.WRAP_CONTENT)); | |
v.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), | |
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); | |
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight()); | |
Bitmap bitmap = Bitmap.createBitmap(v.getMeasuredWidth(), | |
v.getMeasuredHeight(), | |
Bitmap.Config.ARGB_8888); | |
Canvas c = new Canvas(bitmap); | |
v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom()); | |
v.draw(c); | |
return bitmap; | |
} |
Calling This Method
We can call this method by passing our view in this method. But first we need to inflate that xml.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); | |
View inflatedFrame = inflater.inflate(R.layout.my_view, null); | |
Bitmap bitmap = loadBitmapFromView(inflatedFrame.findViewById(R.id.id_of_parent_layout)); |
thank you so much!!
ReplyDeleteWhat a lifesaver, thank you! I wanted to create a PDF from an XML layout with databindings and gridviews with adapters and all kinds of stuff & the layout is too big for a normal display.
ReplyDeleteIt would have been hard to do without being able to see it in the design editor so I created a new tablet device called PDF Doc and gave it a 14" diagonal. That allowed me to test the view in the design editor using inches as units and making the root 10in by 7.5in for a nice half inch margin all around. Yay, this all worked like a charm!
Thank you so much!
ReplyDeleteGiving me exception that width and height must be > 0
ReplyDeleteSorry Its working fine actually need to call the above createBitmapFromView which you have write down. While I was used my function by the above function its working very fine... Thanks
DeleteHow do you get text from an editText to the bitmap. i have some edit texts in the layout and i cant get the text when i save the image. thank you
ReplyDeleteCool and that i have a keen provide: Where To Start House Remodeling house renovation channel
ReplyDelete