主流的加載圖片框架有UIL(Universal-Image-Loader)、Picasso、Glide、Fresco,它們之間的對比可以參照文章全面瞭解Android主流圖片加載庫。下面來介紹Picasso圖片加載庫。
一、簡介
Picasso是一個強大的圖片下載和緩存來源庫。使用Picasso非常簡單,只需一行代碼:
使用Picasso加載圖片很容易和方便,只需一行代碼就夠了。
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
Picasso不僅實現了圖片異步加載的功能,還解決了android中加載圖片時需要解決的一些常見問題:
1. 在adapter中需要取消已經不在視野範圍的ImageView圖片資源的加載,否則會導致圖片錯位,Picasso已經解決了這個問題。
2. 使用複雜的圖片壓縮轉換來儘可能的減少內存消耗;
3. 自帶內存和硬盤二級緩存功能;
特性
- Adapter中下載
Adapter的重用會被自動檢測到,Picasso會取消上次的加載。
@Override
public void getView(int position, View convertView, ViewGroup parent) {
SquaredImageView view = (SquaredImageView) convertView;
if (view == null) {
view = new SquaredImageView(context);**
}
String url = getItem(position);
Picasso.with(context).load(url).into(view);
}
- 圖片轉換
轉換圖片以適應佈局大小並減少內存佔用
Picasso.with(context)
.load(url)
.resize(50, 50)
.centerCrop()
.into(imageView);
你還可以自定義轉換:
public class CropSquareTransformation implements Transformation {
@Override public Bitmap transform(Bitmap source) {
int size = Math.min(source.getWidth(), source.getHeight());
int x = (source.getWidth() - size) / 2;
int y = (source.getHeight() - size) / 2;
Bitmap result = Bitmap.createBitmap(source, x, y, size, size);
if (result != source) {
source.recycle();
}
return result;
}
@Override public String key() { return "square()"; }
}
將CropSquareTransformation 的對象傳遞給transform 方法即可。
- Place holder
空白或者錯誤佔位圖片:picasso提供了兩種佔位圖片,未加載完成或者加載發生錯誤的時需要一張圖片作爲提示。
Picasso.with(context)
.load(url)
.placeholder(R.drawable.user_placeholder)
.error(R.drawable.user_placeholder_error)
.into(imageView);
如果加載發生錯誤會重複三次請求,三次都失敗纔會顯示error Place holder
- 資源文件加載
除了加載網絡圖片picasso還支持加載Resources, assets, files, content providers中的資源文件。
Picasso.with(context).load(R.drawable.landing_screen).into(imageView1);
Picasso.with(context).load(new File(...)).into(imageView2);
下面從圖片的加載過程來分析Picasso的源碼。
二、源碼分析
源碼的分析分爲加載圖片和取消加載兩個部分,首先來看加載圖片的過程。
2.1 加載圖片
2.1.1 構造Picasso實例對象
Picasso實例對象不是通過默認的構造器來構建的,而是通過內部類Bulider方式來構建的,這樣可以方便用戶自定義構建Picasso對象。默認方法是通過with來創建。
/*
* 構造一個全局的Picasso實例對象,默認實現;
* 該實例自動初始化默認的配置,適用於大多數場景:
* 1,LRU內存緩存佔用應用RAM的15%
* 2,磁盤緩存佔用外置存儲空間的2%,至少5MB,至多50MB。只在API 14以上纔可以,或者是提供磁盤緩存的獨立庫,如OKHttp。
* 3,三個下載線程用來訪問磁盤或者網絡元素。
*
* 如果上面的配置滿足不了你的需求,則可以通過Picasso.Builder來自定義創建一個Picasso對象。
*/
public static Picasso with(@NonNull Context context) {
if (context == null) {
throw new IllegalArgumentException("context == null");
}
if (singleton == null) {
synchronized (Picasso.class) {
if (singleton == null) {
singleton = new Builder(context).build();
}
}
}
return singleton;
}
/*
* 創建一個Picasso實例對象
*/
public Picasso build() {
Context context = this.context;
if (downloader == null) {
//網絡下載
downloader = new OkHttp3Downloader(context);
}
if (cache == null) {
//內存緩存,使用1/8的可用堆內存作爲內存緩存
cache = new LruCache(context);
}
if (service == null) {
//ExecutorService服務,默認由3個線程組成
service = new PicassoExecutorService();
}
if (transformer == null) {
//請求轉換器,默認是不作任何處理
transformer = RequestTransformer.IDENTITY;
}
//狀態管理器
Stats stats = new Stats(cache);
//分發器
Dispatcher dispatcher = new Dispatcher(context, service, HANDLER, downloader, cache, stats);
return new Picasso(context, dispatcher, cache, listener, transformer, requestHandlers, stats,
defaultBitmapConfig, indicatorsEnabled, loggingEnabled);
}
Builder可以組裝構建Picasso的各種參數,例如定義內存緩存的大小。
/*
* 自定義內存緩存
*/
public Builder memoryCache(@NonNull Cache memoryCache) {
if (memoryCache == null) {
throw new IllegalArgumentException("Memory cache must not be null.");
}
if (this.cache != null) {
throw new IllegalStateException("Memory cache already set.");
}
this.cache = memoryCache;
return this;
}
Picasso的構造函數如下:
/*
* Picasso的構造函數
*/
Picasso(Context context, Dispatcher dispatcher, Cache cache, Listener listener,
RequestTransformer requestTransformer, List<RequestHandler> extraRequestHandlers, Stats stats,
Bitmap.Config defaultBitmapConfig, boolean indicatorsEnabled, boolean loggingEnabled) {
this.context = context;
this.dispatcher = dispatcher;
this.cache = cache;
this.listener = listener;
this.requestTransformer = requestTransformer;
this.defaultBitmapConfig = defaultBitmapConfig;
int builtInHandlers = 7; // 內部處理請求的Hanlder數量
int extraCount = (extraRequestHandlers != null ? extraRequestHandlers.size() : 0);
List<RequestHandler> allRequestHandlers = new ArrayList<>(builtInHandlers + extraCount);
//ResourceRequestHandler必須放置在第一個位置
allRequestHandlers.add(new ResourceRequestHandler(context));
if (extraRequestHandlers != null) {
allRequestHandlers.addAll(extraRequestHandlers);
}
allRequestHandlers.add(new ContactsPhotoRequestHandler(context));
allRequestHandlers.add(new MediaStoreRequestHandler(context));
allRequestHandlers.add(new ContentStreamRequestHandler(context));
allRequestHandlers.add(new AssetRequestHandler(context));
allRequestHandlers.add(new FileRequestHandler(context));
allRequestHandlers.add(new NetworkRequestHandler(dispatcher.downloader, stats));
requestHandlers = Collections.unmodifiableList(allRequestHandlers);
this.stats = stats;
this.targetToAction = new WeakHashMap<>();
this.targetToDeferredRequestCreator = new WeakHashMap<>();
this.indicatorsEnabled = indicatorsEnabled;
this.loggingEnabled = loggingEnabled;
this.referenceQueue = new ReferenceQueue<>();
this.cleanupThread = new CleanupThread(referenceQueue, HANDLER);
this.cleanupThread.start();
}
2.1.2 利用load方法加載圖片
Picasso支持多種加載圖片的方式,例如根據網絡Uri來加載圖片,或者是加載ResourceId指定的圖片等。所以load方法被重載了,有多種實現方式:
/*
* 利用指定的Uri來加載圖片
*/
public RequestCreator load(@Nullable Uri uri) {
return new RequestCreator(this, uri, 0);
}
/*
* 通過指定的圖片文件來加載圖片
* 等價於調用load(Uri)方法
*/
public RequestCreator load(@NonNull File file) {
if (file == null) {
return new RequestCreator(this, null, 0);
}
return load(Uri.fromFile(file));
}
/*
* 通過指定的路徑來加載圖片
* 該方法等價調用Load(Uri)
*/
public RequestCreator load(@Nullable String path) {
if (path == null) {
return new RequestCreator(this, null, 0);
}
if (path.trim().length() == 0) {
throw new IllegalArgumentException("Path must not be empty.");
}
return load(Uri.parse(path));
}
/*
* 通過指定的ResourceID加載圖片
*/
public RequestCreator load(@DrawableRes int resourceId) {
if (resourceId == 0) {
throw new IllegalArgumentException("Resource ID must not be zero.");
}
return new RequestCreator(this, null, resourceId);
}
可以看到,load方法最終都是返回一個RequestCreator。RequestCreator保存請求的參數,構造方法如下:
/*
* RequestCreator的構造函數
*/
RequestCreator(Picasso picasso, Uri uri, int resourceId) {
if (picasso.shutdown) {
throw new IllegalStateException(
"Picasso instance already shut down. Cannot submit new requests.");
}
this.picasso = picasso;
this.data = new Request.Builder(uri, resourceId, picasso.defaultBitmapConfig);
}
/*
* 存儲請求的Uri和Resource Id以及Bitmap的配置
*/
Builder(Uri uri, int resourceId, Bitmap.Config bitmapConfig) {
this.uri = uri;
this.resourceId = resourceId;
this.config = bitmapConfig;
}
2.1.3 into方法將ImageView傳入
通過load方法加載的圖片,最終都要在ImageView上顯示。ImageView是通過into方法傳入的。該方法在RequestCreator中實現:
public void into(ImageView target) {
into(target, null);
}
/*
* 異步執行請求,請求執行完成後回調callback
*/
public void into(ImageView target, Callback callback) {
long started = System.nanoTime();
checkMain();
if (target == null) {
throw new IllegalArgumentException("Target must not be null.");
}
//如果沒有請求數據,即uri爲null或者resourceID爲0時,則取消請求。
if (!data.hasImage()) {
picasso.cancelRequest(target);
//加載placeHolder照片
if (setPlaceholder) {
setPlaceholder(target, getPlaceholderDrawable());
}
return;
}
if (deferred) {
if (data.hasSize()) {
throw new IllegalStateException("Fit cannot be used with resize.");
}
int width = target.getWidth();
int height = target.getHeight();
if (width == 0 || height == 0 || target.isLayoutRequested()) {
if (setPlaceholder) {
setPlaceholder(target, getPlaceholderDrawable());
}
picasso.defer(target, new DeferredRequestCreator(this, target, callback));
return;
}
data.resize(width, height);
}
//創建請求以及請求的key
Request request = createRequest(started);
String requestKey = createKey(request);
//從內存緩存中加載圖片
if (shouldReadFromMemoryCache(memoryPolicy)) {
Bitmap bitmap = picasso.quickMemoryCacheCheck(requestKey);
if (bitmap != null) {
picasso.cancelRequest(target);
setBitmap(target, picasso.context, bitmap, MEMORY, noFade, picasso.indicatorsEnabled);
if (picasso.loggingEnabled) {
log(OWNER_MAIN, VERB_COMPLETED, request.plainId(), "from " + MEMORY);
}
if (callback != null) {
callback.onSuccess();
}
return;
}
}
if (setPlaceholder) {
setPlaceholder(target, getPlaceholderDrawable());
}
//創建加載圖片的Action
Action action =
new ImageViewAction(picasso, target, request, memoryPolicy, networkPolicy, errorResId,
errorDrawable, requestKey, tag, callback, noFade);
//將請求入隊列,並提交執行請求
picasso.enqueueAndSubmit(action);
}
RequestCretor是將Request封裝成Action,然後提交請求到任務隊列中,讓任務異步執行。在構造請求之前,先從內存緩存中查看是否意見有圖片了,如果有的話,則直接返回緩存中的圖片,並回調callback接口。
2.1.4 提交任務
RequestCreator封裝好任務請求後,將請求提交到任務隊列中,請求入隊列的過程在Picasso類中實現。
void enqueueAndSubmit(Action action) {
Object target = action.getTarget();
//取消先前的任務,並將任務將入任務隊列中
if (target != null && targetToAction.get(target) != action) {
// This will also check we are on the main thread.
cancelExistingRequest(target);
targetToAction.put(target, action);
}
//提交任務
submit(action);
}
/*
* 提交任務
*/
void submit(Action action) {
dispatcher.dispatchSubmit(action);
}
Dispatcher是一個分發器,根據不同的請求命令分發給不同的方法處理,是基於Handler來傳遞請求消息的。
void dispatchSubmit(Action action) {
handler.sendMessage(handler.obtainMessage(REQUEST_SUBMIT, action));
}
public void handleMessage(final Message msg) {
switch (msg.what) {
case REQUEST_SUBMIT: {
Action action = (Action) msg.obj;
dispatcher.performSubmit(action);
break;
}
....
}
Dispatcher最終調用performSubmit來完成任務的提交。
void performSubmit(Action action) {
performSubmit(action, true);
}
/*
* 執行提交任務
*/
void performSubmit(Action action, boolean dismissFailed) {
//暫停標誌
if (pausedTags.contains(action.getTag())) {
pausedActions.put(action.getTarget(), action);
if (action.getPicasso().loggingEnabled) {
log(OWNER_DISPATCHER, VERB_PAUSED, action.request.logId(),
"because tag '" + action.getTag() + "' is paused");
}
return;
}
BitmapHunter hunter = hunterMap.get(action.getKey());
if (hunter != null) {
hunter.attach(action);
return;
}
//服務關閉了
if (service.isShutdown()) {
if (action.getPicasso().loggingEnabled) {
log(OWNER_DISPATCHER, VERB_IGNORED, action.request.logId(), "because shut down");
}
return;
}
//構建一個hunter,並提交一個任務,返回一個Future
hunter = forRequest(action.getPicasso(), this, cache, stats, action);
hunter.future = service.submit(hunter);
hunterMap.put(action.getKey(), hunter);
if (dismissFailed) {
failedActions.remove(action.getTarget());
}
if (action.getPicasso().loggingEnabled) {
log(OWNER_DISPATCHER, VERB_ENQUEUED, action.request.logId());
}
}
Dispatcher最終會構建一個BitmapHunter,並提交給ExecutorService來執行。BitmapHunter實現了Runnable接口,執行具體的請求操作。
@Override
public void run() {
try {
updateThreadName(data);
if (picasso.loggingEnabled) {
log(OWNER_HUNTER, VERB_EXECUTING, getLogIdsForHunter(this));
}
//通過hunt()方法區獲取執行結果
result = hunt();
//如果執行結果不爲空,則通過Dispatcher的dispatchComplete方法通知執行成功,否則通知執行失敗
if (result == null) {
dispatcher.dispatchFailed(this);
} else {
dispatcher.dispatchComplete(this);
}
} catch (Downloader.ResponseException e) {
if (!e.localCacheOnly || e.responseCode != 504) {
exception = e;
}
dispatcher.dispatchFailed(this);
} catch (NetworkRequestHandler.ContentLengthException e) {
exception = e;
dispatcher.dispatchRetry(this);
} catch (IOException e) {
exception = e;
dispatcher.dispatchRetry(this);
} catch (OutOfMemoryError e) {
StringWriter writer = new StringWriter();
stats.createSnapshot().dump(new PrintWriter(writer));
exception = new RuntimeException(writer.toString(), e);
dispatcher.dispatchFailed(this);
} catch (Exception e) {
exception = e;
dispatcher.dispatchFailed(this);
} finally {
Thread.currentThread().setName(Utils.THREAD_IDLE_NAME);
}
}
BitmapHunter是通過hunt方法去獲取Bitmap,如果獲取Bitmap成功,則通過Dispatcher的dispatchComplete方法通知加載成功,否則通過Dispatcher的dispatchFailed方法通知加載失敗。下面看下hunt方法加載Bitmap的過程:
Bitmap hunt() throws IOException {
Bitmap bitmap = null;
//判斷是否需要從內存緩存中讀取Bitmap,如果從內存緩存中,讀取成功,則直接返回,並更新stats狀態
if (shouldReadFromMemoryCache(memoryPolicy)) {
bitmap = cache.get(key);
if (bitmap != null) {
stats.dispatchCacheHit();
loadedFrom = MEMORY;
if (picasso.loggingEnabled) {
log(OWNER_HUNTER, VERB_DECODED, data.logId(), "from cache");
}
return bitmap;
}
}
data.networkPolicy = retryCount == 0 ? NetworkPolicy.OFFLINE.index : networkPolicy;
//根據指定的請求加載圖片,並將結果返回爲Result
RequestHandler.Result result = requestHandler.load(data, networkPolicy);
if (result != null) {
loadedFrom = result.getLoadedFrom();
exifRotation = result.getExifOrientation();
//從result中獲取bitmap結果
bitmap = result.getBitmap();
// If there was no Bitmap then we need to decode it from the stream.
if (bitmap == null) {
InputStream is = result.getStream();
try {
//從InputStream中解碼出圖片
bitmap = decodeStream(is, data);
} finally {
Utils.closeQuietly(is);
}
}
}
//如果bitmap不爲空,則判斷是否需要執行相應的轉換
if (bitmap != null) {
if (picasso.loggingEnabled) {
log(OWNER_HUNTER, VERB_DECODED, data.logId());
}
stats.dispatchBitmapDecoded(bitmap);
if (data.needsTransformation() || exifRotation != 0) {
synchronized (DECODE_LOCK) {
// 如果bitmap需要轉換的話,則通過transformResult執行相應的轉換
if (data.needsMatrixTransform() || exifRotation != 0) {
bitmap = transformResult(data, bitmap, exifRotation);
if (picasso.loggingEnabled) {
log(OWNER_HUNTER, VERB_TRANSFORMED, data.logId());
}
}
if (data.hasCustomTransformations()) {
bitmap = applyCustomTransformations(data.transformations, bitmap);
if (picasso.loggingEnabled) {
log(OWNER_HUNTER, VERB_TRANSFORMED, data.logId(), "from custom transformations");
}
}
}
if (bitmap != null) {
stats.dispatchBitmapTransformed(bitmap);
}
}
}
//返回執行結果
return bitmap;
}
圖片的加載最終是通過RequestHandler的load方法來加載的,該方法是一個抽象方法,需要根據具體的加載策略來實現。例如從網絡上加載圖片,就是NetworkRequestHandler來處理,如果是根據resourceId來加載圖片,則使用ResourceRequestHandler來處理。
/*
* 根據指定的request來加載圖片
* @param request 加載圖片的請求。
* @param networkPolicy 請求的網絡加載策略
*/
public abstract Result load(Request request, int networkPolicy) throws IOException;
以NetworkRequestHandler爲例來說明一下圖片的加載過程。
@Override
public Result load(Request request, int networkPolicy) throws IOException {
//利用下載器下載請求的Uri對應的內容
Response response = downloader.load(request.uri, request.networkPolicy);
if (response == null) {
return null;
}
//判斷響應結果的來源是來自磁盤還是網絡,因爲Picasso不自己實現磁盤緩存,而是藉助下載器的本地緩存功能,例如OkHtttp下載器就帶有緩存功能。
Picasso.LoadedFrom loadedFrom = response.cached ? DISK : NETWORK;
//1.從磁盤緩存中獲取Bitmap
Bitmap bitmap = response.getBitmap();
if (bitmap != null) {
return new Result(bitmap, loadedFrom);
}
//2.從網絡中獲取Bitmap
InputStream is = response.getInputStream();
if (is == null) {
return null;
}
// Sometimes response content length is zero when requests are being replayed. Haven't found
// root cause to this but retrying the request seems safe to do so.
if (loadedFrom == DISK && response.getContentLength() == 0) {
Utils.closeQuietly(is);
throw new ContentLengthException("Received response with 0 content-length header.");
}
if (loadedFrom == NETWORK && response.getContentLength() > 0) {
stats.dispatchDownloadFinished(response.getContentLength());
}
return new Result(is, loadedFrom);
}
從上面可以看到,圖片的加載過程是通過Downloader的load方法來加載的,該方法是Downloader接口中的一個方法,需要具體的加載器實現,例如OkHttpDownloader下載器。
/*
* 根據指定的Uri下載圖片
*/
Response load(Uri uri, int networkPolicy) throws IOException;
在load加載完圖片後,會判斷該圖片是從磁盤緩存中加載出來的,還是從網絡上獲取的,並更新加載標誌,返回加載的bitmap。需要注意的是Picasso沒有自己實現本地磁盤緩存,而是藉助下載器的本地磁盤緩存,Picasso自身只實現了內存緩存。
至此,一個完整的圖片加載的大致流程出來了。時序圖如下:
2.2 取消加載圖片
一個好的圖片加載庫不僅需要支持靈活的加載配置,而且也要支持相應的取消操作。在某些的情況下,需要提前取消圖片加載的任務,以減少系統資源的消耗。
取消加載圖片的方法是通過Picasso的cancelRequest()方法來實現的,cancelRequest()方法有三種實現,分別爲:
public void cancelRequest(ImageView view) {
cancelExistingRequest(view);
}
public void cancelRequest(Target target) {
cancelExistingRequest(target);
}
public void cancelRequest(RemoteViews remoteViews, int viewId) {
cancelExistingRequest(new RemoteViewsAction.RemoteViewsTarget(remoteViews, viewId));
}
這三個取消方法最終都是調用到cancelExistingRequest()方法,cancelExistingRequest()方法實現如下:
private void cancelExistingRequest(Object target) {
checkMain();
//首先獲取該target對應的action
Action action = targetToAction.remove(target);
if (action != null) {
action.cancel();
//調用Dispatcher的dispatchCancel方法發送取消指令
dispatcher.dispatchCancel(action);
}
//如果target是ImageView,則調用延遲請求的cancel方法
if (target instanceof ImageView) {
ImageView targetImageView = (ImageView) target;
DeferredRequestCreator deferredRequestCreator =
targetToDeferredRequestCreator.remove(targetImageView);
if (deferredRequestCreator != null) {
deferredRequestCreator.cancel();
}
}
}
可以看到cancelExistingRequest的主要工作有兩部分:一部分是通過Dispatcher發送取消Action操作的消息;另外一部分是針對ImageView,調用延遲請求的cancel方法。
2.2.1 Dispatcher發送取消消息
首先來看下Dispatcher的dispatchCancel方法,方法如下所示:
void dispatchCancel(Action action) {
handler.sendMessage(handler.obtainMessage(REQUEST_CANCEL, action));
}
handler由DispatcherHandler內部類實現,sendMessage方法發送消息後,最終調用到handleMessage方法來處理消息:
@Override
public void handleMessage(final Message msg) {
case REQUEST_CANCEL: {
Action action = (Action) msg.obj;
dispatcher.performCancel(action);
break;
}
}
void performCancel(Action action) {
//獲取action對應的key,然後根據key獲取對應的BitmapHunter,BitmapHunter是一個Runnable對象
String key = action.getKey();
BitmapHunter hunter = hunterMap.get(key);
if (hunter != null) {
//將action分離,並重新計算action新的優先級
hunter.detach(action);
//調用BitmapHunter的cancel方法,最終調用Future的cancel方法。
if (hunter.cancel()) {
//移除該key對應的BitmapHunter
hunterMap.remove(key);
if (action.getPicasso().loggingEnabled) {
log(OWNER_DISPATCHER, VERB_CANCELED, action.getRequest().logId());
}
}
}
//如果該Action包含暫停標誌,則從暫停Actions集合中移除該Action
if (pausedTags.contains(action.getTag())) {
pausedActions.remove(action.getTarget());
if (action.getPicasso().loggingEnabled) {
log(OWNER_DISPATCHER, VERB_CANCELED, action.getRequest().logId(),
"because paused request got canceled");
}
}
//從失敗Action中移除該Action
Action remove = failedActions.remove(action.getTarget());
if (remove != null && remove.getPicasso().loggingEnabled) {
log(OWNER_DISPATCHER, VERB_CANCELED, remove.getRequest().logId(), "from replaying");
}
}
可以看到PerformCancel方法的主要作用先分離該Action,然後調用Future的Cancel方法去真正取消任務。任務取消完成後,移除該key對應的BitmapHunter以及其他集合中包含的該Action。
BitmapHunter的Cancel方法如下:
boolean cancel() {
return action == null
&& (actions == null || actions.isEmpty())
&& future != null
&& future.cancel(false);
}
action以及actions集合在BitmapHunter的detach方法被置爲空了:
void detach(Action action) {
boolean detached = false;
//如果當前執行的action就是需要取消的action
if (this.action == action) {
this.action = null;
detached = true;
} else if (actions != null) {
detached = actions.remove(action);
}
// The action being detached had the highest priority. Update this
// hunter's priority with the remaining actions.
// action被分離後,需要重新計算actions集合中其他action的優先級
if (detached && action.getPriority() == priority) {
priority = computeNewPriority();
}
if (picasso.loggingEnabled) {
log(OWNER_HUNTER, VERB_REMOVED, action.request.logId(), getLogIdsForHunter(this, "from "));
}
}
至此,cancelExistingRequest的第一部分工作完成了,發送一個取消消息,由Dispatcher真正去取消一個任。接下來看cancelExistingRequest的第二部分工作,調用延遲請求的cancel方法。
2.2.2 延遲請求的取消
當嘗試調整圖片的大小適應ImageView的邊界,這將延遲執行該請求直至ImageView佈局好了。具體在RequestCreator的fit方法中實現:
public RequestCreator fit() {
//設置deferred標誌位爲true
deferred = true;
return this;
}
需要注意的是fit()方法只能用於ImageView,在調用into(ImageView,Callback)方法時,會根據deferred標誌決定是否需要創建一個DeferredRequestCreator,into方法實現如下:
public void into(ImageView target, Callback callback) {
.....省略部分代碼
if (deferred) {
if (data.hasSize()) {
throw new IllegalStateException("Fit cannot be used with resize.");
}
int width = target.getWidth();
int height = target.getHeight();
if (width == 0 || height == 0) {
if (setPlaceholder) {
setPlaceholder(target, getPlaceholderDrawable());
}
//創建一個DeferredRequestCreator對象,並調用Picasso的defer方法。
picasso.defer(target, new DeferredRequestCreator(this, target, callback));
return;
}
data.resize(width, height);
}
......省略部分代碼
}
void defer(ImageView view, DeferredRequestCreator request) {
targetToDeferredRequestCreator.put(view, request);
}
Picasso的defer方法主要將ImageView和DeferredRequestCreator保存到一個HashMap集合中,以Key值爲鍵值。我們再回過頭來看cancelExistingRequest的延遲請求取消的部分:
private void cancelExistingRequest(Object target) {
.....省略部分代碼
if (target instanceof ImageView) {
ImageView targetImageView = (ImageView) target;
//獲取DeferredRequestCreator對象
DeferredRequestCreator deferredRequestCreator =
targetToDeferredRequestCreator.remove(targetImageView);
//如果DeferredRequestCreator不爲空,則調用cancel方法
if (deferredRequestCreator != null) {
deferredRequestCreator.cancel();
}
}
}
void cancel() {
callback = null;
//DeferredRequestCreator持有ImageView的弱引用
ImageView target = this.target.get();
if (target == null) {
return;
}
//獲取視圖樹的監聽器
ViewTreeObserver vto = target.getViewTreeObserver();
if (!vto.isAlive()) {
return;
}
//移除OnPreDrawListener的監聽
vto.removeOnPreDrawListener(this);
}
在DeferredRequestCreator的構造器中,註冊了監聽視圖樹的監聽,當一個視圖樹將要繪製時,首先回調監聽函數onPreDraw函數。這樣可以在ImageView繪製前,先獲取ImageView的寬高,然後重新發送請求以滿足ImageView的邊界要求,這也是叫延遲請求的緣由了。
class DeferredRequestCreator implements ViewTreeObserver.OnPreDrawListener {
DeferredRequestCreator(RequestCreator creator, ImageView target, Callback callback) {
this.creator = creator;
this.target = new WeakReference<ImageView>(target);
this.callback = callback;
target.getViewTreeObserver().addOnPreDrawListener(this);
}
@Override
public boolean onPreDraw() {
ImageView target = this.target.get();
if (target == null) {
return true;
}
ViewTreeObserver vto = target.getViewTreeObserver();
if (!vto.isAlive()) {
return true;
}
int width = target.getWidth();
int height = target.getHeight();
if (width <= 0 || height <= 0) {
return true;
}
vto.removeOnPreDrawListener(this);
this.creator.unfit().resize(width, height).into(target, callback);
return true;
}
}
至此,完成了延遲請求的取消操作了。
可以看到取消圖片的加載過程,大致包含了取消當前執行的任務,以及ImageView的延遲請求取消操作。
三、Picasso關鍵類圖
picasso開源框架中包含了內存緩存、下載器、任務執行器以及分發器等各個模塊,他們之間的關係可以用類圖來表示:
可以看到Picasso類圖,大致可以分爲以下幾個部分:
Downloaderader:下載器,定義了下載的接口,具體的實現的有OkHttpDownloader和UrlConnectionDownloader。OkHttpDownloader是利用OkHttp來下載圖片的。UrlConnectionDownloader是利用HttpURLConnection來下載圖片的。也可以自定義實現下載器,只需實現loadheshutdown方法。
Cache: 緩存,定義了緩存的接口,這裏的具體實現是LruCache實現的內存緩存。LruCache是基於最近使用原則來保存緩存項。也可以自定義實現緩存,只需實現Cache接口中的方法。
ExecutorService: 任務執行器,這裏的具體實現是PicassoExecutorService,默認是支持3個線程同時工作,採用的優先級隊列。也可以自定義實現任務執行器,只需實現ExecutorService接口中的方法。
Action:任務請求的封裝,定義了任務執行的接口。這裏的具體實現有ImageViewAction、RemoteViewsAction、TargetAction等。在Action中定義了任務執行完成的complete方法和任務出錯的方法error。
Stats:統計任務執行狀態,包括從緩存中命中圖片的次數、總共下載的圖片大小、平均下載圖片的大小、下載次數等等。
RequestHandler:請求處理器,可以處理各種類型的請求。請求圖片可以通過Uri從網絡上獲取,也可以通過ResourceId獲取,RequestHandler是一個抽象類型,可以讓用戶選擇處理請求的方式,目前支持7種內在類型的圖片請求。ResourceRequestHandler是處理ResourceID的圖片請求,NetworkRequestHandler是處理網絡Uri圖片請求。每個請求都通過load方法實現具體的加載邏輯。
RequestCreator:構建圖片加載請求,定義into方法,最終將請求添加到任務隊列中去執行。
Dispatcher:分發器,協調各個模塊的運行,是整個框架的核心樞紐。通過Handler來傳遞消息,完成狀態的流轉。
四、總結
Picasso是Square公司開源的一個圖片加載庫,Picasso自身是不實現“本地緩存”的,需要搭配下載器來實現本地緩存,例如Square開源的OKHttp就提供了本地緩存功能。Picasso整體使用還是比較簡單,源碼也不是很複雜,是圖片加載庫的一個好的選擇。