2017-04-08 33 views
0

我有包含TabLayout和ViewPager佈局,我有3個tab.layout代碼看起來像如何在Android中捕獲Tab和Recyclerview的隱形內容?

<android.support.v4.widget.NestedScrollView 
    android:id="@+id/nest_scrollview" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:clipToPadding="false" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <android.support.design.widget.TabLayout 
      android:id="@+id/tabs" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:layout_gravity="bottom" 
      android:background="@color/tab_background" 
      app:tabIndicatorColor="@color/colorPrimary" /> 

     <android.support.v4.view.ViewPager 
      android:id="@+id/pager" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 
    </LinearLayout> 

</android.support.v4.widget.NestedScrollView> 

現在我想借屏幕快照,應該採取一切標籤,它包含的內容。

我使用下面的代碼拍攝屏幕截圖,但它只是在屏幕上顯示可見部分。

public Bitmap takeScreenshot() { 
     View rootView = findViewById(android.R.id.content).getRootView(); 
     rootView.setDrawingCacheEnabled(true); 
     return rootView.getDrawingCache(); 
    } 

    public void saveBitmap(Bitmap bitmap) { 
     imagePath = new File(Environment.getExternalStorageDirectory() + "/screenshot.png"); 
     FileOutputStream fos; 
     try { 
      fos = new FileOutputStream(imagePath); 
      bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); 
      fos.flush(); 
      fos.close(); 
     } catch (FileNotFoundException e) { 
      Log.e("GREC", e.getMessage(), e); 
     } catch (IOException e) { 
      Log.e("GREC", e.getMessage(), e); 
     } 
    } 

回答

0

您可以使用下面的代碼捕獲整個recyclerview。

public class Utils { 
    public Utils() { 
    } 

    public Bitmap getScreenshotFromRecyclerView(RecyclerView view) { 
     Bitmap bigBitmap = null; 

     RecyclerView.Adapter adapter = view.getAdapter(); 

     if (adapter != null) { 
      int size = adapter.getItemCount(); 
      int height = 0; 
      Paint paint = new Paint(); 
      int iHeight = 0; 
      final int maxMemory = (int) (Runtime.getRuntime().maxMemory()/1024); 

      // Use 1/8th of the available memory for this memory cache. 
      final int cacheSize = maxMemory/8; 
      LruCache<String, Bitmap> bitmaCache = new LruCache<>(cacheSize); 
      for (int i = 0; i < size; i++) { 
       RecyclerView.ViewHolder holder = adapter.createViewHolder(view, adapter.getItemViewType(i)); 
       adapter.onBindViewHolder(holder, i); 
       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()); 
       holder.itemView.setDrawingCacheEnabled(true); 
       holder.itemView.buildDrawingCache(); 
       Bitmap drawingCache = holder.itemView.getDrawingCache(); 
       if (drawingCache != null) { 
        bitmaCache.put(String.valueOf(i), drawingCache); 
       } 
//    holder.itemView.setDrawingCacheEnabled(false); 
//    holder.itemView.destroyDrawingCache(); 
       height += holder.itemView.getMeasuredHeight(); 
      } 

      bigBitmap = Bitmap.createBitmap(view.getMeasuredWidth(), height, Bitmap.Config.ARGB_8888); 
      Canvas bigCanvas = new Canvas(bigBitmap); 
      bigCanvas.drawColor(Color.WHITE); 

      for (int i = 0; i < size; i++) { 
       Bitmap bitmap = bitmaCache.get(String.valueOf(i)); 
       bigCanvas.drawBitmap(bitmap, 0f, iHeight, paint); 
       iHeight += bitmap.getHeight(); 
       bitmap.recycle(); 
      } 
     } 
     storeImage(bigBitmap, "myRecipeTest.jpg"); 
     return bigBitmap; 
    } 

    /** 
    * Convert the bitmap into image and save it into the sdcard. 
    * 
    * @param imageData -Bitmap image. 
    * @param filename -Name of the image. 
    * @return 
    */ 
    public boolean storeImage(Bitmap imageData, String filename) { 
     // get path to external storage (SD card) 
     File sdIconStorageDir = new File(Environment.getExternalStorageDirectory() 
       .getAbsolutePath() + "/myAppDir/"); 
     // create storage directories, if they don't exist 
     sdIconStorageDir.mkdirs(); 
     try { 
      String filePath = sdIconStorageDir.toString() + File.separator + filename; 
      FileOutputStream fileOutputStream = new FileOutputStream(filePath); 
      BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream); 
      Log.e("<== SHARE ==>", "storeImage: Image Saved at----" + filePath); 
      // choose another format if PNG doesn't suit you 
      imageData.compress(Bitmap.CompressFormat.PNG, 100, bos); 
      bos.flush(); 
      bos.close(); 
     } catch (FileNotFoundException e) { 
      Log.w("TAG", "Error saving image file: " + e.getMessage()); 
      return false; 
     } catch (IOException e) { 
      Log.w("TAG", "Error saving image file: " + e.getMessage()); 
      return false; 
     } 
     return true; 
    } 
}