基本用法
從最基本的用法開(kāi)始
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
1咳蔚,with
讓我們先從with方法開(kāi)始
public static Picasso with(Context context) {
if (singleton == null) {
synchronized (Picasso.class) {
if (singleton == null) {
singleton = new Builder(context).build();
}
}
}
return singleton;
}
static volatile Picasso singleton = null;
一個(gè)標(biāo)準(zhǔn)懶漢式Double Check單例模式脖苏,
/** Start building a new {@link Picasso} instance. */
public Builder(Context context) {
if (context == null) {
throw new IllegalArgumentException("Context must not be null.");
}
this.context = context.getApplicationContext();
}
/** Create the {@link Picasso} instance. */
public Picasso build() {
Context context = this.context;
//下載器
if (downloader == null) {
downloader = Utils.createDefaultDownloader(context);
}
//LRU緩存
if (cache == null) {
cache = new LruCache(context);
}
//線程池
if (service == null) {
service = new PicassoExecutorService();
}
if (transformer == null) {
transformer = RequestTransformer.IDENTITY;
}
Stats stats = new Stats(cache);
//事件分發(fā)者
Dispatcher dispatcher = new Dispatcher(context, service, HANDLER, downloader, cache, stats);
return new Picasso(context, dispatcher, cache, listener, transformer, requestHandlers, stats,
defaultBitmapConfig, indicatorsEnabled, loggingEnabled);
}
}
內(nèi)部使用構(gòu)建者模式,將downloader(下載圖片用)畔乙,lru緩存(內(nèi)存緩存)茴扁,線程池(執(zhí)行下載解析圖片的Runnable),dispatcher(picasso內(nèi)部事件調(diào)度者)初始化
最后我們可以獲取一個(gè)單例的Picasso實(shí)例
讓我們接下來(lái)看load方法
2涉馁,load方法
在picasso類中有4個(gè)重載门岔,我們先看String類型的
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));
}
如果path為空,直接回返回一個(gè)RequestCreator烤送,如果不為空固歪,會(huì)調(diào)用Uri的重載
讓我們看看File對(duì)象的重載
public RequestCreator load(File file) {
if (file == null) {
return new RequestCreator(this, null, 0);
}
return load(Uri.fromFile(file));
}
同樣,最后也是調(diào)用Uri的重載,
/**
* Start an image request using the specified URI.
* <p>
* Passing {@code null} as a {@code uri} will not trigger any request but will set a placeholder,
* if one is specified.
*
* @see #load(File)
* @see #load(String)
* @see #load(int)
*/
public RequestCreator load(Uri uri) {
return new RequestCreator(this, uri, 0);
}
int類型的重載和上面不一樣牢裳,入?yún)⑹琴Y源ID
public RequestCreator load(int resourceId) {
if (resourceId == 0) {
throw new IllegalArgumentException("Resource ID must not be zero.");
}
return new RequestCreator(this, null, resourceId);
}
好了逢防,以上幾個(gè)方法,每次調(diào)用都會(huì)生成一個(gè)RequestCreator對(duì)象蒲讯,讓我們看看他的構(gòu)造方法
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;
this.data = new Request.Builder(uri, resourceId, picasso.defaultBitmapConfig);
}
里面用構(gòu)建者模式忘朝,開(kāi)始準(zhǔn)備一個(gè)request,但是還沒(méi)有發(fā)送
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
從load方法后判帮,我們實(shí)際調(diào)用的是RequestCreator對(duì)象局嘁,我們可以根據(jù)需求,調(diào)用RequestCreator方法
往Request里修改東西晦墙,比如centerCrop等等
3悦昵,into
接下來(lái)看into方法,into方法有5個(gè)重載
我們先從ImageView的重載開(kāi)始看
public void into(ImageView target) {
into(target, null);
}
調(diào)用兩個(gè)方法的重載
/**
* Asynchronously fulfills the request into the specified {@link ImageView} and invokes the
* target {@link Callback} if it's not {@code null}.
* <p>
* <em>Note:</em> The {@link Callback} param is a strong reference and will prevent your
* {@link android.app.Activity} or {@link android.app.Fragment} from being garbage collected. If
* you use this method, it is <b>strongly</b> recommended you invoke an adjacent
* {@link Picasso#cancelRequest(android.widget.ImageView)} call to prevent temporary leaking.
*/
public void into(ImageView target, Callback callback) {
long started = System.nanoTime();
checkMain();
if (target == null) {
throw new IllegalArgumentException("Target must not be null.");
}
if (!data.hasImage()) {
picasso.cancelRequest(target);
if (setPlaceholder) {
setPlaceholder(target, getPlaceholderDrawable());
}
return;
}
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);
}
Request request = createRequest(started);
String requestKey = createKey(request);
if (shouldReadFromMemoryCache(memoryPolicy)) {
Bitmap bitmap = picasso.quickMemoryCacheCheck(requestKey);
if (bitmap != null) {
picasso.cancelRequest(target);
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());
}
Action action =
new ImageViewAction(picasso, target, request, memoryPolicy, networkPolicy, errorResId,
errorDrawable, requestKey, tag, callback, noFade);
picasso.enqueueAndSubmit(action);
}
方法比較長(zhǎng)晌畅,我們一點(diǎn)點(diǎn)來(lái)看
checkMain();
static void checkMain() {
if (!isMain()) {
throw new IllegalStateException("Method call should happen from the main thread.");
}
}
static boolean isMain() {
return Looper.getMainLooper().getThread() == Thread.currentThread();
}
檢測(cè)線程但指,如果調(diào)用into方法的線程不是主線程會(huì)報(bào)異常
"Method call should happen from the main thread."
再往下看
if (!data.hasImage()) {
picasso.cancelRequest(target);
if (setPlaceholder) {
setPlaceholder(target, getPlaceholderDrawable());
}
return;
}
boolean hasImage() {
return uri != null || resourceId != 0;
}
調(diào)用request對(duì)象hasImage,如果uri和resourceId都沒(méi)有抗楔,就會(huì)取消交易棋凳,如果有圖片占位符,則會(huì)直接顯示占位符
接下來(lái)看
if (deferred) {
//fit不能和resize一起使用
if (data.hasSize()) {
throw new IllegalStateException("Fit cannot be used with resize.");
}
//獲取ImageView寬高
int width = target.getWidth();
int height = target.getHeight();
//如果寬和高中有一個(gè)是0
if (width == 0 || height == 0) {
//如果設(shè)置過(guò)圖片占位符连躏,則會(huì)先顯示占位符
if (setPlaceholder) {
setPlaceholder(target, getPlaceholderDrawable());
}
//調(diào)用defer方法剩岳,延遲調(diào)用任務(wù),返
picasso.defer(target, new DeferredRequestCreator(this, target, callback));
return;
}
//如果target這個(gè)ImageView寬高都不為0入热,則拍棕,圖片之后會(huì)被調(diào)整大小和ImageView一樣大
data.resize(width, height);
}
deferred是一個(gè)RequestCreator類的成員變量,是否延期的意思勺良,如果之前調(diào)用過(guò)RequestCreator的fit方法莫湘,會(huì)把deferred值為true
/**
* Attempt to resize the image to fit exactly into the target {@link ImageView}'s bounds. This
* will result in delayed execution of the request until the {@link ImageView} has been laid out.
* <p>
* <em>Note:</em> This method works only when your target is an {@link ImageView}.
*/
public RequestCreator fit() {
deferred = true;
return this;
}
讓我們繼續(xù)into方法繼續(xù)往下看
Request request = createRequest(started);
/** Create the request optionally passing it through the request transformer. */
private Request createRequest(long started) {
int id = nextId.getAndIncrement();
//這里通過(guò)build把request對(duì)象,構(gòu)建好了
Request request = data.build();
request.id = id;
request.started = started;
boolean loggingEnabled = picasso.loggingEnabled;
if (loggingEnabled) {
log(OWNER_MAIN, VERB_CREATED, request.plainId(), request.toString());
}
//如果之前有設(shè)置過(guò)requestTransformer郑气,會(huì)對(duì)request進(jìn)行transform
Request transformed = picasso.transformRequest(request);
if (transformed != request) {
// If the request was changed, copy over the id and timestamp from the original.
transformed.id = id;
transformed.started = started;
if (loggingEnabled) {
log(OWNER_MAIN, VERB_CHANGED, transformed.logId(), "into " + transformed);
}
}
return transformed;
}
我們繼續(xù)從into往下看
String requestKey = createKey(request);
這個(gè)方法,主要是之后圖片內(nèi)存緩存標(biāo)志
接下來(lái)看
//判斷是否從內(nèi)存獲取圖片
if (shouldReadFromMemoryCache(memoryPolicy)) {
//根據(jù)之前生成的key腰池,從內(nèi)存中獲取緩存的bitmap
Bitmap bitmap = picasso.quickMemoryCacheCheck(requestKey);
//如果內(nèi)存中有尾组,則會(huì)完成請(qǐng)求
if (bitmap != null) {
picasso.cancelRequest(target);
//將圖片設(shè)置給ImageView
setBitmap(target, picasso.context, bitmap, MEMORY, noFade, picasso.indicatorsEnabled);
if (picasso.loggingEnabled) {
log(OWNER_MAIN, VERB_COMPLETED, request.plainId(), "from " + MEMORY);
}
//如果設(shè)置了回調(diào),則會(huì)調(diào)用onSuccess
if (callback != null) {
callback.onSuccess();
}
return;
}
}
我們繼續(xù)into方法往下看
if (setPlaceholder) {
setPlaceholder(target, getPlaceholderDrawable());
}
設(shè)置占位符
Action action =
new ImageViewAction(picasso, target, request, memoryPolicy, networkPolicy, errorResId,
errorDrawable, requestKey, tag, callback, noFade);
picasso.enqueueAndSubmit(action);
到這里示弓,我們要開(kāi)始異步請(qǐng)求圖片了
void enqueueAndSubmit(Action action) {
Object target = action.getTarget();
if (target != null && targetToAction.get(target) != action) {
//如果現(xiàn)在map中有一個(gè)ImageView正在進(jìn)行的請(qǐng)求讳侨,或者還沒(méi)有請(qǐng)求
// This will also check we are on the main thread.
// 取消當(dāng)前的請(qǐng)求
cancelExistingRequest(target);
targetToAction.put(target, action);//將target為key,action為值奏属,塞進(jìn)map中
}
submit(action);
}
讓我們來(lái)看看submit方法
void submit(Action action) {
dispatcher.dispatchSubmit(action);
}
調(diào)用Dispatcher來(lái)之前創(chuàng)建的Action
dispatcher在picasso.builder build方法中創(chuàng)建
void dispatchSubmit(Action action) {
handler.sendMessage(handler.obtainMessage(REQUEST_SUBMIT, action));
}
內(nèi)部通過(guò)handler來(lái)傳遞消息
這個(gè)handler再Dispatcher構(gòu)造方法中初始化跨跨,傳入的DispatcherThread的looper,DispatcherThread繼承自HandlerThread,當(dāng)這個(gè)線程運(yùn)行的時(shí)候勇婴,會(huì)自動(dòng)創(chuàng)建一個(gè)looper
實(shí)際上走的是DispatcherHandler忱嘹,讓我們看看這個(gè)類
找到handlerMessage中REQUEST_SUBMIT這個(gè)分支,調(diào)用了dispatcher的performSubmit的方法
void performSubmit(Action action) {
performSubmit(action, true);
}
void performSubmit(Action action, boolean dismissFailed) {
//如果當(dāng)前action處于pause狀態(tài)耕渴,會(huì)暫停加載拘悦,并且放入pausedActions中緩存起來(lái)
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;
}
//如果線程池已經(jīng)關(guān)閉了,則會(huì)直接返回
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());
}
}
先看看forRequest方法
static BitmapHunter forRequest(Picasso picasso, Dispatcher dispatcher, Cache cache, Stats stats,
Action action) {
Request request = action.getRequest();
List<RequestHandler> requestHandlers = picasso.getRequestHandlers();
// Index-based loop to avoid allocating an iterator.
//noinspection ForLoopReplaceableByForEach
//遍歷requestHandlers橱脸,看看是否有可以處理這個(gè)request的handler
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);
}
}
//沒(méi)找到會(huì)把一個(gè)錯(cuò)誤的默認(rèn)handler傳回去
return new BitmapHunter(picasso, dispatcher, cache, stats, action, ERRORING_HANDLER);
}
最后會(huì)返回一個(gè)BitmapHunter础米,他繼承自Runnable
我們繼續(xù)看之前Dispatcher類的performSubmit方法
hunter.future = service.submit(hunter);
會(huì)把剛剛生產(chǎn)的hunter交給線程池去執(zhí)行,這個(gè)線程池也是在picasso構(gòu)建的時(shí)候生成
我們繼續(xù)看剛剛的BitmapHunter添诉,既然是Runnable屁桑,我們先看看他的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);
}
·····
}
調(diào)用hunt方法,result就是返回的bitmap栏赴,那么核心從硬盤網(wǎng)絡(luò)讀取bitmap就是hunt方法了
Bitmap hunt() throws IOException {
Bitmap bitmap = null;
//是否從內(nèi)存中讀取
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;
//通過(guò)之前匹配的RequestHandler來(lái)處理這個(gè)request
RequestHandler.Result result = requestHandler.load(data, networkPolicy);
···
return bitmap;
}
我們回頭看蘑斧,在picasso構(gòu)造的時(shí)候,已經(jīng)加入了好多個(gè)RequestHandler
從這里我們可以看到picasso支持的幾種獲取圖片的途徑
依次是聯(lián)系人相冊(cè)艾帐,媒體庫(kù)乌叶,ContentResolver,asset柒爸,文件准浴,網(wǎng)絡(luò)
我們隨便挑一個(gè)看看
如果之前Picasso的load方法傳入的是file對(duì)象,則會(huì)走FileRequestHandler
如果之前傳入的string里是http路徑捎稚,則會(huì)走NetworkRequestHandler
讓我們來(lái)看看NetworkRequestHandler的實(shí)現(xiàn)
@Override public Result load(Request request, int networkPolicy) throws IOException {
//調(diào)用downloader的load方法乐横, downloader有兩個(gè)實(shí)現(xiàn),一個(gè)是okhttp今野,一個(gè)是urlconnection
Response response = downloader.load(request.uri, request.networkPolicy);
//如果沒(méi)有結(jié)果葡公,則會(huì)返回
if (response == null) {
return null;
}
Picasso.LoadedFrom loadedFrom = response.cached ? DISK : NETWORK;
//獲取圖片
Bitmap bitmap = response.getBitmap();
if (bitmap != null) {
//如果獲取到了圖片,則會(huì)返回
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);
}
最新的方法里条霜,downloader返回的response都不會(huì)直接返回bitmap催什,而是inputstream
最后load方法會(huì)把inputsteam流封裝人result返回
回到BitmapHunter hunt方法
Bitmap hunt() throws IOException {
```
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 {
//從inputstream轉(zhuǎn)換成bitmap
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) {
//如果圖片中Exif有旋轉(zhuǎn)信息,進(jìn)行旋轉(zhuǎn)
bitmap = transformResult(data, bitmap, exifRotation);
if (picasso.loggingEnabled) {
log(OWNER_HUNTER, VERB_TRANSFORMED, data.logId());
}
}
if (data.hasCustomTransformations()) {
//如果requestCreator設(shè)置了transformation
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;
}
我們先來(lái)看decodeStream方法
/**
* Decode a byte stream into a Bitmap. This method will take into account additional information
* about the supplied request in order to do the decoding efficiently (such as through leveraging
* {@code inSampleSize}).
*/
static Bitmap decodeStream(InputStream stream, Request request) throws IOException {
MarkableInputStream markStream = new MarkableInputStream(stream);
stream = markStream;
long mark = markStream.savePosition(65536); // TODO fix this crap.
final BitmapFactory.Options options = RequestHandler.createBitmapOptions(request);
final boolean calculateSize = RequestHandler.requiresInSampleSize(options);
boolean isWebPFile = Utils.isWebPFile(stream);
markStream.reset(mark);
// When decode WebP network stream, BitmapFactory throw JNI Exception and make app crash.
// Decode byte array instead
if (isWebPFile) {
byte[] bytes = Utils.toByteArray(stream);
if (calculateSize) {
BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
RequestHandler.calculateInSampleSize(request.targetWidth, request.targetHeight, options,
request);
}
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
} else {
if (calculateSize) {
BitmapFactory.decodeStream(stream, null, options);
RequestHandler.calculateInSampleSize(request.targetWidth, request.targetHeight, options,
request);
markStream.reset(mark);
}
Bitmap bitmap = BitmapFactory.decodeStream(stream, null, options);
if (bitmap == null) {
// Treat null as an IO exception, we will eventually retry.
throw new IOException("Failed to decode stream.");
}
return bitmap;
}
}
在這里獲取request中的圖片格式(config) 和 設(shè)置好的寬高(resize方法)
config如果之前沒(méi)有設(shè)置宰睡,默認(rèn)是ARGB_8888蒲凶,
如果設(shè)置了resize,在這里則會(huì)使用inSampleSize調(diào)整圖片大小拆内,否則會(huì)按默認(rèn)大小加載
然后回到run方法
返回給dispatcher
void dispatchComplete(BitmapHunter hunter) {
handler.sendMessage(handler.obtainMessage(HUNTER_COMPLETE, hunter));
}
回到DispatcherHandler
去到dispatcher的performComplete
void performComplete(BitmapHunter hunter) {
if (shouldWriteToMemoryCache(hunter.getMemoryPolicy())) {
cache.set(hunter.getKey(), hunter.getResult());
}
hunterMap.remove(hunter.getKey());
batch(hunter);
if (hunter.getPicasso().loggingEnabled) {
log(OWNER_DISPATCHER, VERB_BATCHED, getLogIdsForHunter(hunter), "for completion");
}
}
在這里旋圆,根據(jù)對(duì)request設(shè)置的內(nèi)存策略,判斷需不需要內(nèi)存緩存麸恍,
如果需要灵巧,會(huì)放入LRUCache里
重點(diǎn)看batch,準(zhǔn)備批量任務(wù)
private void batch(BitmapHunter hunter) {
if (hunter.isCancelled()) {
return;
}
batch.add(hunter);
if (!handler.hasMessages(HUNTER_DELAY_NEXT_BATCH)) {
handler.sendEmptyMessageDelayed(HUNTER_DELAY_NEXT_BATCH, BATCH_DELAY);
}
}
回到DispatcerHandler
void performBatchComplete() {
List<BitmapHunter> copy = new ArrayList<BitmapHunter>(batch);
batch.clear();
mainThreadHandler.sendMessage(mainThreadHandler.obtainMessage(HUNTER_BATCH_COMPLETE, copy));
logBatch(copy);
}
批量任務(wù)都已經(jīng)準(zhǔn)備就緒
static final Handler HANDLER = new Handler(Looper.getMainLooper()) {
@Override public void handleMessage(Message msg) {
switch (msg.what) {
case HUNTER_BATCH_COMPLETE: {
@SuppressWarnings("unchecked") List<BitmapHunter> batch = (List<BitmapHunter>) msg.obj;
//noinspection ForLoopReplaceableByForEach
for (int i = 0, n = batch.size(); i < n; i++) {
BitmapHunter hunter = batch.get(i);
//
hunter.picasso.complete(hunter);
}
break;
}
···
}
}
};
最后for循環(huán)遍歷batch
hunter.picasso.complete(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();
if (single != null) {
deliverAction(result, from, single);
}
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);
}
}
終于看到了complete方法,但是還沒(méi)完刻肄,這里核心是走deliverAction方法
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.");
}
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());
}
}
}
action.complete(result, from);
主要看action.complete(result, from);
之前我們?cè)诎讶蝿?wù)加入dispatcher的時(shí)候瓤球,new了一個(gè)ImageViewAction,ImageViewAction繼承自Action
查看Action的繼承肄方,有很多子類
我們最開(kāi)始設(shè)置的是ImageView冰垄,我們看看ImageViewAction的complete實(shí)現(xiàn)
ImageViewAction(Picasso picasso, ImageView imageView, Request data, int memoryPolicy,
int networkPolicy, int errorResId, Drawable errorDrawable, String key, Object tag,
Callback callback, boolean noFade) {
super(picasso, imageView, data, memoryPolicy, networkPolicy, errorResId, errorDrawable, key,
tag, noFade);
this.callback = callback;
}
@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;
PicassoDrawable.setBitmap(target, context, result, from, noFade, indicatorsEnabled);
if (callback != null) {
callback.onSuccess();
}
}
重點(diǎn)看這一句
PicassoDrawable.setBitmap(target, context, result, from, noFade, indicatorsEnabled);
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);
target.setImageDrawable(drawable);
}
target.setImageDrawable(drawable);
這個(gè)target就是我們的ImageView
好了,到這里权她,我們終于把我們的Bitmap轉(zhuǎn)換成PicassoDrawable虹茶,PicassoDrawable繼承自BitmapDrawable,最后塞給ImageView了隅要,
如果這里不是ImageView蝴罪,流程也都大體相似,
如果我們?cè)谡{(diào)用picasso的into方法時(shí)步清,傳入的時(shí)一個(gè)target實(shí)現(xiàn)類要门,最終會(huì)通過(guò)調(diào)用Target的onBitmaoLoaded方法完成圖片的加載