| 38 | * 图片加载控制 |
| 39 | */ |
| 40 | /*package*/ final class ImageLoader implements |
| 41 | Callback.PrepareCallback<File, Drawable>, |
| 42 | Callback.CacheCallback<Drawable>, |
| 43 | Callback.ProgressCallback<Drawable>, |
| 44 | Callback.TypedCallback<Drawable>, |
| 45 | Callback.Cancelable { |
| 46 | |
| 47 | private MemCacheKey key; |
| 48 | private ImageOptions options; |
| 49 | private WeakReference<ImageView> viewRef; |
| 50 | private int fileLockedExceptionRetryCount = 0; |
| 51 | |
| 52 | private final static AtomicLong SEQ_SEEK = new AtomicLong(0); |
| 53 | private final long seq = SEQ_SEEK.incrementAndGet(); |
| 54 | |
| 55 | private volatile boolean stopped = false; |
| 56 | private volatile boolean cancelled = false; |
| 57 | private volatile boolean skipOnWaitingCallback = false; |
| 58 | private volatile boolean skipOnFinishedCallback = false; |
| 59 | private Callback.Cancelable httpCancelable; |
| 60 | private Callback.CommonCallback<Drawable> callback; |
| 61 | private Callback.PrepareCallback<File, Drawable> prepareCallback; |
| 62 | private Callback.CacheCallback<Drawable> cacheCallback; |
| 63 | private Callback.ProgressCallback<Drawable> progressCallback; |
| 64 | |
| 65 | private final static String DISK_CACHE_DIR_NAME = "xUtils_img"; |
| 66 | private final static Executor EXECUTOR = new PriorityExecutor(10, false); |
| 67 | private final static int MEM_CACHE_MIN_SIZE = 1024 * 1024 * 4; // 4M |
| 68 | private final static LruCache<MemCacheKey, Drawable> MEM_CACHE = |
| 69 | new LruCache<MemCacheKey, Drawable>(MEM_CACHE_MIN_SIZE) { |
| 70 | private boolean deepClear = false; |
| 71 | |
| 72 | @Override |
| 73 | protected int sizeOf(MemCacheKey key, Drawable value) { |
| 74 | if (value instanceof BitmapDrawable) { |
| 75 | Bitmap bitmap = ((BitmapDrawable) value).getBitmap(); |
| 76 | return bitmap == null ? 0 : bitmap.getByteCount(); |
| 77 | } else if (value instanceof GifDrawable) { |
| 78 | return ((GifDrawable) value).getByteCount(); |
| 79 | } |
| 80 | return super.sizeOf(key, value); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public void trimToSize(int maxSize) { |
| 85 | if (maxSize < 0) { |
| 86 | deepClear = true; |
| 87 | } |
| 88 | super.trimToSize(maxSize); |
| 89 | deepClear = false; |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | protected void entryRemoved(boolean evicted, MemCacheKey key, Drawable oldValue, Drawable newValue) { |
| 94 | super.entryRemoved(evicted, key, oldValue, newValue); |
| 95 | if (evicted && deepClear && oldValue instanceof ReusableDrawable) { |
| 96 | ((ReusableDrawable) oldValue).setMemCacheKey(null); |
| 97 | } |