【问题标题】:Android get screenshot of all ListView itemsAndroid获取所有ListView项目的截图
【发布时间】:2012-10-05 08:24:26
【问题描述】:
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 的项目比屏幕上可见的项目多,则当项目大于可见矩形时,此代码不会捕获。

如何正确捕获我的 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;
}

【问题讨论】:

  • 等待这个问题的答案。
  • @Sharath 检查我的答案
  • @Sharath 检查我更新的代码:getWholeListViewItemsToBitmap() 函数
  • 代码只是给出了一个空白的后屏作为输出
  • 很抱歉这么说。它还没有为我工作。 :( 。我得到了黑色背景,而我的样本背景是白色的。:(

标签: android screenshot


【解决方案1】:

工作代码:

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

【讨论】:

  • 您好,我尝试在我的应用程序中实现您的代码,但遇到了问题。一个是大位图只绘制数组中的第一个元素,另一个是我在使用 bmp.recycle() 时得到“尝试使用回收位图”。你能帮助我吗?详情:stackoverflow.com/questions/21044984/…
  • 这里是 MyActivity.mFocusedListView;它是什么 mFocusedListview ??
  • 我们可以在gridview上使用同样的东西吗?我已经尝试过,但它在 bigcanvas.drawBitmap(bmp, 0, iHeight, paint); 处给了我空指针;有什么想法吗?
  • @lolyoshi 你的问题“试图使用回收位图”可以通过评论 bmp.recycle 来解决,因为 android 不允许重用回收的位图
  • 不错。将其移植到 Xamarin C# 并且效果很好
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    使用此函数获取列表视图的位图

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

    并根据需要使用此位图

    希望帮助..

    【讨论】:

    • 不工作,我正在处理片段,一个片段 - 一个列表视图。此代码与其他代码相同...
    • 当列表有滚动条(更多可见的元素)时,此代码不起作用。
    【解决方案4】:

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

    View v = adapter.getView(i, null, listview);
                v.measure(View.MeasureSpec.makeMeasureSpec(listview.getWidth(), View.MeasureSpec.EXACTLY),
                        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
                v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
                v.setDrawingCacheEnabled(true);
                v.buildDrawingCache(true);
                v.setBackgroundColor(Color.parseColor("#F08080"));
                bmps.add(v.getDrawingCache(true));
                allitemsheight += v.getMeasuredHeight();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多