Picasso圖片加載框架 —— 源碼解析(一)

圖片加載是Android開發(fā)中的常見情景,在日常項目中却妨,也一直在用Picasso來處理各種加載需求饵逐。用的多了,也就自然想了解下其實(shí)現(xiàn)過程彪标。這里倍权,將深入源碼,學(xué)習(xí)Picasso框架的實(shí)現(xiàn)過程和設(shè)計思想捞烟。計劃用三篇文章來對Picasso做一個全面的分析薄声,而這第一篇的目標(biāo)則是梳理一下Picasso加載圖片的整體思路和核心流程。這樣有了主干题画,學(xué)習(xí)起來才能全而不亂默辨。

一、Picasso簡介

Picasso是Square公司推出的開源圖片加載庫苍息。體量輕缩幸、功能完善、使用簡單竞思,配合OkHttp使用效果極佳表谊。
官方鏈接:http://square.github.io/picasso/

若想在項目中使用Picasso,只需在gradle中添加依賴即可:

//2.5.0為版本號
dependencies {
    compile 'com.squareup.picasso:picasso:2.5.0'
}

本文所涉及到的源碼均為Picasso 2.5.0版本

二盖喷、源碼分析

在分析源碼之前爆办,我們先看下Picasso是如何使用的:

Picasso.with(context).load(url).into(imageView);

給定一個url和ImageView,就可以將圖片加載到ImageView中课梳,十分簡單距辆。

接下來余佃,我們就一步步的看下,Picasso是如何通過一行代碼實(shí)現(xiàn)圖片加載的跨算。

1. 初始化Picasso實(shí)例

static Picasso singleton = null;

Picasso是一個單例類咙冗,可通過Picasso.with()靜態(tài)方法對Picasso進(jìn)行初始化。

/**
 * Lru memory cache: 默認(rèn)緩存大小為程序運(yùn)行內(nèi)存的15%
 * Disk cache: 默認(rèn)緩存大小為5MB ~ 50MB 
*/
public static Picasso with(Context context) {
  if (singleton == null) {
    synchronized (Picasso.class) {
      if (singleton == null) {
        singleton = new Builder(context).build();
      }
    }
  }
  return singleton;
}

從源碼可以看到漂彤,Picasso.with()方法做的事情非常簡單,就是使用雙重校驗鎖的方式灾搏,調(diào)用Picasso.Builder.build()構(gòu)造Picasso的單例對象挫望。

既然如此,那么Builder就一定保存了初始化Picasso所需的參數(shù):

public static class Builder {
  //上下文
  private final Context context;
  //從外部資源(網(wǎng)絡(luò)狂窑、磁盤等)下載圖片的下載器
  private Downloader downloader;
  //異步加載圖片的線程池  
  private ExecutorService service;
  //內(nèi)存緩存    
  private Cache cache;
  //監(jiān)聽圖片加載失敗的回調(diào) 
  private Listener listener;
  //request的修改器 
  private RequestTransformer transformer;
  //自定義的圖片獲取方式  
  private List<RequestHandler> requestHandlers;
  //圖片配置    
  private Bitmap.Config defaultBitmapConfig;
  //是否顯示debug indicators    
  private boolean indicatorsEnabled;
  //是否允許debug logging   
  private boolean loggingEnabled;
}

所有變量均提供了set方法供用戶從外部進(jìn)行設(shè)置媳板。若不設(shè)置,則Builder.build()方法也為其提供了默認(rèn)實(shí)現(xiàn)泉哈。

