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

时间:2012-10-05 08:24:26

标签: android screenshot

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?

我创建的新工作代码

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

4 个答案:

答案 0 :(得分:43)

工作代码:

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

答案 1 :(得分:6)

虽然无法制作尚未呈现内容的屏幕截图(如ListView的屏幕外项目),但您可以制作多个屏幕截图,在每个镜头之间滚动内容,然后加入图像。这是一个可以为您自动执行此操作的工具:https://github.com/PGSSoft/scrollscreenshot

illustration

免责声明:我是此工具的作者,由my employer发布。欢迎提出功能请求。

答案 2 :(得分:0)

使用此功能获取列表视图的位图

public Bitmap getBitmapFromView(View view) {

Bitmap returnedBitmap = Bitmap.createBitmap(view.getMeasuredWidth(),
              view.getMeasuredHeight() , Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
Drawable bgDrawable = view.getBackground();
if (bgDrawable != null)
    bgDrawable.draw(canvas);
else
    canvas.drawColor(Color.WHITE);
view.draw(canvas);
        return returnedBitmap;
}

将其用作

Bitmap b = getBitmapFromView(your listview object here);

并根据需要使用此位图

希望有所帮助..

答案 3 :(得分:0)

如果生成的位图具有黑色背景。这是因为您查看无法从xml文件中获取颜色。所以设置你的视图颜色

Ether / IP / ICMP 192.168.1.74 > 192.168.1.84 echo-request 0 / Raw
Ether / IP / ICMP 192.168.1.84 > 192.168.1.74 echo-reply 0 / Raw
Ether / IPv6 / ICMPv6 Echo Request (id: 0x306 seq: 0x3bb)
Ether / IPv6 / ICMPv6 Echo Request (id: 0x306 seq: 0x3bb)
Ether / IPv6 / ICMPv6 Echo Reply (id: 0x306 seq: 0x3bb)
Ether / IPv6 / ICMPv6 Echo Reply (id: 0x306 seq: 0x3bb)
Ether / IP / TCP 192.168.1.84:ssh > 192.168.1.74:61336 PA / Raw
Ether / IP / TCP 192.168.1.74:61336 > 192.168.1.84:ssh A
Ether / IP / ICMP 192.168.1.74 > 192.168.1.84 echo-request 0 / Raw
Ether / IP / ICMP 192.168.1.84 > 192.168.1.74 echo-reply 0 / Raw