Picasso加载一张图片

2018-11-07  本文已影响6人  _Ryan

目录

Picasso加载一张图片的流程

aaaa.png

build( ) 创建Picasso实例

/** Create the {@link Picasso} instance. */
public Picasso build() {
  Context context = this.context;

  //创建默认下载器
  if (downloader == null) {
    downloader = new OkHttp3Downloader(context);
  }
  
  //创建LRU内存缓存
  if (cache == null) {
    cache = new LruCache(context);
  }
  
  //创建线程池,默认有3个执行线程
  if (service == null) {
    service = new PicassoExecutorService();
  }
  
  //创建默认transformer
  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);
}

//Picasso构造方法 对象赋值&创建一个新对象
//最主要的是初始化了allRequestHandlers 除了自定义的extraRequestHandlers  添加了7个默认的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 {
  
  //来判断当前类型的RequestHandler能否处理request
  public abstract boolean canHandleRequest(Request data);
  
  //使用传入的requst来加载图片,加载的结果放入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;
  }
}

创建好Picasso实例后,便是load了

//load的几个重载方法最后都会来调用参数为Uri的方法 生成一个RequestCreator,
//来执行接下来的trannform() rotate() into()等操作
public RequestCreator load(@Nullable Uri uri) {
  return new RequestCreator(this, uri, 0);
}
上一篇下一篇

猜你喜欢

热点阅读