public Picasso build() {
  Context context = this.context;

  if (downloader == null) {
    //若能反射找到com.squareup.okhttp.OkHttpClient,則downloader為OkHttpDownloader
    //否則downloader為UrlConnectionDownloader
    downloader = Utils.createDefaultDownloader(context);
  }
  if (cache == null) {
    cache = new LruCache(context);
  }
  if (service == null) {
    //核心線程數(shù)和最大線程數(shù)均為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.build()方法做的事情就是為各成員變量賦值,并調(diào)用new Picasso()來構(gòu)建Picasso的實(shí)例丛晦。

Picasso(Context context, Dispatcher dispatcher, Cache cache, Listener listener,
      RequestTransformer requestTransformer, List<RequestHandler> extraRequestHandlers, Stats stats,
      Bitmap.Config defaultBitmapConfig, boolean indicatorsEnabled, boolean loggingEnabled) {
    //省略其他代碼

    //分發(fā)器
    this.dispatcher = dispatcher;
    //請求處理器
    List<RequestHandler> allRequestHandlers =
        new ArrayList<RequestHandler>(builtInHandlers + extraCount);
    // ResourceRequestHandler needs to be the first in the list to avoid
    // forcing other RequestHandlers to perform null checks on request.uri
    // to cover the (request.resourceId != 0) case.
    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);
    //省略其他代碼
}

在這之中奕纫,有兩個很重要角色:Dispatcher和RequestHandler。大家先對此有個印象烫沙,后面用到時會詳細(xì)講匹层。

2. 創(chuàng)建Request

初始化好了Picasso的實(shí)例,接下來就要調(diào)用Picasso.load()方法了锌蓄。

Picasso.load()默認(rèn)提供了四種重載:
load()
// load(String)和load(File)最終都會調(diào)用load(Uri)升筏。
public RequestCreator load(File file) {
    if (file == null) {
      return new RequestCreator(this, null, 0);
    }
    return load(Uri.fromFile(file));
  }

public RequestCreator load(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));
  }

public RequestCreator load(Uri uri) {
    return new RequestCreator(this, uri, 0);
  }

//直接創(chuàng)建RequestCreator
public RequestCreator load(int resourceId) {
    if (resourceId == 0) {
      throw new IllegalArgumentException("Resource ID must not be zero.");
    }
    return new RequestCreator(this, null, resourceId);
  }

可以看到,這四個方法內(nèi)部實(shí)現(xiàn)大同小異瘸爽,最終都會創(chuàng)建一個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;
    //data即Request.Builder對象,其封裝了真實(shí)的請求內(nèi)容
    this.data = new Request.Builder(uri, resourceId, picasso.defaultBitmapConfig);
  }

3. 分發(fā)Action

Picasso.load()方法創(chuàng)建好了RequestCreator剪决,也準(zhǔn)備好了url灵汪,接下來就要創(chuàng)建Action了。

//采用異步方式昼捍,將request的結(jié)果填充到ImageView中
public void into(ImageView target) {
    into(target, null);
  }

// Callback為監(jiān)聽圖片添加結(jié)果(成功/失斒缎椤)的回調(diào)
public void into(ImageView target, Callback callback) {
    long started = System.nanoTime();
    //檢查是否為主線程調(diào)用
    checkMain();

    if (target == null) {
      throw new IllegalArgumentException("Target must not be null.");
    }
    
    //檢查請求中是否設(shè)置了uri或resourceId。若均沒有設(shè)置妒茬,則視其為無效請求担锤,取消這次request
    if (!data.hasImage()) {
      picasso.cancelRequest(target);
      if (setPlaceholder) {
        setPlaceholder(target, getPlaceholderDrawable());
      }
      return;
    }

    //是否調(diào)整圖片尺寸
    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());
        }
        picasso.defer(target, new DeferredRequestCreator(this, target, callback));
        return;
      }
      data.resize(width, height);
    }

    //創(chuàng)建真實(shí)請求
    Request request = createRequest(started);
    String requestKey = createKey(request);

    //是否允許從cache中讀取圖片
    if (shouldReadFromMemoryCache(memoryPolicy)) {
      Bitmap bitmap = picasso.quickMemoryCacheCheck(requestKey);
      //若cache中存在要請求的圖片,則取消請求乍钻,直接加載cache中的圖片
      if (bitmap != null) {
        picasso.cancelRequest(target);
        //將cache中的圖片加載到ImageView上
        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());
    }

    //執(zhí)行到此肛循,則表示需要從外部資源下載圖片
    //創(chuàng)建ImageViewAction铭腕,ImageViewAction內(nèi)部持有ImageView和Request
    Action action =
        new ImageViewAction(picasso, target, request, memoryPolicy, networkPolicy, errorResId,
            errorDrawable, requestKey, tag, callback, noFade);
    
    //將Action放入隊列并提交
    picasso.enqueueAndSubmit(action);
  }

