PROBLEM
I am trying to save a view group (which has CardView as one of its childern) as a PNG file. To achieve this,
- I inflate the viewing group and populate the views with the necessary information.
- Upload image to image view via Glide
- Add
ViewTreeObserver.OnGlobalLayoutListener to the ViewTreeObserver image and pass the full (parent) view that will be passed to the method that converts the view into a bitmap when the image snapshot is greater than zero (the image height attribute is wrap_content , so its bottom will be zero before the image is loaded) .
This way I can convert the view to a bitmap, however, with one caveat: CardView display CardView not appear in the bitmap.
REFUSED ATTEMPTS
So far I have tried:
- Switch between the
layerType attribute from “software” to “hardware”. - Turn on or off the
cardUseCompatPadding CardView attribute. - I tried to adjust the image without using Glide.
- I tried without loading an image that can be drawn at all.
CODE
Here are the code snippets that can help you solve the problem:
Method used to convert a view to a bitmap
public static Bitmap getBitmapFromView(View view) {
An XML view layout file that is getBitmapFromView() and passed to getBitmapFromView() above.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="16dp"> <com.devspark.robototextview.widget.RobotoTextView android:id="@+id/title" style="@style/text_subhead" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="top" android:layout_marginBottom="@dimen/lessons_horizontal_margin_narrow" android:layout_marginLeft="@dimen/lessons_horizontal_margin_narrow" android:layout_marginRight="@dimen/lessons_horizontal_margin_narrow" android:layout_marginTop="@dimen/lessons_horizontal_margin_narrow" android:gravity="left" app:typeface="roboto_medium" /> <com.devspark.robototextview.widget.RobotoTextView android:id="@+id/text" style="@style/text_subhead" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/lessons_horizontal_margin_narrow" android:layout_marginRight="@dimen/lessons_horizontal_margin_narrow" android:gravity="left" android:textColor="@color/text" app:typeface="roboto_regular" /> <android.support.v7.widget.CardView android:id="@+id/image_container" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="@dimen/lessons_horizontal_margin_narrow" app:cardCornerRadius="@dimen/lessons_image_card_corner_radius" app:cardElevation="3dp" app:cardPreventCornerOverlap="false" app:cardUseCompatPadding="true"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <com.makeramen.roundedimageview.RoundedImageView android:id="@+id/image" android:layout_width="match_parent" android:layout_height="wrap_content" android:contentDescription="@null" app:riv_corner_radius_top_left="@dimen/lessons_image_card_corner_radius" app:riv_corner_radius_top_right="@dimen/lessons_image_card_corner_radius" /> <com.devspark.robototextview.widget.RobotoTextView android:id="@+id/caption" style="@style/text_caption" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="@dimen/lessons_image_card_caption_margin" android:gravity="left" app:typeface="roboto_condensed_regular" /> </LinearLayout> </android.support.v7.widget.CardView> </LinearLayout>
source share