Android-Universal-Image-Loader 學習筆記(二)載入圖片原理
阿新 • • 發佈:2019-02-06
public void displayImage(String uri, ImageAware imageAware, DisplayImageOptions options, ImageLoadingListener listener, ImageLoadingProgressListener progressListener) { //首先檢測UIL的配置是否被初始化,在App的application中設定 checkConfiguration(); if (imageAware == null) { throw new IllegalArgumentException(ERROR_WRONG_ARGUMENTS); } if (listener == null) { // 設定一個預設的監聽器 SimpleImageLoadingListener listener = emptyListener; } if (options == null) { // 當沒有options,使用預設 options = configuration.defaultDisplayImageOptions; } if (TextUtils.isEmpty(uri)) { //當uri是null,顯示預設圖片或者不顯示 engine.cancelDisplayTaskFor(imageAware); listener.onLoadingStarted(uri, imageAware.getWrappedView()); if (options.shouldShowImageForEmptyUri()) { imageAware.setImageDrawable(options.getImageForEmptyUri(configuration.resources)); } else { imageAware.setImageDrawable(null); } listener.onLoadingComplete(uri, imageAware.getWrappedView(), null); return; } // 解析ImageView的大小,以便後面解析圖片用 ImageSize targetSize = ImageSizeUtils.defineTargetSizeForView(imageAware, configuration.getMaxImageSize()); String memoryCacheKey = MemoryCacheUtils.generateKey(uri, targetSize); engine.prepareDisplayTaskFor(imageAware, memoryCacheKey);<span style="font-family: Arial; font-size: 14px; line-height: 26px;">//ImageLoaderEngine中存在一個HashMap,用來記錄正在載入的任務</span> listener.onLoadingStarted(uri, imageAware.getWrappedView()); // Bitmap 是否已經存在快取 Bitmap bmp = configuration.memoryCache.get(memoryCacheKey); if (bmp != null && !bmp.isRecycled()) { //Bitmap 存在cache快取中,取出來用 L.d(LOG_LOAD_IMAGE_FROM_MEMORY_CACHE, memoryCacheKey); if (options.shouldPostProcess()) { ImageLoadingInfo imageLoadingInfo = new ImageLoadingInfo(uri, imageAware, targetSize, memoryCacheKey, options, listener, progressListener, engine.getLockForUri(uri)); //處理並顯示圖片 ProcessAndDisplayImageTask displayTask = new ProcessAndDisplayImageTask(engine, bmp, imageLoadingInfo, defineHandler(options)); if (options.isSyncLoading()) { displayTask.run(); } else { engine.submit(displayTask); } } else { Log.d("wutingyou....", "ImageLoader from Cache"); // 顯示圖片,根據顯示方式顯示 預設SimpleBitmapDisplayer options.getDisplayer().display(bmp, imageAware, LoadedFrom.MEMORY_CACHE); // 回撥完成介面 listener.onLoadingComplete(uri, imageAware.getWrappedView(), bmp); } } else { //Bitmap 不存在cache快取中,從檔案中或者網路裡面獲取bitmap物件 if (options.shouldShowImageOnLoading()) { // 顯示我預設載入的下載時顯示圖片 imageAware.setImageDrawable(options.getImageOnLoading(configuration.resources)); } else if (options.isResetViewBeforeLoading()) { // 什麼都不顯示 imageAware.setImageDrawable(null); } ImageLoadingInfo imageLoadingInfo = new ImageLoadingInfo(uri, imageAware, targetSize, memoryCacheKey, options, listener, progressListener, engine.getLockForUri(uri)); //啟動一個執行緒,載入並顯示圖片 LoadAndDisplayImageTask displayTask = new LoadAndDisplayImageTask(engine, imageLoadingInfo, defineHandler(options)); if (options.isSyncLoading()) { //是否同步載入 displayTask.run(); } else { engine.submit(displayTask); } } }
從上面的程式碼中,我們可以看出,它會將ImageView轉換成ImageViewAware, ImageViewAware主要是做什麼的呢?該類主要是將ImageView進行一個包裝,將ImageView的強引用變成弱引用,當記憶體不足的時候,可以更好的回收ImageView物件,還有就是獲取ImageView的寬度和高度。這使得我們可以根據ImageView的寬高去對圖片進行一個裁剪,減少記憶體的使用。