【发布时间】:2014-06-19 18:38:36
【问题描述】:
我有这个 sn-p 的代码,我想对其进行优化。我有一个由 OSMdroid 库定期调用的方法来加载大量的地图。此方法直接调用文件流并直接加载位图,并且在主 UI 线程上加载后将返回位图。
虽然我已经设法使用AsyncTask 和并行执行器在后台运行。有时,在 mapview 中有大量覆盖(逐项)时,此代码的 sn-p 运行速度较慢,因为定期触发 GC_FO_ALLOC 进行分配,并且在我的日志消息中我得到 Grow Heap (frag case)。我尝试了很多方法来解决,但都不够有效。出于某种原因,这个任务在主线程上执行是我的感觉,因为在我的日志消息中我也得到了Skipped xx frames, the application may be doing lot of task。知道如何使这变得更好吗?问题是方法必须返回,一旦加载,我怎么能让这个方法等到地图视图没有平移或缩放,然后加载图块?
@SuppressWarnings("deprecation")
@Override
public Drawable getDrawable(final InputStream aFileInputStream) throws LowMemoryException {
try {
df = new DisplayFile();
df.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, aFileInputStream);
return new BitmapDrawable(df.get());
} catch (final OutOfMemoryError e) {
System.gc();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
return null;
}
private class DisplayFile extends AsyncTask<InputStream, Bitmap, Bitmap> {
InputStream path;
@Override
protected Bitmap doInBackground(InputStream... arg0) {
path = arg0[0];
BitmapFactory.Options mBitOpt = new BitmapFactory.Options();
mBitOpt.inDither = false;
mBitOpt.inSampleSize = 1;
mBitOpt.inPurgeable = true;
mBitOpt.inInputShareable = true;
mBitOpt.inPreferredConfig = Bitmap.Config.ARGB_8888;
final Bitmap mBitmap = BitmapFactory.decodeStream(path,null,mBitOpt);
return mBitmap;
}
}
【问题讨论】:
标签: android multithreading bitmap android-asynctask osmdroid