目錄
Picasso加載一張圖片的流程
-
創(chuàng)建
//通過定義一個(gè)PicassoProvider來獲取Context //單例創(chuàng)建Picasso實(shí)例 public static Picasso get() { if (singleton == null) { synchronized (Picasso.class) { if (singleton == null) { if (PicassoProvider.context == null) { throw new IllegalStateException("context == null"); } singleton = new Builder(PicassoProvider.context).build(); } } } return singleton; } //在AndroidManifest.xml中聲明 @RestrictTo(LIBRARY) public final class PicassoProvider extends ContentProvider { @SuppressLint("StaticFieldLeak") static Context context; @Override public boolean onCreate() { context = getContext(); return true; } }

build( ) 創(chuàng)建Picasso實(shí)例
/** Create the {@link Picasso} instance. */
public Picasso build() {
Context context = this.context;
//創(chuàng)建默認(rèn)下載器
if (downloader == null) {
downloader = new OkHttp3Downloader(context);
}
//創(chuàng)建LRU內(nèi)存緩存
if (cache == null) {
cache = new LruCache(context);
}
//創(chuàng)建線程池,默認(rèn)有3個(gè)執(zhí)行線程
if (service == null) {
service = new PicassoExecutorService();
}
//創(chuàng)建默認(rèn)transformer
if (transformer == null) {
transformer = RequestTransformer.IDENTITY;
}
//統(tǒng)計(jì)
Stats stats = new Stats(cache);
//調(diào)度
Dispatcher dispatcher = new Dispatcher(context, service, HANDLER, downloader, cache, stats);
return new Picasso(context, dispatcher, cache, listener, transformer, requestHandlers, stats,
defaultBitmapConfig, indicatorsEnabled, loggingEnabled);
}
//Picasso構(gòu)造方法 對(duì)象賦值&創(chuàng)建一個(gè)新對(duì)象
//最主要的是初始化了allRequestHandlers 除了自定義的extraRequestHandlers 添加了7個(gè)默認(rèn)的RequestHandler
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; // Adjust this as internal handlers are added or removed.
int extraCount = (extraRequestHandlers != null ? extraRequestHandlers.size() : 0);
List<RequestHandler> allRequestHandlers = new ArrayList<>(builtInHandlers + extraCount);
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);
//省略...
}
public abstract class RequestHandler {
//來判斷當(dāng)前類型的RequestHandler能否處理request
public abstract boolean canHandleRequest(Request data);
//使用傳入的requst來加載圖片,加載的結(jié)果放入Result中返回
@Nullable
public abstract Result load(Request request, int networkPolicy) throws IOException;
//待定
int getRetryCount() {
return 0;
}
//待定
boolean shouldRetry(boolean airplaneMode, NetworkInfo info) {
return false;
}
//待定
boolean supportsReplay() {
return false;
}
}
創(chuàng)建好Picasso實(shí)例后,便是load了
//load的幾個(gè)重載方法最后都會(huì)來調(diào)用參數(shù)為Uri的方法 生成一個(gè)RequestCreator,
//來執(zhí)行接下來的trannform() rotate() into()等操作
public RequestCreator load(@Nullable Uri uri) {
return new RequestCreator(this, uri, 0);
}
-
入隊(duì)
//通過構(gòu)造方法傳入一個(gè)Picasso實(shí)例的引用,并且創(chuàng)建一個(gè)Request.Builder()的實(shí)例 //為創(chuàng)建一個(gè)Request做準(zhǔn)備 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); }將我們需要加載的圖片的信息都保存在data里面,我們?cè)O(shè)置centerCrop( )或者transform( )實(shí)際上都是對(duì)data內(nèi)對(duì)應(yīng)的變量標(biāo)識(shí)進(jìn)行設(shè)置,然后在into( )中進(jìn)行build 生成一個(gè)Request,
然后看一下into( )方法的實(shí)現(xiàn)
public void into(ImageView target, Callback callback) { long started = System.nanoTime(); //檢查是否在主線程 checkMain(); if (target == null) { throw new IllegalArgumentException("Target must not be null."); } //如果沒有設(shè)置要加載的圖片的Uri或者resourceId,直接設(shè)置占位圖然后返回 if (!data.hasImage()) { picasso.cancelRequest(target); if (setPlaceholder) { setPlaceholder(target, getPlaceholderDrawable()); } return; } //是否延時(shí)加載,也就是選擇fit()模式 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()); } //監(jiān)聽I(yíng)mageView的ViewTreeObserver.OnPreDrawListener接口,一旦ImageView //的寬高被賦值,就按照ImageView的寬高繼續(xù)加載. picasso.defer(target, new DeferredRequestCreator(this, target, callback)); return; } data.resize(width, height); } //構(gòu)建request Request request = createRequest(started); String requestKey = createKey(request); //是否要從內(nèi)存中讀取 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; } } //緩存中沒有,先設(shè)置占位圖,然后構(gòu)建一個(gè)ImageViewAction if (setPlaceholder) { setPlaceholder(target, getPlaceholderDrawable()); } //Action的實(shí)現(xiàn)類保存了這次加載需要的所有信息,還提供了加載完成后的回調(diào)方法 Action action = new ImageViewAction(picasso, target, request, memoryPolicy, networkPolicy, errorResId, errorDrawable, requestKey, tag, callback, noFade); //將Action對(duì)象提交到隊(duì)列 picasso.enqueueAndSubmit(action); }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); } //提交Action 最后轉(zhuǎn)到Dispatcher類的 dispatchSubmit方法 submit(action); } void submit(Action action) { dispatcher.dispatchSubmit(action); } //Dispatcher類的dispatchXXX系列方法都是通過一個(gè)Handler來發(fā)送消息看一下Dispatcher的主要實(shí)現(xiàn)
Dispatcher(Context context, ExecutorService service, Handler mainThreadHandler, Downloader downloader, Cache cache, Stats stats) { this.dispatcherThread = new DispatcherThread(); this.dispatcherThread.start(); this.handler = new DispatcherHandler(dispatcherThread.getLooper(), this); this.mainThreadHandler = mainThreadHandler; } void dispatchSubmit(Action action) { handler.sendMessage(handler.obtainMessage(REQUEST_SUBMIT, action)); } static class DispatcherThread extends HandlerThread { DispatcherThread() { super(Utils.THREAD_PREFIX + DISPATCHER_THREAD_NAME, THREAD_PRIORITY_BACKGROUND); } } private static class DispatcherHandler extends Handler { private final Dispatcher dispatcher; DispatcherHandler(Looper looper, Dispatcher dispatcher) { super(looper); this.dispatcher = dispatcher; } @Override public void handleMessage(final Message msg) { switch (msg.what) { case REQUEST_SUBMIT: { Action action = (Action) msg.obj; dispatcher.performSubmit(action); break; //省略... } } }首先自己創(chuàng)建了一個(gè)DispatcherThread子線程,然后創(chuàng)建了DispatcherHandler對(duì)象,用這個(gè)Handler來進(jìn)行發(fā)送消息,而在DispatcherHandler的handleMessage中,最終調(diào)用了dispatcher.performXXX(action);等方法
-
執(zhí)行、解碼、變化
來看一下performSubmit( )方法的實(shí)現(xiàn)
void performSubmit(Action action, boolean dismissFailed) { //如果pausedActions包含這個(gè)Action,則暫停 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; } //此Action對(duì)應(yīng)的hunter存在,attach后直接返回 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; } //構(gòu)建hunter對(duì)象 實(shí)現(xiàn)了Runnable接口 hunter = forRequest(action.getPicasso(), this, cache, stats, action); //通過ExecutorService執(zhí)行hunter,并返回future對(duì)象。 //提交線程池執(zhí)行 hunter.future = service.submit(hunter); //將構(gòu)造好的hunter對(duì)象加入到hunterMap緩存起來 hunterMap.put(action.getKey(), hunter); if (dismissFailed) { failedActions.remove(action.getTarget()); } if (action.getPicasso().loggingEnabled) { log(OWNER_DISPATCHER, VERB_ENQUEUED, action.request.logId()); } }static BitmapHunter forRequest(Picasso picasso, Dispatcher dispatcher, Cache cache, Stats stats, Action action) { Request request = action.getRequest(); List<RequestHandler> requestHandlers = picasso.getRequestHandlers(); //通過遍歷所有的requestHandlers(包括自定的和默認(rèn)添加的那7個(gè))尋找能處理這個(gè)Request的requestHandler //使用這個(gè)requestHandler來構(gòu)建BitmapHunter for (int i = 0, count = requestHandlers.size(); i < count; i++) { RequestHandler requestHandler = requestHandlers.get(i); if (requestHandler.canHandleRequest(request)) { return new BitmapHunter(picasso, dispatcher, cache, stats, action, requestHandler); } } return new BitmapHunter(picasso, dispatcher, cache, stats, action, ERRORING_HANDLER); } //BitmapHunter實(shí)現(xiàn)了Runable接口,并且通過ExecutorService來執(zhí)行,接下來就看一下BitmapHunter的run( )方法 @Override public void run() { try { updateThreadName(data); if (picasso.loggingEnabled) { log(OWNER_HUNTER, VERB_EXECUTING, getLogIdsForHunter(this)); } //調(diào)用hunt()方法并且返回一個(gè)Bitmap對(duì)象 result = hunt(); //然后根絕結(jié)果調(diào)用Dispatcher的dispatchXXX系列方法來發(fā)送不同的消息 if (result == null) { dispatcher.dispatchFailed(this); } else { dispatcher.dispatchComplete(this); } } catch (NetworkRequestHandler.ResponseException e) { if (!NetworkPolicy.isOfflineOnly(e.networkPolicy) || e.code != 504) { exception = e; } dispatcher.dispatchFailed(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); } } /** * Global lock for bitmap decoding to ensure that we are only decoding one at a time. Since * this will only ever happen in background threads we help avoid excessive memory thrashing as * well as potential OOMs. Shamelessly stolen from Volley. */ private static final Object DECODE_LOCK = new Object(); //hunter方法 Bitmap hunt() throws IOException { Bitmap bitmap = null; //是否可以從緩存中讀取 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; } } networkPolicy = retryCount == 0 ? NetworkPolicy.OFFLINE.index : networkPolicy; //調(diào)用requestHandler的load方法得到一個(gè)result //load方法在不同的requestHandler中有不同的實(shí)現(xiàn) //(解碼) RequestHandler.Result result = requestHandler.load(data, networkPolicy); if (result != null) { loadedFrom = result.getLoadedFrom(); exifOrientation = result.getExifOrientation(); bitmap = result.getBitmap(); // If there was no Bitmap then we need to decode it from the stream. if (bitmap == null) { Source source = result.getSource(); try { bitmap = decodeStream(source, data); } finally { try { //noinspection ConstantConditions If bitmap is null then source is guranteed non-null. source.close(); } catch (IOException ignored) { } } } } if (bitmap != null) { if (picasso.loggingEnabled) { log(OWNER_HUNTER, VERB_DECODED, data.logId()); } stats.dispatchBitmapDecoded(bitmap); //處理Transformation(變化) if (data.needsTransformation() || exifOrientation != 0) { //圖片在轉(zhuǎn)化的時(shí)候需要一片臨時(shí)內(nèi)存,加全局鎖解決瞬時(shí)OOOM的問題 //或者使用synchronized(BitmapHunter.class){} synchronized (DECODE_LOCK) { if (data.needsMatrixTransform() || exifOrientation != 0) { bitmap = transformResult(data, bitmap, exifOrientation); 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); } } } //返回Bitmap return bitmap; }最后拿到result之后,會(huì)調(diào)用dispatcher.dispatchComplete(this),最終會(huì)調(diào)用dispatcher.performComplete()方法
-
批處理
void performComplete(BitmapHunter hunter) { //是否加入緩存 if (shouldWriteToMemoryCache(hunter.getMemoryPolicy())) { cache.set(hunter.getKey(), hunter.getResult()); } //完成后hunterMap中移除 hunterMap.remove(hunter.getKey()); batch(hunter); if (hunter.getPicasso().loggingEnabled) { log(OWNER_DISPATCHER, VERB_BATCHED, getLogIdsForHunter(hunter), "for completion"); } } //批處理 private void batch(BitmapHunter hunter) { if (hunter.isCancelled()) { return; } if (hunter.result != null) { hunter.result.prepareToDraw(); } batch.add(hunter); if (!handler.hasMessages(HUNTER_DELAY_NEXT_BATCH)) { //Handler中調(diào)用performBatchComplete handler.sendEmptyMessageDelayed(HUNTER_DELAY_NEXT_BATCH, BATCH_DELAY); } } void performBatchComplete() { List<BitmapHunter> copy = new ArrayList<>(batch); batch.clear(); //消息發(fā)動(dòng)到主線程進(jìn)行處理 mainThreadHandler.sendMessage(mainThreadHandler.obtainMessage(HUNTER_BATCH_COMPLETE, copy)); logBatch(copy); } //mainThreadHandler中 //... case HUNTER_BATCH_COMPLETE: { @SuppressWarnings("unchecked") List<BitmapHunter> batch = (List<BitmapHunter>) msg.obj; //noinspection ForLoopReplaceableByForEach for (int i = 0, n = batch.size(); i < n; i++) { BitmapHunter hunter = batch.get(i); //調(diào)用Picasso#complete方法 hunter.picasso.complete(hunter); } break; } //... -
完成、分發(fā)
接著看一下Picasso#complete( )方法的實(shí)現(xiàn)
void complete(BitmapHunter hunter) { //獲取執(zhí)行的那個(gè)Action Action single = hunter.getAction(); //獲取被添加進(jìn)來的Action List<Action> joined = hunter.getActions(); boolean hasMultiple = joined != null && !joined.isEmpty(); boolean shouldDeliver = single != null || hasMultiple; if (!shouldDeliver) { return; } Uri uri = hunter.getData().uri; Exception exception = hunter.getException(); Bitmap result = hunter.getResult(); LoadedFrom from = hunter.getLoadedFrom(); //分發(fā) if (single != null) { deliverAction(result, from, single, exception); } if (hasMultiple) { //noinspection ForLoopReplaceableByForEach for (int i = 0, n = joined.size(); i < n; i++) { Action join = joined.get(i); deliverAction(result, from, join, exception); } } if (listener != null && exception != null) { listener.onImageLoadFailed(this, uri, exception); } } //deliverAction的實(shí)現(xiàn) //此方法的主要實(shí)現(xiàn)就是回調(diào)Action的complete方法或者error方法 private void deliverAction(Bitmap result, LoadedFrom from, Action action, Exception e) { if (action.isCancelled()) { return; } if (!action.willReplay()) { targetToAction.remove(action.getTarget()); } if (result != null) { if (from == null) { throw new AssertionError("LoadedFrom cannot be null."); } action.complete(result, from); if (loggingEnabled) { log(OWNER_MAIN, VERB_COMPLETED, action.request.logId(), "from " + from); } } else { action.error(e); if (loggingEnabled) { log(OWNER_MAIN, VERB_ERRORED, action.request.logId(), e.getMessage()); } } } -
顯示
之前分析是看到,這個(gè)加載流程這里的Action是ImageViewAction,下面看一下ImageViewAction對(duì)應(yīng)的實(shí)現(xiàn)
@Override public void complete(Bitmap result, Picasso.LoadedFrom from) { if (result == null) { throw new AssertionError( String.format("Attempted to complete action with no result!\n%s", this)); } //獲取要顯示的ImageView ImageView target = this.target.get(); if (target == null) { return; } Context context = picasso.context; boolean indicatorsEnabled = picasso.indicatorsEnabled; //通過PicassoDrawable來將bitmap設(shè)置到ImageView上 PicassoDrawable.setBitmap(target, context, result, from, noFade, indicatorsEnabled); if (callback != null) { callback.onSuccess(); } } //使用PicassoDrawable來設(shè)置Bitmap,主要是為了設(shè)置漸變的加載動(dòng)畫 -
PicassoDrawable漸變動(dòng)畫
PicassoDrawable(Context context, Bitmap bitmap, Drawable placeholder, Picasso.LoadedFrom loadedFrom, boolean noFade, boolean debugging) { super(context.getResources(), bitmap); this.debugging = debugging; this.density = context.getResources().getDisplayMetrics().density; this.loadedFrom = loadedFrom; //noFade為false,并且不是從內(nèi)存中加載的時(shí)候,才會(huì)有漸變動(dòng)畫 boolean fade = loadedFrom != MEMORY && !noFade; if (fade) { this.placeholder = placeholder; animating = true; startTimeMillis = SystemClock.uptimeMillis(); } } @Override public void draw(Canvas canvas) { if (!animating) { super.draw(canvas); } else { float normalized = (SystemClock.uptimeMillis() - startTimeMillis) / FADE_DURATION; if (normalized >= 1f) { animating = false; placeholder = null; super.draw(canvas); } else { if (placeholder != null) { placeholder.draw(canvas); } //漸變動(dòng)畫 // setAlpha will call invalidateSelf and drive the animation. int partialAlpha = (int) (alpha * normalized); super.setAlpha(partialAlpha); super.draw(canvas); super.setAlpha(alpha); } } if (debugging) { drawDebugIndicator(canvas); } }