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);
publicvoidinto(ImageView target, Callback callback){ long started = System.nanoTime(); //检查调用是否在主线程 checkMain();
if (target == null) { thrownew IllegalArgumentException("Target must not be null."); } //如果没有设置需要加载的uri,或者resourceId if (!data.hasImage()) { picasso.cancelRequest(target); //如果设置占位图片,直接加载并返回 if (setPlaceholder) { setPlaceholder(target, getPlaceholderDrawable()); } return; } //如果是延时加载,也就是选择了fit()模式 if (deferred) { //fit()模式是适应target的宽高加载,所以并不能手动设置resize,如果设置就抛出异常 if (data.hasSize()) { thrownew IllegalStateException("Fit cannot be used with resize."); } int width = target.getWidth(); int height = target.getHeight(); //如果目标ImageView的宽或高现在为0 if (width == 0 || height == 0) { //先设置占位符 if (setPlaceholder) { setPlaceholder(target, getPlaceholderDrawable()); } //监听ImageView的ViewTreeObserver.OnPreDrawListener接口,一旦ImageView //的宽高被赋值,就按照ImageView的宽高继续加载. picasso.defer(target, new DeferredRequestCreator(this, target, callback)); return; } //如果ImageView有宽高就设置设置 data.resize(width, height); }
voidenqueueAndSubmit(Action action){ Object target = action.getTarget(); //取消这个target已经有的action. if (target != null && targetToAction.get(target) != action) { // This will also check we are on the main thread. cancelExistingRequest(target); targetToAction.put(target, action); } //提交action submit(action); } //调用dispatcher来派发action voidsubmit(Action action){ dispatcher.dispatchSubmit(action); }
// 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); //处理Transformation if (data.needsTransformation() || exifOrientation != 0) { 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; }
voidcomplete(BitmapHunter hunter){ //获取单个Action Action single = hunter.getAction(); //获取被添加进来的Action List<Action> joined = hunter.getActions();
//是否有合并的Action 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) { //noinspection ForLoopReplaceableByForEach 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); } } privatevoiddeliverAction(Bitmap result, LoadedFrom from, Action action){ if (action.isCancelled()) { return; } if (!action.willReplay()) { targetToAction.remove(action.getTarget()); } if (result != null) { if (from == null) { thrownew AssertionError("LoadedFrom cannot be null."); } //回调action的complete()方法 action.complete(result, from); if (loggingEnabled) { log(OWNER_MAIN, VERB_COMPLETED, action.request.logId(), "from " + from); } } else { //失败则回调error()方法 action.error(); if (loggingEnabled) { log(OWNER_MAIN, VERB_ERRORED, action.request.logId()); } } }
@Overridepublicvoidcomplete(Bitmap result, Picasso.LoadedFrom from){ if (result == null) { thrownew AssertionError( String.format("Attempted to complete action with no result!\n%s", this)); }