|
| 1 | +package info.anodsplace.android.image; |
| 2 | + |
| 3 | +import android.app.Activity; |
| 4 | +import android.graphics.Bitmap; |
| 5 | +import android.widget.ImageView; |
| 6 | + |
| 7 | +import java.util.Collections; |
| 8 | +import java.util.Map; |
| 9 | +import java.util.WeakHashMap; |
| 10 | +import java.util.concurrent.ExecutorService; |
| 11 | +import java.util.concurrent.Executors; |
| 12 | + |
| 13 | +import info.anodsplace.android.cache.MemoryCache; |
| 14 | + |
| 15 | +abstract public class ImageLoader { |
| 16 | + |
| 17 | + private MemoryCache mMemoryCache = new MemoryCache(); |
| 18 | + |
| 19 | + private Map<ImageView, String> mImageViews = Collections |
| 20 | + .synchronizedMap(new WeakHashMap<ImageView, String>()); |
| 21 | + |
| 22 | + private ExecutorService mExecutorService; |
| 23 | + |
| 24 | + public ImageLoader() { |
| 25 | + mExecutorService = Executors.newFixedThreadPool(5); |
| 26 | + } |
| 27 | + |
| 28 | + abstract protected Bitmap loadBitmap(String imgUID); |
| 29 | + |
| 30 | + public Bitmap getCachedImage(String imgUID) { |
| 31 | + return mMemoryCache.get(imgUID); |
| 32 | + } |
| 33 | + |
| 34 | + protected void cacheImage(String imgUID, Bitmap bmp) { |
| 35 | + mMemoryCache.put(imgUID, bmp); |
| 36 | + } |
| 37 | + |
| 38 | + public void releaseImageView(ImageView imageView) { |
| 39 | + mImageViews.remove(imageView); |
| 40 | + } |
| 41 | + |
| 42 | + public void loadImage(String imgUID, ImageView imageView) { |
| 43 | + mImageViews.put(imageView, imgUID); |
| 44 | + Bitmap bitmap = mMemoryCache.get(imgUID); |
| 45 | + if (bitmap != null) { |
| 46 | + imageView.setImageBitmap(bitmap); |
| 47 | + } else { |
| 48 | + queuePhoto(imgUID, imageView); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + private void queuePhoto(String imgUID, ImageView imageView) { |
| 53 | + PhotoToLoad p = new PhotoToLoad(imgUID, imageView); |
| 54 | + mExecutorService.submit(new PhotosLoader(p)); |
| 55 | + } |
| 56 | + |
| 57 | + //Task for the queue |
| 58 | + private class PhotoToLoad { |
| 59 | + |
| 60 | + public String imgUID; |
| 61 | + |
| 62 | + public ImageView imageView; |
| 63 | + |
| 64 | + public PhotoToLoad(String u, ImageView i) { |
| 65 | + imgUID = u; |
| 66 | + imageView = i; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + class PhotosLoader implements Runnable { |
| 71 | + |
| 72 | + PhotoToLoad mPhotoToLoad; |
| 73 | + |
| 74 | + PhotosLoader(PhotoToLoad photoToLoad) { |
| 75 | + mPhotoToLoad = photoToLoad; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void run() { |
| 80 | + if (imageViewReused(mPhotoToLoad)) { |
| 81 | + return; |
| 82 | + } |
| 83 | + Bitmap bmp = loadBitmap(mPhotoToLoad.imgUID); |
| 84 | + cacheImage(mPhotoToLoad.imgUID, bmp); |
| 85 | + if (imageViewReused(mPhotoToLoad)) { |
| 86 | + return; |
| 87 | + } |
| 88 | + Activity a = (Activity) mPhotoToLoad.imageView.getContext(); |
| 89 | + if (bmp == null) { |
| 90 | + mImageViews.remove(mPhotoToLoad.imageView); |
| 91 | + return; |
| 92 | + } |
| 93 | + BitmapDisplayer bd = new BitmapDisplayer(bmp, mPhotoToLoad); |
| 94 | + a.runOnUiThread(bd); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + boolean imageViewReused(PhotoToLoad photoToLoad) { |
| 99 | + String tag = mImageViews.get(photoToLoad.imageView); |
| 100 | + if (tag == null || !tag.equals(photoToLoad.imgUID)) { |
| 101 | + return true; |
| 102 | + } |
| 103 | + return false; |
| 104 | + } |
| 105 | + |
| 106 | + //Used to display mBitmap in the UI thread |
| 107 | + class BitmapDisplayer implements Runnable { |
| 108 | + |
| 109 | + Bitmap mBitmap; |
| 110 | + |
| 111 | + PhotoToLoad mPhotoToLoad; |
| 112 | + |
| 113 | + public BitmapDisplayer(Bitmap b, PhotoToLoad p) { |
| 114 | + mBitmap = b; |
| 115 | + mPhotoToLoad = p; |
| 116 | + } |
| 117 | + |
| 118 | + public void run() { |
| 119 | + if (imageViewReused(mPhotoToLoad)) { |
| 120 | + return; |
| 121 | + } |
| 122 | + if (mBitmap != null) { |
| 123 | + mPhotoToLoad.imageView.setImageBitmap(mBitmap); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + public void clearCache() { |
| 129 | + mMemoryCache.clear(); |
| 130 | + } |
| 131 | + |
| 132 | +} |
0 commit comments