首先,我要感谢您的 cmets 和有关解决方案的想法。
过去两天我正在研究这个问题,当我问你的时候,所以实际上我想出了我的最终解决方案,当我在这里发布时已经部分准备好了:
public class IdeaAdapter extends ArrayAdapter<Idea> {
private final Context context;
private final Display display;
private List<Idea> dataList;
private Set<String> imagesInProgress;
public IdeaAdapter(Display display, Activity c, List<Idea> dataList) {
super(c, android.R.layout.simple_list_item_1, dataList);
this.dataList = dataList;
this.context = c;
this.display = display;
imagesInProgress = new HashSet<>();
}
@Override
public int getCount() {
return dataList.size();
}
@Override
public Idea getItem(int position) {
return dataList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder vh;
if (convertView == null) {
vh = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.idea_list_item, null);
vh.ideaImage = (ImageView) convertView.findViewById(R.id.idea_image);
vh.ideaName = (TextView) convertView.findViewById(R.id.idea_title);
vh.progressWheel = (ProgressWheel) convertView.findViewById(R.id.progress_wheel);
convertView.setTag(vh);
} else {
vh = (ViewHolder) convertView.getTag();
}
final Idea ideaItem = dataList.get(position);
vh.ideaName.setText(ideaItem.getTitle());
if (ideaItem.getImages().size() > 0) {
final String imageId = ideaItem.getImages().get(0);
Log.e(" isContains", String.valueOf(ImagesCache.getInstance().containsKey(String.valueOf(imageId))));
if (!ImagesCache.getInstance().containsKey(String.valueOf(imageId)) && !imagesInProgress.contains(imageId)) {
Log.e(" downloading", String.valueOf(imageId));
vh.ideaImage.setImageBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher));
imagesInProgress.add(imageId);
new DownloadImageAsyncTask(vh.ideaImage, display, imageId).execute();
} else {
Log.e("CACHE", String.valueOf(imageId));
vh.ideaImage.setImageBitmap(ImagesCache.getInstance().getBitmap(String.valueOf(imageId)));
}
}
return convertView;
}
private static class ViewHolder {
private ImageView ideaImage;
private TextView ideaName;
private ProgressWheel progressWheel;
}
}
图像缓存:
public class ImagesCache {
private static ImagesCache INSTANCE;
private static Context context;
private static DiskLruCache mDiskCache;
private static Bitmap.CompressFormat mCompressFormat = Bitmap.CompressFormat.JPEG;
private static int mCompressQuality = 70;
private static final int APP_VERSION = 1;
private static final int VALUE_COUNT = 1;
private static final String TAG = "DiskLruImageCache";
private static final String uniqueName = "IdeasCache";
private static final int diskCacheSize = 20*1024*1024; // in bytes
/**
* Make sure to give an application context to me.
* I like to keep it as a static reference
*
* @param c
*/
public static void init(Context c) {
context = c;
}
public static ImagesCache getInstance() {
if (INSTANCE == null) {
INSTANCE = new ImagesCache();
}
return INSTANCE;
}
private ImagesCache() {
try {
final File diskCacheDir = getDiskCacheDir(context, uniqueName);
mDiskCache = DiskLruCache.open(diskCacheDir, APP_VERSION, VALUE_COUNT, diskCacheSize);
/*mCompressFormat = compressFormat;
mCompressQuality = quality;*/
} catch (IOException e) {
e.printStackTrace();
}
}
private static boolean writeBitmapToFile(Bitmap bitmap, DiskLruCache.Editor editor)
throws IOException {
OutputStream out = null;
try {
out = new BufferedOutputStream(editor.newOutputStream(0), Utils.IO_BUFFER_SIZE);
return bitmap.compress(mCompressFormat, mCompressQuality, out);
} finally {
if (out != null) {
out.close();
}
}
}
private File getDiskCacheDir(Context context, String uniqueName) {
// Check if media is mounted or storage is built-in, if so, try and use external cache dir
// otherwise use internal cache dir
final String cachePath =
Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) ||
!Utils.isExternalStorageRemovable() ?
Utils.getExternalCacheDir(context).getPath() :
context.getCacheDir().getPath();
return new File(cachePath + File.separator + uniqueName);
}
public static void put(String key, Bitmap data) {
DiskLruCache.Editor editor = null;
try {
editor = mDiskCache.edit(key);
if (editor == null) {
return;
}
if (writeBitmapToFile(data, editor)) {
mDiskCache.flush();
editor.commit();
if (BuildConfig.DEBUG) {
Log.d("cache_test_DISK_", "image put on disk cache " + key);
}
} else {
editor.abort();
if (BuildConfig.DEBUG) {
Log.d("cache_test_DISK_", "ERROR on: image put on disk cache " + key);
}
}
} catch (IOException e) {
if (BuildConfig.DEBUG) {
Log.d("cache_test_DISK_", "ERROR on: image put on disk cache " + key);
}
try {
if (editor != null) {
editor.abort();
}
} catch (IOException ignored) {
}
}
}
public static Bitmap getBitmap(String key) {
Bitmap bitmap = null;
DiskLruCache.Snapshot snapshot = null;
try {
snapshot = mDiskCache.get(key);
if (snapshot == null) {
return null;
}
final InputStream in = snapshot.getInputStream(0);
if (in != null) {
final BufferedInputStream buffIn =
new BufferedInputStream(in, Utils.IO_BUFFER_SIZE);
bitmap = BitmapFactory.decodeStream(buffIn);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (snapshot != null) {
snapshot.close();
}
}
if (BuildConfig.DEBUG) {
Log.d("cache_test_DISK_", bitmap == null ? "" : "image read from disk " + key);
}
return bitmap;
}
public static boolean containsKey(String key) {
boolean contained = false;
DiskLruCache.Snapshot snapshot = null;
try {
snapshot = mDiskCache.get(key);
contained = snapshot != null;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (snapshot != null) {
snapshot.close();
}
}
return contained;
}
public static void clearCache() {
if (BuildConfig.DEBUG) {
Log.d("cache_test_DISK_", "disk cache CLEARED");
}
try {
mDiskCache.delete();
} catch (IOException e) {
e.printStackTrace();
}
}
public static File getCacheFolder() {
return mDiskCache.getDirectory();
}
}
我需要做的就是管理线程(阻塞队列)以不为每个 getView 调用触发!