拍摄LinearLayout和RecyclerView的屏幕截图 [英] Taking screenshot of LinearLayout and RecyclerView

查看:76
本文介绍了拍摄LinearLayout和RecyclerView的屏幕截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面有一个LinearLayout和一个RecyclerView.在Google上搜索时,我发现了一些代码来拍摄RecyclerView的屏幕截图(说实话,我不明白它是如何工作的).这是代码:

I have a LinearLayout and a RecyclerView just below it. Searching on Google, I found some code to take a screenshot of RecyclerView (To be true, I couldn't understand how it works). Here's the code:

  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.

Now I want to include the LinearLayout in it too which is just above the RecyclerView. Since I couldn't understand the code, I can't modify it to include LinearLayout. What I couldn't understand are the terms related to Bitmap like Canvas, DrawingCache. So If anyone can give some basic info, that would be great. Also help me include linearlayout in the processed Bitmap.

推荐答案

您可以轻松地进行操作,无需在适配器类中进行编码,这将在活动类中完成,并且还可以获取滚动视图类型的屏幕截图activity.只是您必须找到XML中声明了RecycleView的布局ID.我发布完整的活动后,您只是找到了代码init,此代码可用于recycleview和嵌套滚动视图

you can take it easily, you wouldn't need to code in adapter class it will be done in activity class and also you can take screenshot of scroll view type activity.just you have to find the id of layout in which RecycleView is declare in XML.i'm posting full activity you just find your code init, this code work for recycleview as well as nested scroll view

 @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;
}

这篇关于拍摄LinearLayout和RecyclerView的屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