RequestCreator.into()方法主要做了一些檢查和準(zhǔn)備工作,包括:
1)確保在主線程進(jìn)行調(diào)用多糠;
2)檢查request的有效性累舷,即uri或resourceId是否為空;
3)是否從cache中獲取圖片夹孔;
4)若request有效且cache中沒有目標(biāo)圖片被盈,則創(chuàng)建ImageViewAction,提交并放入隊列搭伤。

到此為止只怎,還是沒有看到處理url和添加圖片的過程,只能跟著Picasso.enqueueAndSubmit(action)繼續(xù)往下看怜俐。

void enqueueAndSubmit(Action action) {
    //對于ImageViewAction來說身堡,target即ImageView
    Object target = action.getTarget();

    //檢查ImageView是否有其他正在執(zhí)行的action,若有拍鲤,則取消之前的action
    if (target != null && targetToAction.get(target) != action) {
      cancelExistingRequest(target);
      targetToAction.put(target, action);
    }

    //提交action
    submit(action);
  }

Picasso.enqueueAndSubmit()方法將action存入WeakHashMap中贴谎,并確保ImageView當(dāng)前只有一個正在執(zhí)行的action。

void submit(Action action) {
    dispatcher.dispatchSubmit(action);
  }

Picasso.submit()方法也沒有對action進(jìn)行處理季稳,而是直接調(diào)用Dispatcher.dispatchSubmit()繼續(xù)下發(fā)擅这。

Dispatcher(Context context, ExecutorService service, Handler mainThreadHandler,
      Downloader downloader, Cache cache, Stats stats) {
    //省略其他代碼

    // DispatcherHandler繼承自HandlerThread,因此handler為子線程的handler
    this.handler = new DispatcherHandler(dispatcherThread.getLooper(), this);
    // mainThreadHandler為主線程handler
    this.mainThreadHandler = mainThreadHandler;

    //省略其他代碼
}

void dispatchSubmit(Action action) {
    //將action裝入message绞幌,并由子線程handler進(jìn)行發(fā)送蕾哟,完成線程切換
    handler.sendMessage(handler.obtainMessage(REQUEST_SUBMIT, action));
  }

Dispatcher是分發(fā)器,該類持有兩個Handler:一個是mainThreadHandler莲蜘,另一個是子線程handler谭确。dispatchSubmit()方法即調(diào)用子線程handler的sendMessage()方法,將action發(fā)送到子線程進(jìn)行處理票渠。

值得注意的是逐哈,之前的代碼是在主線程中運(yùn)行的,而在handler.sendMessage()之后问顷,便完成了主線程到子線程的切換昂秃。這也意味著接下來要做耗時操作了。

4. 提交BitmapHunter

在子線程接收到message后杜窄,會調(diào)用Dispatcher.performSubmit()方法肠骆。

@Override 
public void handleMessage(final Message msg) {
      switch (msg.what) {
        case REQUEST_SUBMIT: {
          Action action = (Action) msg.obj;
          dispatcher.performSubmit(action);
          break;
        }
      //省略其他代碼
      }
}

void performSubmit(Action action) {
    performSubmit(action, true);
}

