我在其下面有一个LinearLayout和一个RecyclerView。在Google上搜索时,我发现了一些代码来截取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。

最佳答案

您可以轻松地做到这一点,无需在适配器类中进行编码,而将在活动类中完成,也可以获取滚动视图类型的活动的屏幕快照。只需要找到在其中声明了RecycleView的布局ID即可XML.i发布了完整的活动,您只需要找到代码初始化即可,此代码可用于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;
}

关于android - 拍摄LinearLayout和RecyclerView的屏幕截图,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45421252/

10-13 06:59