Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Take a screenshot of RecyclerView in FULL length .. each item having a list

I am using a recyclerView having lists of dynamic sizes...and when i use this method to take screenshot of each recyclerView item.. it taking the screenshot but each item containing only one list item each.. even if the list item size > 1.

public static Bitmap getRecyclerViewScreenshot(RecyclerView view) {
    int size = view.getAdapter().getItemCount();
    RecyclerView.ViewHolder holder = view.getAdapter().createViewHolder(view, 0);
    view.getAdapter().onBindViewHolder(holder, 0);
    ArrayList<Bitmap> bitmaps=new ArrayList<>();
    holder.itemView.measure(View.MeasureSpec.makeMeasureSpec(view.getWidth(),
            View.MeasureSpec.EXACTLY),
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    holder.itemView.layout(0, 0, holder.itemView.getMeasuredWidth(), holder.itemView.getMeasuredHeight());
    Bitmap bigBitmap = Bitmap.createBitmap(view.getMeasuredWidth(), holder.itemView.getMeasuredHeight() * size,
            Bitmap.Config.ARGB_8888);
    Canvas bigCanvas = new Canvas(bigBitmap);
    bigCanvas.drawColor(Color.WHITE);
    Paint paint = new Paint();
    int iHeight = 0;
    holder.itemView.setDrawingCacheEnabled(true);
    holder.itemView.buildDrawingCache();
    bigCanvas.drawBitmap(holder.itemView.getDrawingCache(), 0f, iHeight, paint);
    holder.itemView.setDrawingCacheEnabled(false);
    holder.itemView.destroyDrawingCache();
    iHeight += holder.itemView.getMeasuredHeight();
    for (int i = 1; i < size; i++) {
        view.getAdapter().onBindViewHolder(holder, i);
        holder.itemView.setDrawingCacheEnabled(true);
        holder.itemView.buildDrawingCache();
        bitmaps.add(holder.itemView.getDrawingCache());
        bigCanvas.drawBitmap(holder.itemView.getDrawingCache(), 0f, iHeight, paint);
        iHeight += holder.itemView.getMeasuredHeight();
        holder.itemView.setDrawingCacheEnabled(false);
        holder.itemView.destroyDrawingCache();
    }
    return bigBitmap;
}



  Here is my screenshot attached (`of each item of recyclerView`)!! It should show `two items` but uniformly its showing `1 item` each ` I am facing problem with the `inner listView` 

Original is :-enter image description here

Screenshot is :-enter image description here

Its the case for each and every recycler item screenshot only 1 item is shown.

like image 502
Santanu Sur Avatar asked Jan 08 '18 11:01

Santanu Sur


People also ask

Which RecyclerView method is called when getting the size of the data set?

getItemCount() : RecyclerView calls this method to get the size of the data set. For example, in an address book app, this might be the total number of addresses. RecyclerView uses this to determine when there are no more items that can be displayed.

What is the advantage of using RecyclerView over list view in android?

Simple answer: You should use RecyclerView in a situation where you want to show a lot of items, and the number of them is dynamic. ListView should only be used when the number of items is always the same and is limited to the screen size.


1 Answers

faced the same issue and resolved this by using this piece of code.

First of all, ask your view to calculate it's measurements and then ask view to give MeasuredHeight and all done.

recyclerView.measure(
        View.MeasureSpec.makeMeasureSpec(recyclerView.getWidth(), View.MeasureSpec.EXACTLY),
        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));

Bitmap bm = Bitmap.createBitmap(recyclerView.getWidth(), recyclerView.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
recyclerView.draw(new Canvas(bm));

saveImage(bm);
ImageView im
          = new ImageView(getActivity());
im.setImageBitmap(bm);
 new AlertDialog.Builder(getActivity()).setView(im).show();
like image 65
Nouman Ch Avatar answered Nov 14 '22 23:11

Nouman Ch