Android获取所有ListView项目的屏幕截图

发布于 2021-01-30 17:22:45

View v = rootView.findViewById(R.id.layout1);
if (v != null) {
    v.buildDrawingCache();
    Bitmap bitmap = v.getDrawingCache();
    canvas.drawBitmap(bitmap, dummyMatrix, null);
    v.destroyDrawingCache();   
}

我有这个代码。但是我需要对所有ListView项进行屏幕截图,但是如果我的Listview的项多于屏幕上的可见项,则当这些项大于可见的rect时,不会捕获此代码。

如何正确捕获我的ListView?

我创建的新工作代码

public static Bitmap getWholeListViewItemsToBitmap() {

    ListView listview    = MyActivity.mFocusedListView;
    ListAdapter adapter  = listview.getAdapter(); 
    int itemscount       = adapter.getCount();
    int allitemsheight   = 0;
    List<Bitmap> bmps    = new ArrayList<Bitmap>();

    for (int i = 0; i < itemscount; i++) {

        View childView      = adapter.getView(i, null, listview);
        childView.measure(MeasureSpec.makeMeasureSpec(listview.getWidth(), MeasureSpec.EXACTLY), 
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

        childView.layout(0, 0, childView.getMeasuredWidth(), childView.getMeasuredHeight());
        childView.setDrawingCacheEnabled(true);
        childView.buildDrawingCache();
        bmps.add(childView.getDrawingCache());
        allitemsheight+=childView.getMeasuredHeight();
    }

    Bitmap bigbitmap    = Bitmap.createBitmap(listview.getMeasuredWidth(), allitemsheight, Bitmap.Config.ARGB_8888);
    Canvas bigcanvas    = new Canvas(bigbitmap);

    Paint paint = new Paint();
    int iHeight = 0;

    for (int i = 0; i < bmps.size(); i++) {
        Bitmap bmp = bmps.get(i);
        bigcanvas.drawBitmap(bmp, 0, iHeight, paint);
        iHeight+=bmp.getHeight();

        bmp.recycle();
        bmp=null;
    }


    return bigbitmap;
}
关注者
0
被浏览
395
1 个回答
  • 面试哥
    面试哥 2021-01-30
    为面试而生,有面试问题,就找面试哥。

    工作代码:

    public static Bitmap getWholeListViewItemsToBitmap() {
    
        ListView listview    = MyActivity.mFocusedListView;
        ListAdapter adapter  = listview.getAdapter(); 
        int itemscount       = adapter.getCount();
        int allitemsheight   = 0;
        List<Bitmap> bmps    = new ArrayList<Bitmap>();
    
        for (int i = 0; i < itemscount; i++) {
    
            View childView      = adapter.getView(i, null, listview);
            childView.measure(MeasureSpec.makeMeasureSpec(listview.getWidth(), MeasureSpec.EXACTLY), 
                    MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    
            childView.layout(0, 0, childView.getMeasuredWidth(), childView.getMeasuredHeight());
            childView.setDrawingCacheEnabled(true);
            childView.buildDrawingCache();
            bmps.add(childView.getDrawingCache());
            allitemsheight+=childView.getMeasuredHeight();
        }
    
        Bitmap bigbitmap    = Bitmap.createBitmap(listview.getMeasuredWidth(), allitemsheight, Bitmap.Config.ARGB_8888);
        Canvas bigcanvas    = new Canvas(bigbitmap);
    
        Paint paint = new Paint();
        int iHeight = 0;
    
        for (int i = 0; i < bmps.size(); i++) {
            Bitmap bmp = bmps.get(i);
            bigcanvas.drawBitmap(bmp, 0, iHeight, paint);
            iHeight+=bmp.getHeight();
    
            bmp.recycle();
            bmp=null;
        }
    
    
        return bigbitmap;
    }
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看