void performSubmit(Action action, boolean dismissFailed) {
    //如果當(dāng)前action在pausedTags中,則暫停執(zhí)行并將其放入pausedActions
    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;
    }

    //從active hunters中尋找有無action對應(yīng)的BitmapHunter
    BitmapHunter hunter = hunterMap.get(action.getKey());
    if (hunter != null) {
      hunter.attach(action);
      return;
    }

    //若線程池已關(guān)閉塞耕,則退出執(zhí)行
    if (service.isShutdown()) {
      if (action.getPicasso().loggingEnabled) {
        log(OWNER_DISPATCHER, VERB_IGNORED, action.request.logId(), "because shut down");
      }
      return;
    }

    //創(chuàng)建hunter對象
    hunter = forRequest(action.getPicasso(), this, cache, stats, action);
    //執(zhí)行hunter任務(wù)
    hunter.future = service.submit(hunter);
    //將hunter放入active hunters
    hunterMap.put(action.getKey(), hunter);
    if (dismissFailed) {
      failedActions.remove(action.getTarget());
    }

    if (action.getPicasso().loggingEnabled) {
      log(OWNER_DISPATCHER, VERB_ENQUEUED, action.request.logId());
    }
}

Dispatcher.performSubmit()方法是Dispatcher中非常重要的方法蚀腿,所有的請求都會經(jīng)此方法進(jìn)行提交。該方法主要干了幾件事:
1)從action中取出對應(yīng)的tag,并檢查其是否在pausedTags中莉钙。如果存在廓脆,則暫停執(zhí)行該action并將其放入pausedActions;
2)從active hunters中尋找有無action對應(yīng)的BitmapHunter磁玉。如果有停忿,則直接取出對應(yīng)的BitmapHunter,并將action attach上去蚊伞;
3)若以上條件均不滿足席赂,則為當(dāng)前action創(chuàng)建新的BitmapHunter,并提交給線程池執(zhí)行时迫。

5. hunt目標(biāo)圖片

至此氧枣,我們可以大膽猜測,這個BitmapHunter應(yīng)該就是一個Runnable别垮,圖片下載過程應(yīng)該就在其run()方法中。

class BitmapHunter implements Runnable {

  static BitmapHunter forRequest(Picasso picasso, Dispatcher dispatcher, Cache cache, Stats stats, Action action) {
    Request request = action.getRequest();
    List<RequestHandler> requestHandlers = picasso.getRequestHandlers();

    //遍歷Picasso.getRequestHandlers()扎谎,尋找合適的請求處理器
    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);
    }

  @Override
  public void run() {
    try {
      updateThreadName(data);

      if (picasso.loggingEnabled) {
        log(OWNER_HUNTER, VERB_EXECUTING, getLogIdsForHunter(this));
      }

      //調(diào)用hunt()方法獲取結(jié)果
      // result是一個Bitmap變量
      result = hunt();

      //如果bitmap為null碳想,則圖片獲取失敗,調(diào)用dispatchFailed()方法分發(fā)失敗消息
      //否則毁靶,調(diào)用dispatchComplete()方法分發(fā)成功消息
      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有一個名為forRequest()的靜態(tài)方法胧奔,用于BitmapHunter的實(shí)例化。在該方法內(nèi)部预吆,會遍歷Picasso.getRequestHandlers()列表龙填,尋找合適的RequestHandler。而這個RequestHandlers列表便是在Picasso.new()方法中創(chuàng)建的拐叉,參考2.1節(jié)岩遗。

得到BitmapHunter后,便可將其交給線程池來執(zhí)行了凤瘦。run()方法主要做了兩件事:
1)調(diào)用hunt()方法得到Bitmap結(jié)果宿礁;
2)若成功獲取圖片,則調(diào)用dispatchComplete()方法分發(fā)成功消息蔬芥,否則梆靖,調(diào)用dispatchFailed()方法分發(fā)失敗消息。

接著看BitmapHunter.hunt()方法:

