我想基于ListView创建XML文件。现在我正在创建一个ListView的Bitmap,使其成为drawable,然后将其设置为相对布局的背景。像这样......
public Bitmap getWholeListViewItemsToBitmap() {
ListView listview = (ListView) findViewById(R.id.hourList);
line.setDrawingCacheEnabled(true);
line.buildDrawingCache();
line.layout(0, 0, line.getMeasuredWidth(), line.getMeasuredHeight());
Bitmap linebmp = line.getDrawingCache().copy(Bitmap.Config.ARGB_8888, false);
ListAdapter adapter = listview.getAdapter();
int itemscount = adapter.getCount();
int allitemsheight = 0;
List<Bitmap> bmps = new ArrayList<>();
for (int i = 0; i < itemscount; i++) {
View childView = adapter.getView(i, null, listview);
childView.measure(View.MeasureSpec.makeMeasureSpec(listview.getWidth(), View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
childView.layout(0, 0, childView.getMeasuredWidth(), childView.getMeasuredHeight());
childView.setDrawingCacheEnabled(true);
childView.buildDrawingCache();
bmps.add(childView.getDrawingCache());
allitemsheight+=childView.getMeasuredHeight();
allitemsheight+=line.getMeasuredHeight();
}
allitemsheight-=line.getMeasuredHeight();
Bitmap bigbitmap = Bitmap.createBitmap(listview.getMeasuredWidth(), allitemsheight, Bitmap.Config.ARGB_8888);
Canvas bigcanvas = new Canvas(bigbitmap);
Paint paint = new Paint();
int iHeight = 0;
// This is where the Bitmap is created
for (int i = 0; i < bmps.size(); i++) {
Bitmap bmp = bmps.get(i);
bigcanvas.drawBitmap(bmp, 0, iHeight, paint);
bigcanvas.drawBitmap(linebmp, 0, iHeight, paint);
iHeight += bmp.getHeight();
iHeight += linebmp.getHeight();
bmp.recycle();
bmp = null;
}
return bigbitmap;
}
我在ListView中获取每个列表的位图,然后将它们放在一个大的Bitmap中(中间有行)。这是结果......
但是在内存中加载的这么大的图像会导致很多延迟......所以我想知道如何使用XML布局来做类似的事情。如果可能的话,我希望将ListView中的每个Item添加到XML布局中的单个视图/项目中。所以我可以根据不同的因素隐藏或显示每个项目。
如果您有任何建议,非常感谢,谢谢!