繁体   English   中英

将CardView从RecyclerView转换为位图

[英]converting CardView from RecyclerView to a Bitmap

我有一个CardView (有多个项目,如imageViewtextViewButtons )在RecyclerView 我想从这张cardview获取bitmap 我尝试了一种将cardView转换为bitmap的简单方法(将cardView 转换为bitmap ),但是它无法正常工作。 我只得到卡的原始视图,就像在xml一样,没有从firebaseserver获得任何更新的项目。

您可能需要在视图的屏幕快照完全膨胀之前进行拍摄。 尝试添加2秒的延迟或在卡片视图的布局侦听器上实现此延迟

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