2017-07-31 41 views
1

我有一個LinearLayout和一個RecyclerView正下方。在谷歌上搜索,我發現了一些代碼來截取RecyclerView(爲了真實,我不明白它是如何工作的)。這裏的代碼:截取LinearLayout和RecyclerView

public static Bitmap getRecyclerViewScreenshot(RecyclerView view) { 
     int size = view.getAdapter().getItemCount(); 
     RecyclerView.ViewHolder holder = view.getAdapter().createViewHolder(view, 0); 
     view.getAdapter().onBindViewHolder(holder, 0); 
     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(); 
     try { 
      for (int i = 1; i < size; i++) { 
       view.getAdapter().onBindViewHolder(holder, i); 
       holder.itemView.setDrawingCacheEnabled(true); 
       holder.itemView.buildDrawingCache(); 
       bigCanvas.drawBitmap(holder.itemView.getDrawingCache(), 0f, iHeight, paint); 
       iHeight += holder.itemView.getMeasuredHeight(); 
       holder.itemView.setDrawingCacheEnabled(false); 
       holder.itemView.destroyDrawingCache(); 
      } 
     } catch (Exception e) { 
     } 

     return bigBitmap; 
    } 

現在我想包括LinearLayout也在它剛好在RecyclerView之上。由於我無法理解代碼,我無法修改它以包含LinearLayout。我無法理解的是與像素相關的術語,如Canvas,DrawingCache。所以,如果任何人都可以提供一些基本信息,那就太好了。還幫助我在處理的位圖中包含linearlayout。

+0

https://stackoverflow.com/questions/30085063/take- a-screenshot-of-recyclerview-in-full-length –

+0

https://stackoverflow.com/questions/30085063/take-a-screenshot-of-recyclerview-in-full-length –

回答

1

你可以很容易地拿到它,你不需要在適配器類中編寫代碼,它將在活動類中完成,你也可以截取滾動視圖類型的活動。你必須找到佈局的id recycleView是XML.i'm聲明張貼充分的活動,你只要找到你的代碼初始化,對於recycleview此代碼的工作,以及嵌套的滾動視圖

@Override 
public void onClick(View v) { 
    switch (v.getId()) { 
     case R.id.btn_download: 

      Bitmap bitmap1 = getBitmapFromView(ll_linear); 
      Log.e("ll_linear", "" + ll_linear.getWidth()); 
      Log.e("ll_linear", "" + ll_linear.getHeight()); 
      saveBitmap(bitmap1); 
      break; 

    } 

} 

public void saveBitmap(Bitmap bitmap) { 
    isStoragePermissionGranted(bitmap); 
} 

public boolean isStoragePermissionGranted(Bitmap bitmap) { 

    Date now = new Date(); 
    android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now); 
    String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg"; 

    Log.e("mpath", mPath); 

    File imagePath = new File(mPath); 
    FileOutputStream fos; 

    if (Build.VERSION.SDK_INT >= 23) { 

     if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) 
       == PackageManager.PERMISSION_GRANTED) { 

      try { 
       fos = new FileOutputStream(imagePath); 
       bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos); 
       fos.flush(); 
       fos.close(); 

       Toast.makeText(getApplicationContext(), imagePath.getAbsolutePath() + "", Toast.LENGTH_SHORT).show(); 
       boolean_save = true; 
       btn_download.setText("Check image"); 

      } catch (FileNotFoundException e) { 
       Log.e("Exception", "" + e); 

      } catch (IOException e) { 
       Log.e("Exception", "" + e); 
      } 
      Log.v("Tag", "Permission is granted"); 

      return true; 

     } else { 

      Log.v("Tag", "Permission is revoked"); 
      ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1); 
      return false; 
     } 
    } else { //permission is automatically granted on sdk<23 upon installation 

     File imagePath1 = new File("/sdcard/screenshotdemo.jpg"); 
     FileOutputStream fos1; 

     try { 
      fos1 = new FileOutputStream(imagePath1); 
      bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos1); 
      fos1.flush(); 
      fos1.close(); 
      Toast.makeText(getApplicationContext(), imagePath1.getAbsolutePath() + "", Toast.LENGTH_SHORT).show(); 
      boolean_save = true; 
      btn_download.setText("Check image"); 

     } catch (FileNotFoundException e) { 
      Log.e("Exception", "" + e); 

     } catch (IOException e) { 
      Log.e("Exception", "" + e); 
     } 
     Log.v("Tag", "Permission is granted"); 

     return true; 
    } 

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

這是否適用於Recyclerview,具有可滾動內容?另外如何爲多個視圖做到這一點?如果我在NestedScrollview中結合了LL和RecV,它會給出完整的截圖嗎? –

+0

是的,它的工作原理。是offcourse – farhana

+0

對我來說,它沒有得到完整的截圖。 recyclerview不在NestedScrollView –

相關問題