Bitmap hunt() throws IOException {
    Bitmap bitmap = null;

    //從cache中獲取目標(biāo)圖片
    if (shouldReadFromMemoryCache(memoryPolicy)) {
      bitmap = cache.get(key);
      if (bitmap != null) {
        //若找到目標(biāo)圖標(biāo)笔诵,則更新stats狀態(tài)并返回bitmap
        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;

    //調(diào)用requestHandler.load()方法獲取目標(biāo)圖片
    //目標(biāo)圖片會以Bitmap或Stream的形式存在result中
    RequestHandler.Result result = requestHandler.load(data, networkPolicy);
    if (result != null) {
      loadedFrom = result.getLoadedFrom();
      exifRotation = result.getExifOrientation();

      bitmap = result.getBitmap();

      //如果目標(biāo)圖片是Stream形式返吻,則需decodeStream()
      if (bitmap == null) {
        InputStream is = result.getStream();
        try {
          bitmap = decodeStream(is, data);
        } finally {
          Utils.closeQuietly(is);
        }
      }
    }

    if (bitmap != null) {
      if (picasso.loggingEnabled) {
        log(OWNER_HUNTER, VERB_DECODED, data.logId());
      }
      stats.dispatchBitmapDecoded(bitmap);
      //是否需要對圖片進(jìn)行變換
      if (data.needsTransformation() || exifRotation != 0) {
        synchronized (DECODE_LOCK) {
          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()完成的乎婿。而這個RequestHandler就是我們在forRequest()方法中找的請求處理器测僵。

6. 下載目標(biāo)圖片

假設(shè)我們要從網(wǎng)絡(luò)上獲取圖片,那么forRequest()中找的的目標(biāo)RequestHandler應(yīng)該就是NetworkRequestHandler次酌。load()方法自然就是發(fā)送網(wǎng)絡(luò)請求恨课,返回Result的過程舆乔。

@Override 
public Result load(Request request, int networkPolicy) throws IOException {
    //downloader才是request的真正執(zhí)行者
    Response response = downloader.load(request.uri, request.networkPolicy);
    if (response == null) {
      return null;
    }

    Picasso.LoadedFrom loadedFrom = response.cached ? DISK : NETWORK;

    //目標(biāo)圖片以Bitmap的形式返回
    Bitmap bitmap = response.getBitmap();
    if (bitmap != null) {
      return new Result(bitmap, loadedFrom);
    }

    //目標(biāo)圖片以Stream的形式返回
    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);
}

通過源碼發(fā)現(xiàn),NetworkRequestHandler并非request的真正處理者剂公,其內(nèi)部的downloader才是request的負(fù)責(zé)人希俩。

Downloader是一個接口,Picasso內(nèi)置兩種實(shí)現(xiàn):OkHttpDownloader和UrlConnectionDownloader纲辽。如果用戶不主動設(shè)置的話颜武,Picasso.Builder.build()方法會根據(jù)規(guī)則兩者選一:若能反射找到com.squareup.okhttp.OkHttpClient,則使用OkHttpDownloader拖吼,否則downloader為UrlConnectionDownloader鳞上。

這里,我們以UrlConnectionDownloader為例吊档,看一下UrlConnectionDownloader.load()方法:

@Override 
public Response load(Uri uri, int networkPolicy) throws IOException {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
      installCacheIfNeeded(context);
    }
    
    //建立HttpURLConnection 
    HttpURLConnection connection = openConnection(uri);
    //使用cache
    connection.setUseCaches(true);

    if (networkPolicy != 0) {
      String headerValue;

      if (NetworkPolicy.isOfflineOnly(networkPolicy)) {
        headerValue = FORCE_CACHE;
      } else {
        StringBuilder builder = CACHE_HEADER_BUILDER.get();
        builder.setLength(0);

        if (!NetworkPolicy.shouldReadFromDiskCache(networkPolicy)) {
          builder.append("no-cache");
        }
        if (!NetworkPolicy.shouldWriteToDiskCache(networkPolicy)) {
          if (builder.length() > 0) {
            builder.append(',');
          }
          builder.append("no-store");
        }

        headerValue = builder.toString();
      }

      connection.setRequestProperty("Cache-Control", headerValue);
    }

    int responseCode = connection.getResponseCode();
    if (responseCode >= 300) {
      connection.disconnect();
      throw new ResponseException(responseCode + " " + connection.getResponseMessage(),
          networkPolicy, responseCode);
    }

    long contentLength = connection.getHeaderFieldInt("Content-Length", -1);
    boolean fromCache = parseResponseSourceHeader(connection.getHeaderField(RESPONSE_SOURCE));
    //返回Response
    return new Response(connection.getInputStream(), fromCache, contentLength);
}

基本就是HttpURLConnection發(fā)起網(wǎng)絡(luò)請求的過程篙议。

7. 分發(fā)Response

在UrlConnectionDownloader.load()得到Response后,會一層層地將結(jié)果返回:UrlConnectionDownloader.load() ---> NetworkRequestHandler.load() ---> BitmapHunter.hunt() ---> BitmapHunter.run()怠硼。

在BitmapHunter.run()中鬼贱,根據(jù)目標(biāo)圖片是否為null來決定是發(fā)送成功消息,還是失敗消息香璃。

@Override 
public void run() {
      //省略其他代碼
      result = hunt();
      if (result == null) {
        dispatcher.dispatchFailed(this);
      } else {
        dispatcher.dispatchComplete(this);
      }
      //省略其他代碼
}

這里这难,我們假設(shè)分發(fā)成功消息,即調(diào)用dispatcher.dispatchComplete(this)葡秒。

void dispatchComplete(BitmapHunter hunter) {
    handler.sendMessage(handler.obtainMessage(HUNTER_COMPLETE, hunter));
  }

@Override 
public void handleMessage(final Message msg) {
  switch (msg.what) {
    case HUNTER_COMPLETE: {
      BitmapHunter hunter = (BitmapHunter) msg.obj;
      dispatcher.performComplete(hunter);
      break;
    }
    //省略其他代碼
  }
}

void performComplete(BitmapHunter hunter) {
    //是否將目標(biāo)圖片緩存起來
    if (shouldWriteToMemoryCache(hunter.getMemoryPolicy())) {
      cache.set(hunter.getKey(), hunter.getResult());
    }
    //將當(dāng)前的BitmapHunter從active hunters中移除
    hunterMap.remove(hunter.getKey());
    //批處理
    batch(hunter);
    if (hunter.getPicasso().loggingEnabled) {
      log(OWNER_DISPATCHER, VERB_BATCHED, getLogIdsForHunter(hunter), "for completion");
    }
}

在Dispatcher.performComplete()中姻乓,根據(jù)內(nèi)存策略決定是否緩存目標(biāo)圖片并調(diào)用Dispatcher.batch()繼續(xù)批處理。

private void batch(BitmapHunter hunter) {
   if (hunter.isCancelled()) {
     return;
   }

   //將hunter添加到List中
   batch.add(hunter);
   if (!handler.hasMessages(HUNTER_DELAY_NEXT_BATCH)) {
     //延遲200ms發(fā)送消息
     handler.sendEmptyMessageDelayed(HUNTER_DELAY_NEXT_BATCH, BATCH_DELAY);
   }
}

@Override 
public void handleMessage(final Message msg) {
 switch (msg.what) {
   case HUNTER_DELAY_NEXT_BATCH: {
         dispatcher.performBatchComplete();
         break;
   }
   //省略其他代碼
 }
}

Dispatcher.batch()方法會將分發(fā)過來的hunter放到batch列表中眯牧,并延遲發(fā)送蹋岩。

void performBatchComplete() {
    List<BitmapHunter> copy = new ArrayList<BitmapHunter>(batch);
    batch.clear();
    //調(diào)用mainThreadHandler發(fā)送消息
    mainThreadHandler.sendMessage(mainThreadHandler.obtainMessage(HUNTER_BATCH_COMPLETE, copy));
    logBatch(copy);
  }

@Override 
public void handleMessage(final Message msg) {
  switch (msg.what) {
    case HUNTER_BATCH_COMPLETE: {
      List<BitmapHunter> batch = (List<BitmapHunter>) msg.obj;
      //遍歷batch列表,依次處理各個hunter
      for (int i = 0, n = batch.size(); i < n; i++) {
        BitmapHunter hunter = batch.get(i);
        hunter.picasso.complete(hunter);
      }
      break;
    }
    //省略其他代碼
  }
}

在接收到HUNTER_DELAY_NEXT_BATCH消息后学少,Dispatcher.performBatchComplete()調(diào)用主線程handler繼續(xù)分發(fā)消息星澳,實(shí)現(xiàn)線程切換。

8. 加載圖片

在Picasso.handleMessage()收到批處理消息后旱易,會依次取出各個hunter禁偎,并調(diào)用Picasso.complete()。

void complete(BitmapHunter hunter) {
    Action single = hunter.getAction();
    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();

    //處理action
    if (single != null) {
      deliverAction(result, from, single);
    }

    //處理action
    if (hasMultiple) {
      for (int i = 0, n = joined.size(); i < n; i++) {
        Action join = joined.get(i);
        deliverAction(result, from, join);
      }
    }

    if (listener != null && exception != null) {
      listener.onImageLoadFailed(this, uri, exception);
    }
}

private void deliverAction(Bitmap result, LoadedFrom from, Action action) {
    if (action.isCancelled()) {
      return;
    }
    if (!action.willReplay()) {
      targetToAction.remove(action.getTarget());
    }
    if (result != null) {
      if (from == null) {
        throw new AssertionError("LoadedFrom cannot be null.");
      }

      //回調(diào)action.complete()
      action.complete(result, from);
      if (loggingEnabled) {
        log(OWNER_MAIN, VERB_COMPLETED, action.request.logId(), "from " + from);
      }
    } else {
      action.error();
      if (loggingEnabled) {
        log(OWNER_MAIN, VERB_ERRORED, action.request.logId());
      }
    }
}

經(jīng)過Picasso.complete() ---> Picasso.deliverAction()阀坏,最終回調(diào)到ImageViewAction.complete()方法如暖。在該方法中,完成了ImageView加載圖片的過程忌堂。

class ImageViewAction extends Action<ImageView> {
  @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 target = this.target.get();
    if (target == null) {
      return;
    }

    Context context = picasso.context;
    boolean indicatorsEnabled = picasso.indicatorsEnabled;
    //將圖片加載到ImageView上
    PicassoDrawable.setBitmap(target, context, result, from, noFade, indicatorsEnabled);

    if (callback != null) {
      callback.onSuccess();
    }
  }
}

final class PicassoDrawable extends BitmapDrawable {
  static void setBitmap(ImageView target, Context context, Bitmap bitmap,
      Picasso.LoadedFrom loadedFrom, boolean noFade, boolean debugging) {
    Drawable placeholder = target.getDrawable();
    if (placeholder instanceof AnimationDrawable) {
      ((AnimationDrawable) placeholder).stop();
    }
    PicassoDrawable drawable =
        new PicassoDrawable(context, bitmap, placeholder, loadedFrom, noFade, debugging);
    //為ImageView添加圖片
    target.setImageDrawable(drawable);
  }
}

三盒至、總結(jié)

經(jīng)過上述源碼分析,想必大家都已了解Picasso是如何從網(wǎng)絡(luò)下載圖片并加載到ImageView的。這里枷遂,直接用流程圖作簡要總結(jié):
Picasso工作流程

其中樱衷,橙線為發(fā)送過程調(diào)用鏈,綠線為返回過程調(diào)用鏈酒唉。主要步驟如下:
1)在Picasso中創(chuàng)建RequestCreator矩桂;
2)創(chuàng)建并提交Action至Dispatcher;
3)Dispatcher創(chuàng)建BitmapHunter并交給線程池執(zhí)行痪伦;
4)BitmapHunter調(diào)用ResourceHandler及Downloader下載圖片侄榴;
5)圖片下載成功后,返回BitmapHunter并由Dispatcher負(fù)責(zé)分發(fā)网沾;
6)Picasso收到下載成功的消息后癞蚕,回調(diào)Action的complete()方法,將圖片加載到ImageView上辉哥。

