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;
}
工作代码:
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的),你可以做一个多屏,滚动每个镜头之间的内容,然后再加入图像。 这里是一个工具,它可以为你自动完成: https://github.com/PGSSoft/scrollscreenshot
免责声明:我是这个工具的作者,它是由出版我的雇主 。 功能要求的欢迎。
使用此功能,让您的列表视图的位图
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();