我需要以编程方式创建 ListView 并获取它的位图。我不需要在屏幕上绘制它,我只需要从其他屏幕准备好适配器和数据:
ListView listView1;
LinearLayout ll = new LinearLayout(applicationGlobalState.getActiveContext(QuickTestResultsActivity.class));
listView1 = new ListView(applicationGlobalState.getActiveContext(QuickTestResultsActivity.class));
ll.addView(listView1, LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
listView1.setBackgroundColor(R.color.orange);
ListAdapter adapter = listView1.getAdapter();
int itemscount = adapter.getCount();
int allitemsheight = 0;
List<Bitmap> bmps = new ArrayList<Bitmap>();
Bitmap bigbitmap = null;
try{
for (int i = 0; i < itemscount; i++) {
View childView = adapter.getView(i, null, listView1);
childView.setBackgroundColor(R.color.orange);
childView.measure(View.MeasureSpec.makeMeasureSpec(listView1.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();
}
//////
int m0 =listView1.getMeasuredWidth();
int m = allitemsheight;
//bigbitmap = Bitmap.createBitmap(listView1.getMeasuredWidth(), allitemsheight, Bitmap.Config.ARGB_8888);
bigbitmap = Bitmap.createBitmap(800, 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;
}
}catch(Exception e){Log.e("¬","",e);}
Bitmap bmp = bigbitmap;
FileOutputStream fos = new FileOutputStream("/data/data/co.uk.orange.application/app_temp_results/aps" +(System.currentTimeMillis()/1000)+".png", true);
applicationGlobalState.wifiScreenshotPath="/data/data/co.uk.orange.application/app_temp_results/aps" +(System.currentTimeMillis()/1000)+".png";
bmp.compress(CompressFormat.PNG, 75, fos);
bmp.recycle();
给我一个例外:“bmps.get(i)”为空;
01-04 19:08:49.843: ERROR/¬(1549): java.lang.NullPointerException
at android.graphics.Canvas.throwIfRecycled(Canvas.java:954)
at android.graphics.Canvas.drawBitmap(Canvas.java:980)
at co.uk.orange.application.asynctasks.WifiAPPicture.getDrawingBitmap(WifiAPPicture.java:121)
at co.uk.orange.application.asynctasks.WifiAPPicture.onCreate(WifiAPPicture.java:82)
at co.uk.orange.application.asynctasks.WifiAPsTestAsyncTask.doInBackground(WifiAPsTestAsyncTask.java:100)
at co.uk.orange.application.asynctasks.WifiAPsTestAsyncTask.doInBackground(WifiAPsTestAsyncTask.java:20)
at android.os.AsyncTask$2.call(AsyncTask.java:185)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
at java.lang.Thread.run(Thread.java:1096)