简体   繁体   中英

converting CardView from RecyclerView to a Bitmap

I have a CardView (with multiple items like imageView , textView , Buttons ) in a RecyclerView . I want to get bitmap from this cardview . I tried a simple method of converting cardView into bitmap ( convert a cardview to bitmap ) but its not working properly. I only got the raw view of the card just like in xml without any updated items from a firebaseserver .

you are probably taking the screenshot of view before it is fully inflated. Try adding a 2 second delay or implement this on layout listener of the card view

CardView extend view, so you probably can convert it as any other view, try to pass the CardView into that function:

  public static Bitmap getBitmapFromView(View view) {
    //Define a bitmap with the same size as the view
    Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
    //Bind a canvas to it
    Canvas canvas = new Canvas(returnedBitmap);
    //Get the view's 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 returnedBitmap;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM