简体   繁体   English

将CardView从RecyclerView转换为位图

[英]converting CardView from RecyclerView to a Bitmap

I have a CardView (with multiple items like imageView , textView , Buttons ) in a RecyclerView . 我有一个CardView (有多个项目,如imageViewtextViewButtons )在RecyclerView I want to get bitmap from this cardview . 我想从这张cardview获取bitmap I tried a simple method of converting cardView into bitmap ( convert a cardview to bitmap ) but its not working properly. 我尝试了一种将cardView转换为bitmap的简单方法(将cardView 转换为bitmap ),但是它无法正常工作。 I only got the raw view of the card just like in xml without any updated items from a firebaseserver . 我只得到卡的原始视图,就像在xml一样,没有从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 尝试添加2秒的延迟或在卡片视图的布局侦听器上实现此延迟

CardView extend view, so you probably can convert it as any other view, try to pass the CardView into that function: CardView扩展视图,因此您可能可以将其转换为任何其他视图,请尝试将CardView传递给该函数:

  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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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