Take a screenshot of RecyclerView in FULL length

后端 未结 5 1555
南方客
南方客 2020-12-13 07:55

I want to get a \"full page\" screenshot of the activity. The view contains a RecyclerView with many items.

I can take a screenshot of the current view with this fun

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-13 08:30

    The best solution I found is

    1. create a new NestedScrollView

    2. add it to recyclerview's parent

    3. remove recyclerview from its parent "it's important to add it to a new parent"

    4. add recyclerview to nestedscrollview

    5. take screenshot of nestedscrollview

    6. add recyclerview to its main parent.

       nestedScreenShot = new NestedScrollView(getContext());
      
       constraintLayout.removeView(recyclerView);
      
       ConstraintLayout.LayoutParams params =new 
      
       Constraints.LayoutParams(viewGroup.LayoutParams.MATCH_PARENT,
      
       ViewGroup.LayoutParams.MATCH_PARENT);
      
       nestedScreenShot.setLayoutParams(params);
      
      Drawable scrollBackground = scrollView.getBackground();
      if (scrollBackground != null) {
          tempNestedScreenShot.setBackground(scrollBackground);
      }
      tempNestedScreenShot.setPadding(scrollView.getLeft(), 
      scrollView.getTop(), scrollView.getRight(), scrollView.getBottom());
      
       constraintLayout.addView(nestedScreenShot);
      
       nestedScreenShot.addView(recyclerView, params);
      
       new Handler().postDelayed(new Runnable() {
              @Override
              public void run() {
      
                  nestedScreenShot.removeView(recyclerView);
                  constraintLayout.removeView(nestedScreenShot);
                  nestedScreenShot = null;
                  constraintLayout.addView(recyclerView, params);
      
      
              }
              }, 8000);
        takescreenshotOfNested(nestedScreenShot);
      

    This method will return screenshot of nestedscrollview

           private Bitmap takescreenshotOfNested(View u) {
            
            NestedScrollView viewNested = null;
                
            ScrollView viewScroll = null;
                  
            if (u instanceof NestedScrollView) {
                     
           
            viewNested = (NestedScrollView) u;
                 
         
           } else if (u instanceof ScrollView) {
                  
          
             viewScroll = (ScrollView) u;
                
           }
                
                    Bitmap bitmap;
                    if (viewNested != null) {
                        viewNested.setDrawingCacheEnabled(false);
                        viewNested.invalidate();
                        viewNested.getChildAt(0).setDrawingCacheEnabled(false);
                        viewNested.getChildAt(0).invalidate();
                
                        bitmap = Bitmap.createBitmap(viewNested.getChildAt(0).getWidth(),
                                viewNested.getChildAt(0).getHeight() + 8, Bitmap.Config.ARGB_8888);
                        Canvas canvas = new Canvas(bitmap);
                        canvas.drawColor(activity.getResources().getColor(R.color.layout_background));
                        viewNested.draw(canvas);
                    } else {
                
                        try {
                
                            viewScroll.setDrawingCacheEnabled(false);
                            viewScroll.invalidate();
                        } catch (Exception ignored) {
                        }
                        viewScroll.getChildAt(0).setDrawingCacheEnabled(false);
                        viewScroll.getChildAt(0).invalidate();
                
                        bitmap = Bitmap.createBitmap(viewScroll.getChildAt(0).getWidth(),
                                viewScroll.getChildAt(0).getHeight() + 8, Bitmap.Config.ARGB_8888);
                        Canvas canvas = new Canvas(bitmap);
                        canvas.drawColor(activity.getResources().getColor(R.color.layout_background));
                        viewScroll.draw(canvas);
                
                    }
                    return bitmap;
                
                }
    

    don't forget to recycle the bitmap after using it

提交回复
热议问题