Picasso源码分析

来源:互联网 发布:linux 系统api 编辑:程序博客网 时间:2024/05/16 02:59

项目介绍

Picasso是Square公司开源的一个Android平台上的图片加载框架,也是大名鼎鼎的JakeWharton的代表作品之一.

特点

(1) 自带统计监控功能
支持图片缓存使用的监控,包括缓存命中率、已使用内存大小、节省的流量等。
 
(2) 支持优先级处理
每次任务调度前会选择优先级高的任务,比如 App 页面中 Banner 的优先级高于 Icon 时就很适用。
 
(3) 支持延迟到图片尺寸计算完成加载
 
(4) 支持飞行模式、并发线程数根据网络类型而变
手机切换到飞行模式或网络类型变换时会自动调整线程池最大并发数,比如 wifi 最大并发为 4, 4g 为 3,3g 为 2。
这里 Picasso 根据网络类型来决定最大并发数,而不是 CPU 核数。
 
(5) “无”本地缓存
无”本地缓存,不是说没有本地缓存,而是 Picasso 自己没有实现,交给了 Square 的另外一个网络库 okhttp 去实现,这样的好处是可以通过请求 Response Header 中的 Cache-Control 及 Expired 控制图片的过期时间。

简单用法

Picasso.with(SplashActivity.this).load("http://www.baidu.com/img/bdlogo.png").into(mIvPicassoTest);

总体设计

上面是 Picasso 的总体设计图。整个库分为 Dispatcher,RequestHandler 及 Downloader,PicassoDrawable 等模块。
 
Dispatcher 负责分发和处理 Action,包括提交、暂停、继续、取消、网络状态变化、重试等等。
 
简单的讲就是 Picasso 收到加载及显示图片的任务,创建 Request 并将它交给 Dispatcher,Dispatcher 分发任务到具体 RequestHandler,任务通过 MemoryCache 及 Handler(数据获取接口) 获取图片,图片获取成功后通过 PicassoDrawable 显示到 Target 中。
 
需要注意的是上面 Data 的 File system 部分,Picasso 没有自定义本地缓存的接口,默认使用 http 的本地缓存,API 9 以上使用 okhttp,以下使用 Urlconnection,所以如果需要自定义本地缓存就需要重定义 Downloader。

类图

流程分析

以下从源码角度分析一下流程:

我们首先会调用Picasso的with方法,我们来看以下with方法的实现

  public static Picasso with(Context context) {    if (singleton == null) {      synchronized (Picasso.class) {        if (singleton == null) {          singleton = new Builder(context).build();        }      }    }    return singleton;  }

可以看到这里是一个单例模式,通过这个单例模式来创建了Picasso对象,创建也不是采用通常的new一个对象出来,而是采用的建造者模式。只不过是建造者模式的简写方式(这快我在后面的博客中有写到,可到后面的博客中去看)。
之后会调用load方法,源码如下:

  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));  }

继续往下看,调用到load(Uri.parse(path))

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

之后继续往下走,会调用到RequestCreator的into方法:

  public void into(Target target) {    long started = System.nanoTime();    checkMain();    if (target == null) {      throw new IllegalArgumentException("Target must not be null.");    }    if (deferred) {      throw new IllegalStateException("Fit cannot be used with a Target.");    }    if (!data.hasImage()) {      picasso.cancelRequest(target);      target.onPrepareLoad(setPlaceholder ? getPlaceholderDrawable() : null);      return;    }    Request request = createRequest(started);    String requestKey = createKey(request);    if (shouldReadFromMemoryCache(memoryPolicy)) {      Bitmap bitmap = picasso.quickMemoryCacheCheck(requestKey);      if (bitmap != null) {        picasso.cancelRequest(target);        target.onBitmapLoaded(bitmap, MEMORY);        return;      }    }    target.onPrepareLoad(setPlaceholder ? getPlaceholderDrawable() : null);    Action action =        new TargetAction(picasso, target, request, memoryPolicy, networkPolicy, errorDrawable,            requestKey, tag, errorResId);    picasso.enqueueAndSubmit(action);  }

这里重点对这块代码进行分析一把,可以看到这里会先从内存中取数据,有的话就返回,没有的话就继续向下,走到这一块:

    Action action =        new TargetAction(picasso, target, request, memoryPolicy, networkPolicy, errorDrawable,            requestKey, tag, errorResId);    picasso.enqueueAndSubmit(action);

即走到Picasso的enqueueAndSubmit方法

  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);  }

再调用到Picasso的submit(action);

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

再调用到dispatcher的dispatchSubmit方法

  void dispatchSubmit(Action action) {    handler.sendMessage(handler.obtainMessage(REQUEST_SUBMIT, action));  }

这个handler是哪个线程的呢?

