CardView shadow does not display when converting to bitmap

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) { //Define a bitmap with the same size as the view Bitmap b = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888); //Bind a canvas to it Canvas canvas = new Canvas(b); //Get the view background Drawable bgDrawable = view.getBackground(); if (bgDrawable != null) //has background drawable, then draw it on the canvas bgDrawable.draw(canvas); else //does not have background drawable, then draw white background on the canvas canvas.drawColor(Color.WHITE); // draw the view on the canvas view.draw(canvas); //return the bitmap return b; } 

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> <!-- Some other views that aren't particularly interesting --> </LinearLayout> 
+5
source share
2 answers

just change the map view to view and set

Android: background = "@ android: draw / dialog _holo_light_frame"

due to the fact that you need to debug yourself

+3
source

I have the same problem. So after searching, I based this. Hope this helps https://medium.com/@ArmanSo/take-control-of-views-shadow-android-c6b35ba573e9

0
source

Source: https://habr.com/ru/post/1232889/


All Articles