Picasso源码分析

Picasso源码分析_第1张图片

一张图片加载可以分为以下几步:
创建->入队->执行->解码->变换->批处理->完成->分发->显示(可选)

涉及到的设计模式
1.单例
2.Builder
3.责任链模式

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

  //从requestHandlers中检测哪个RequestHandler可以处理这个request,如果找到就创建
  //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);
}

  1. private Downloader downloader;
    此对象是用来下载网络上的图片
  • private Cache cache;
    cache对象把图片资源缓存在内存当中,内存容量是有大小限制的,但超过限制的时候,会消除最老的图片资源
    LinkedHashMap中保存Bitmap的一个对象引用。
  • private ExecutorService service;
    线程池,用来下载图片和解析图片的。
  • private RequestTransformer transformer;
    对图片资源进行变换操作。
  • Stats stats
    状态通知中心,通知状态的完成情况。
  • Dispatcher dispatcher
    操作管理中心,可以用来管理操作的执行,可以取消操作。
    把他们集合起来,就是
    return new Picasso(context, dispatcher, cache, listener, transformer, stats,
    indicatorsEnabled, loggingEnabled);

Picasso源代码分析

你可能感兴趣的:(Picasso源码分析)