尝试截取动态列表视图,这是我的代码
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;
}
这里我得到nullpointer异常(listview.getAdapter())任何人都可以建议我如何解决这个问题。 关注this also
答案 0 :(得分:0)
无法真正看到代码,但您的listview为null。
ListView listview = MyActivity.mFocusedListView;
ListView listview = (ListView)findById(R.id.YOURLISTVIEW);
这就是为什么它是null。发布您的xml,以便我们可以看到ID是什么。
答案 1 :(得分:0)
虽然无法制作尚未渲染的内容(如ListView的屏幕外项目)的屏幕截图,但您可以制作多个屏幕截图,在每个镜头之间滚动内容,然后缝合图像。这是一个可以为您自动执行此操作的工具:https://github.com/PGSSoft/scrollscreenshot
免责声明:我是此工具的作者,由my employer发布。欢迎提出功能请求。