this.handler = new DispatcherHandler(dispatcherThread.getLooper(), this);
 static class DispatcherThread extends HandlerThread {    DispatcherThread() {      super(Utils.THREAD_PREFIX + DISPATCHER_THREAD_NAME, THREAD_PRIORITY_BACKGROUND);    }  }

这里用到了一个handlerthread,我们可以看出来,dispatcher的dispatchSubmit是在UI线程中的,通用了DispatcherThread的Handler发送了一条消息,将消息post到了DispatcherThread的消息队列中,DispatcherHandler收到这个消息后会在DispatcherThread中处理这个消息,具体的处理方式如下:

  private static class DispatcherHandler extends Handler {    private final Dispatcher dispatcher;    public 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;        }        case REQUEST_CANCEL: {          Action action = (Action) msg.obj;          dispatcher.performCancel(action);          break;        }        case TAG_PAUSE: {          Object tag = msg.obj;          dispatcher.performPauseTag(tag);          break;        }        case TAG_RESUME: {          Object tag = msg.obj;          dispatcher.performResumeTag(tag);          break;        }        case HUNTER_COMPLETE: {          BitmapHunter hunter = (BitmapHunter) msg.obj;          dispatcher.performComplete(hunter);          break;        }        case HUNTER_RETRY: {          BitmapHunter hunter = (BitmapHunter) msg.obj;          dispatcher.performRetry(hunter);          break;        }        case HUNTER_DECODE_FAILED: {          BitmapHunter hunter = (BitmapHunter) msg.obj;          dispatcher.performError(hunter, false);          break;        }        case HUNTER_DELAY_NEXT_BATCH: {          dispatcher.performBatchComplete();          break;        }        case NETWORK_STATE_CHANGE: {          NetworkInfo info = (NetworkInfo) msg.obj;          dispatcher.performNetworkStateChange(info);          break;        }        case AIRPLANE_MODE_CHANGE: {          dispatcher.performAirplaneModeChange(msg.arg1 == AIRPLANE_MODE_ON);          break;        }        default:          Picasso.HANDLER.post(new Runnable() {            @Override public void run() {              throw new AssertionError("Unknown handler message received: " + msg.what);            }          });      }    }  }

我们以分支:来分析

case REQUEST_SUBMIT: {          Action action = (Action) msg.obj;          dispatcher.performSubmit(action);          break;        }

即会在DispatcherThread这个子线程中执行dispatcher.performSubmit(action);这个方法,具体看这个方法的源码:

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

又调用到了performSubmit这个方法,具体源码如下:

  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 = 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());    }  }

第一次的话hunter不存在,会通过forRequest(action.getPicasso(), this, cache, stats, action)这个方法创建一个BitmapHunter。BitmapHunter是一个Runnable。之后调用service.submit(hunter),即这个BitmapHunter被放到了线程池中去执行。

下面我们具体看一下BitmapHunter的run方法:

  @Override public void run() {    try {      updateThreadName(data);      if (picasso.loggingEnabled) {        log(OWNER_HUNTER, VERB_EXECUTING, getLogIdsForHunter(this));      }      result = hunt();      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);    }  }

首先看result = hunt();

看一下hunt方法的具体实现:

  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;      }    }    data.networkPolicy = retryCount == 0 ? NetworkPolicy.OFFLINE.index : networkPolicy;    RequestHandler.Result result = requestHandler.load(data, networkPolicy);    if (result != null) {      loadedFrom = result.getLoadedFrom();      exifRotation = result.getExifOrientation();      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 {          bitmap = decodeStream(is, data);        } finally {          Utils.closeQuietly(is);        }      }    }    if (bitmap != null) {      if (picasso.loggingEnabled) {        log(OWNER_HUNTER, VERB_DECODED, data.logId());      }      stats.dispatchBitmapDecoded(bitmap);      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;  }

这里会首先从文件中取,没有就从网络上取,然后返回bitmap。

网络访问具体在这里:

    RequestHandler.Result result = requestHandler.load(data, networkPolicy);

具体调用到了NetworkRequestHandler的load方法:

 @Override public Result load(Request request, int networkPolicy) throws IOException {    Response response = downloader.load(request.uri, request.networkPolicy);    if (response == null) {      return null;    }    Picasso.LoadedFrom loadedFrom = response.cached ? DISK : NETWORK;    Bitmap bitmap = response.getBitmap();    if (bitmap != null) {      return new Result(bitmap, loadedFrom);    }    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);  }

这里具体是调用 Response response = downloader.load(request.uri, request.networkPolicy);

我们看到其实调用的是downloader.load。

我们看downloader,会发现这是一个接口,有两种实现:OkHttpDownloader和UrlConnectionDownloader。即在这两种下载器中去做具体的网络请求。

todo问题

并发处理的地方还不是特别理解?
网络请求的地方还是看不太明白,用了代理??
它确实没有实现文件缓存,

0 0