Android获取所有ListView项目的截图

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?

由我创build的新工作代码

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

Solutions Collecting From Web of "Android获取所有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; } 

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

插图

免责声明:我是这个工具的作者,是由我的雇主出版的。 function请求是受欢迎的。

使用这个函数来获取你的列表视图的位图

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

并根据需要使用此位图

希望帮助..

如果你生成的位图有黑色背景。 这是因为您查看无法从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();