最后

本文作為Picasso源碼解析系列的第一篇桦山,目標(biāo)是從源碼入手,梳理Picasso加載圖片的工作流程醋旦,掌握其主要思路度苔。如大家對Picasso框架的實(shí)現(xiàn)細(xì)節(jié)感興趣,歡迎關(guān)注該系列的后續(xù)文章浑度。如對本文有疑問,歡迎留言交流鸦概。如需要轉(zhuǎn)載箩张,則請注明出處。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末窗市,一起剝皮案震驚了整個濱河市先慷,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌咨察,老刑警劉巖论熙,帶你破解...
    沈念sama閱讀 216,470評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異摄狱,居然都是意外死亡脓诡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,393評論 3 392
  • 文/潘曉璐 我一進(jìn)店門媒役,熙熙樓的掌柜王于貴愁眉苦臉地迎上來祝谚,“玉大人,你說我怎么就攤上這事酣衷〗还撸” “怎么了?”我有些...
    開封第一講書人閱讀 162,577評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長席爽。 經(jīng)常有香客問我意荤,道長,這世上最難降的妖魔是什么只锻? 我笑而不...
    開封第一講書人閱讀 58,176評論 1 292
  • 正文 為了忘掉前任玖像,我火速辦了婚禮,結(jié)果婚禮上炬藤,老公的妹妹穿的比我還像新娘御铃。我一直安慰自己,他們只是感情好沈矿,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,189評論 6 388
  • 文/花漫 我一把揭開白布上真。 她就那樣靜靜地躺著,像睡著了一般羹膳。 火紅的嫁衣襯著肌膚如雪睡互。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,155評論 1 299
  • 那天陵像,我揣著相機(jī)與錄音就珠,去河邊找鬼。 笑死醒颖,一個胖子當(dāng)著我的面吹牛妻怎,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播泞歉,決...
    沈念sama閱讀 40,041評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼逼侦,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了腰耙?” 一聲冷哼從身側(cè)響起榛丢,我...
    開封第一講書人閱讀 38,903評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎挺庞,沒想到半個月后晰赞,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,319評論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡选侨,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,539評論 2 332
  • 正文 我和宋清朗相戀三年掖鱼,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片援制。...
    茶點(diǎn)故事閱讀 39,703評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡锨用,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出隘谣,到底是詐尸還是另有隱情增拥,我是刑警寧澤啄巧,帶...
    沈念sama閱讀 35,417評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站掌栅,受9級特大地震影響秩仆,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜猾封,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,013評論 3 325
  • 文/蒙蒙 一澄耍、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧晌缘,春花似錦齐莲、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,664評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至岳枷,卻和暖如春芒填,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背空繁。 一陣腳步聲響...
    開封第一講書人閱讀 32,818評論 1 269
  • 我被黑心中介騙來泰國打工殿衰, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人盛泡。 一個月前我還...
    沈念sama閱讀 47,711評論 2 368
  • 正文 我出身青樓闷祥,卻偏偏與公主長得像,于是被迫代替她去往敵國和親傲诵。 傳聞我的和親對象是個殘疾皇子凯砍,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,601評論 2 353

推薦閱讀更多精彩內(nèi)容