picasso的使用非常簡(jiǎn)單
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
1.0 單例模式 生成一個(gè)Picasso對(duì)象
public static Picasso with(Context context) {
if (singleton == null) {
synchronized (Picasso.class) {
if (singleton == null) {
singleton = new Builder(context).build();
}
}
}
return singleton;
}
2.0 構(gòu)建Picasso對(duì)象時(shí)進(jìn)行初始化各個(gè)成員變量
public Picasso build() {
Context context = this.context;
// 2.1 實(shí)現(xiàn)Downloader的接口,這個(gè)接口主要有以下抽象方法
// Response load(Uri uri, int networkPolicy) throws IOException;
// 此Response非OkHttp里面的Response序目,而是Downloader的內(nèi)部類塘幅,主要存儲(chǔ)了Bitmap,InputStream對(duì)象 圖片數(shù)據(jù)
if (downloader == null) {
downloader = Utils.createDefaultDownloader(context);
}
// 2.2 內(nèi)存緩存途事,使用最近最少使用算法
if (cache == null) {
cache = new LruCache(context);
}
// 2.3 構(gòu)建線程池
if (service == null) {
service = new PicassoExecutorService();
}
// 2.4 對(duì)Request對(duì)象(非OkHttp里面的Request)進(jìn)行轉(zhuǎn)換接口访惜,默認(rèn)不做任何操作,給用戶自定義
if (transformer == null) {
transformer = RequestTransformer.IDENTITY;
}
// 2.5 創(chuàng)建Stats對(duì)象主要用來(lái)統(tǒng)計(jì)的,日志記錄葛假。
Stats stats = new Stats(cache);
// 2.6 創(chuàng)建Dispatcher對(duì)象
Dispatcher dispatcher = new Dispatcher(context, service, HANDLER, downloader, cache, stats);
// 2.7 生成Picasso對(duì)象
return new Picasso(context, dispatcher, cache, listener, transformer, requestHandlers, stats,
defaultBitmapConfig, indicatorsEnabled, loggingEnabled);
}
2.1.1 若有OkHttpClient類就使用OkHttp庫(kù)進(jìn)行網(wǎng)絡(luò)請(qǐng)求下載圖片,否則使用UrlConnection
static Downloader createDefaultDownloader(Context context) {
try {
Class.forName("com.squareup.okhttp.OkHttpClient");
return OkHttpLoaderCreator.create(context);
} catch (ClassNotFoundException ignored) {
}
return new UrlConnectionDownloader(context);
}
2.1.2 創(chuàng)建OkHttpDownloader對(duì)象滋恬,并設(shè)置硬盤緩存聊训,硬盤緩存大小為硬盤的50分之1 緩存大小為最大50M,最小5M.
private static class OkHttpLoaderCreator {
static Downloader create(Context context) {
return new OkHttpDownloader(context);
}
}
public OkHttpDownloader(final Context context) {
this(Utils.createDefaultCacheDir(context));
}
static File createDefaultCacheDir(Context context) {
File cache = new File(context.getApplicationContext().getCacheDir(), PICASSO_CACHE);
if (!cache.exists()) {
//noinspection ResultOfMethodCallIgnored
cache.mkdirs();
}
return cache;
}
public OkHttpDownloader(final File cacheDir) {
this(cacheDir, Utils.calculateDiskCacheSize(cacheDir));
}
static long calculateDiskCacheSize(File dir) {
long size = MIN_DISK_CACHE_SIZE;
try {
StatFs statFs = new StatFs(dir.getAbsolutePath());
long available = ((long) statFs.getBlockCount()) * statFs.getBlockSize();
// Target 2% of the total space.
size = available / 50;
} catch (IllegalArgumentException ignored) {
}
// Bound inside min/max size for disk cache.
return Math.max(Math.min(size, MAX_DISK_CACHE_SIZE), MIN_DISK_CACHE_SIZE);
}
public OkHttpDownloader(final File cacheDir, final long maxSize) {
this(defaultOkHttpClient());
try {
client.setCache(new com.squareup.okhttp.Cache(cacheDir, maxSize));
} catch (IOException ignored) {
}
}
2.2.1 內(nèi)存緩存,大小為app最大使用內(nèi)存的7分之1
public LruCache(Context context) {
this(Utils.calculateMemoryCacheSize(context));
}
static int calculateMemoryCacheSize(Context context) {
ActivityManager am = getService(context, ACTIVITY_SERVICE);
boolean largeHeap = (context.getApplicationInfo().flags & FLAG_LARGE_HEAP) != 0;
int memoryClass = am.getMemoryClass();
if (largeHeap && SDK_INT >= HONEYCOMB) {
memoryClass = ActivityManagerHoneycomb.getLargeMemoryClass(am);
}
// Target ~15% of the available heap.
return 1024 * 1024 * memoryClass / 7;
}
2.3.1 創(chuàng)建線程池
PicassoExecutorService() {
super(DEFAULT_THREAD_COUNT, DEFAULT_THREAD_COUNT, 0, TimeUnit.MILLISECONDS,
new PriorityBlockingQueue<Runnable>(), new Utils.PicassoThreadFactory());
}
2.4.1. 默認(rèn)不做任何操作恢氯,用戶可自定義
RequestTransformer IDENTITY = new RequestTransformer() {
@Override public Request transformRequest(Request request) {
return request;
}
};
}
2.5.1.創(chuàng)建一個(gè)后臺(tái)線程带斑,進(jìn)行記錄Picasso運(yùn)行狀態(tài)
Stats(Cache cache) {
this.cache = cache;
this.statsThread = new HandlerThread(STATS_THREAD_NAME, THREAD_PRIORITY_BACKGROUND);
this.statsThread.start();
Utils.flushStackLocalLeaks(statsThread.getLooper());
this.handler = new StatsHandler(statsThread.getLooper(), this);
}
2.6.1 創(chuàng)建Dispatcher對(duì)象
Dispatcher(Context context, ExecutorService service, Handler mainThreadHandler,
Downloader downloader, Cache cache, Stats stats) {
...
this.handler = new DispatcherHandler(dispatcherThread.getLooper(), this);
this.downloader = downloader;
this.mainThreadHandler = mainThreadHandler;
...
}
2.7.1 創(chuàng)建7個(gè)RequestHandler 處理不同數(shù)據(jù)來(lái)源的生成Result包含Bitmap,InputStream對(duì)象
RequestHandler是一個(gè)抽象類 抽象方法如下
public abstract Result load(Request request, int networkPolicy) throws IOException;
public abstract boolean canHandleRequest(Request data);
這7個(gè)RequestHandler勋拟,除ResourceRequestHandler大多用Request的Uri對(duì)象進(jìn)行區(qū)分勋磕,我們也可以實(shí)現(xiàn)以上抽象方法自定義自己的RequestHandler進(jìn)行擴(kuò)展,以便處理這7個(gè)以外的特殊圖片數(shù)據(jù)來(lái)源
Picasso(Context context, Dispatcher dispatcher, Cache cache, Listener listener,
RequestTransformer requestTransformer, List<RequestHandler> extraRequestHandlers, Stats stats,
Bitmap.Config defaultBitmapConfig, boolean indicatorsEnabled, boolean loggingEnabled) {
...
int builtInHandlers = 7; // Adjust this as internal handlers are added or removed.
int extraCount = (extraRequestHandlers != null ? extraRequestHandlers.size() : 0);
List<RequestHandler> allRequestHandlers =
new ArrayList<RequestHandler>(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);
...
}
以上就是第一次調(diào)用with方法所做的操作
3.0. load方法
創(chuàng)建RequestCreator對(duì)象敢靡,配置好uri挂滓,resourceId;若是從網(wǎng)絡(luò)中讀取圖片,resourceId為0
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));
}
public RequestCreator load(Uri uri) {
//
return new RequestCreator(this, uri, 0);
}
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);
}
//
Builder(Uri uri, int resourceId, Bitmap.Config bitmapConfig) {
this.uri = uri;
this.resourceId = resourceId;
this.config = bitmapConfig;
}
4. into 方法
4.1. 圖片綁定到ImageView
public void into(ImageView target) {
into(target, null);
}
4.2 into方法的解析
public void into(ImageView target, Callback callback) {
long started = System.nanoTime();
// 檢查是否是在主線程中操作UI
checkMain();
if (target == null) {
throw new IllegalArgumentException("Target must not be null.");
}
// 是否設(shè)置了uri,或者resourceId醋安,在load方法已經(jīng)設(shè)置了uri杂彭,為false不執(zhí)行
if (!data.hasImage()) {
picasso.cancelRequest(target);
if (setPlaceholder) {
setPlaceholder(target, getPlaceholderDrawable());
}
return;
}
// deferred 默認(rèn)為false,不執(zhí)行。除非調(diào)用了fit()方法
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);
}
// 4.2.1
Request request = createRequest(started);
// 根據(jù)Request的特性生成獨(dú)一無(wú)二的requestKey
String requestKey = createKey(request);
// 4.2.2 若未設(shè)置memoryPolicy 此為ture
if (shouldReadFromMemoryCache(memoryPolicy)) {
// 通過(guò)requestKey從內(nèi)存緩存中獲取吓揪,第一次網(wǎng)絡(luò)請(qǐng)求未存儲(chǔ)獲取不到亲怠,bitmap為null
Bitmap bitmap = picasso.quickMemoryCacheCheck(requestKey);
if (bitmap != null) {
picasso.cancelRequest(target);
setBitmap(target, picasso.context, bitmap, MEMORY, noFade, picasso.indicatorsEnabled);
...
if (callback != null) {
callback.onSuccess();
}
return;
}
}
// 設(shè)置Placeholder,若未Placeholder顯示空白,等待下載圖片時(shí) 的替代圖片顯示到ImageView
if (setPlaceholder) {
setPlaceholder(target, getPlaceholderDrawable());
}
// 4.2.3
Action action =
new ImageViewAction(picasso, target, request, memoryPolicy, networkPolicy, errorResId,
errorDrawable, requestKey, tag, callback, noFade);
// 4.2.4
picasso.enqueueAndSubmit(action);
}
4.2.1 生成Request對(duì)象
private Request createRequest(long started) {
// 生成獨(dú)一無(wú)二的ID
int id = nextId.getAndIncrement();
// 3.3 生成的Request.Builder
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());
}
// 參考2.4 RequestTransformer對(duì)象進(jìn)行轉(zhuǎn)換處理Request柠辞,id和started開始時(shí)間不變
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;
}
4.2.2 獲取存儲(chǔ)策略
/** Skips memory cache lookup when processing a request. */
NO_CACHE(1 << 0),
/**
* Skips storing the final result into memory cache. Useful for one-off requests
* to avoid evicting other bitmaps from the cache.
*/
NO_STORE(1 << 1);
static boolean shouldReadFromMemoryCache(int memoryPolicy) {
return (memoryPolicy & MemoryPolicy.NO_CACHE.index) == 0;
}
static boolean shouldWriteToMemoryCache(int memoryPolicy) {
return (memoryPolicy & MemoryPolicy.NO_STORE.index) == 0;
}
4.2.3 創(chuàng)建ImageViewAction對(duì)象
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;
}
4.2.4 調(diào)用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);
}
void submit(Action action) {
dispatcher.dispatchSubmit(action);
}
void dispatchSubmit(Action action) {
handler.sendMessage(handler.obtainMessage(REQUEST_SUBMIT, action));
}
@Override public void handleMessage(final Message msg) {
switch (msg.what) {
case REQUEST_SUBMIT: {
Action action = (Action) msg.obj;
dispatcher.performSubmit(action);
break;
}
void performSubmit(Action action) {
performSubmit(action, true);
}
void performSubmit(Action action, boolean dismissFailed) {
// pausedTags暫停對(duì)象集合 若是Action設(shè)置了tag 同一個(gè)對(duì)象就會(huì)暫停執(zhí)行
if (pausedTags.contains(action.getTag())) {
pausedActions.put(action.getTarget(), action);
...
...
return;
}
// hunterMap若是存儲(chǔ)了BitmapHunter對(duì)象就不用生成BitmapHunter团秽,第一次未存儲(chǔ)未null
BitmapHunter hunter = hunterMap.get(action.getKey());
if (hunter != null) {
hunter.attach(action);
return;
}
if (service.isShutdown()) {
...
...
return;
}
// 4.2.4.1
hunter = forRequest(action.getPicasso(), this, cache, stats, action);
// 4.2.4.2
// 添加到線程池會(huì)執(zhí)行BitmapHunter的 run()方法
hunter.future = service.submit(hunter);
// 放入hunterMap,下次就可直接使用
hunterMap.put(action.getKey(), hunter);
// 若是之前失敗過(guò),移除掉失敗的ImageView的對(duì)應(yīng)的BitmapHunter對(duì)象
if (dismissFailed) {
failedActions.remove(action.getTarget());
}
...
...
}
4.2.4.1 構(gòu)建BitmapHunter對(duì)象
static BitmapHunter forRequest(Picasso picasso, Dispatcher dispatcher, Cache cache, Stats stats,
Action action) {
Request request = action.getRequest();
List<RequestHandler> requestHandlers = picasso.getRequestHandlers();
for (int i = 0, count = requestHandlers.size(); i < count; i++) {
RequestHandler requestHandler = requestHandlers.get(i);
// 參考 2.7.1設(shè)置的7個(gè)RequestHandler
// 每個(gè)RequestHandler都實(shí)現(xiàn)了canHandleRequest习勤,判斷處理不同的圖片數(shù)據(jù)源
// 我們?cè)O(shè)置的是通過(guò) NetworkRequestHandler處理
if (requestHandler.canHandleRequest(request)) {
return new BitmapHunter(picasso, dispatcher, cache, stats, action, requestHandler);
}
}
return new BitmapHunter(picasso, dispatcher, cache, stats, action, ERRORING_HANDLER);
}
BitmapHunter(Picasso picasso, Dispatcher dispatcher, Cache cache, Stats stats, Action action,
RequestHandler requestHandler) {
this.sequence = SEQUENCE_GENERATOR.incrementAndGet();
this.picasso = picasso;
this.dispatcher = dispatcher;
this.cache = cache;
this.stats = stats;
this.action = action;
this.key = action.getKey();
this.data = action.getRequest();
this.priority = action.getPriority();
this.memoryPolicy = action.getMemoryPolicy();
this.networkPolicy = action.getNetworkPolicy();
this.requestHandler = requestHandler;
this.retryCount = requestHandler.getRetryCount();
}
4.2.4.2 添加到線程池會(huì)執(zhí)行BitmapHunter的 run()方法
@Override public void run() {
try {
...
// 4.2.4.2.1
result = hunt();
if (result == null) {
dispatcher.dispatchFailed(this);
} else {
// 4.2.4.2.2
dispatcher.dispatchComplete(this);
}
...
}
4.2.4.2.1 hunt方法解析
Bitmap hunt() throws IOException {
Bitmap bitmap = null;
// 從內(nèi)存緩存中獲取踪栋,第一次未緩存bitmap為null
if (shouldReadFromMemoryCache(memoryPolicy)) {
bitmap = cache.get(key);
if (bitmap != null) {
...
return bitmap;
}
}
// NetworkRequestHandler設(shè)置的retryCount為2
data.networkPolicy = retryCount == 0 ? NetworkPolicy.OFFLINE.index : networkPolicy;
// 4.2.4.2.1.1
RequestHandler.Result result = requestHandler.load(data, networkPolicy);
if (result != null) {
loadedFrom = result.getLoadedFrom();
exifRotation = result.getExifOrientation();
bitmap = result.getBitmap();
//okhttp下載,bitmap==null,從流中加載生成bitmap
if (bitmap == null) {
InputStream is = result.getStream();
try {
bitmap = decodeStream(is, data);
} finally {
Utils.closeQuietly(is);
}
}
}
if (bitmap != null) {
...
// RequestCreator并未設(shè)置平移或者旋轉(zhuǎn)图毕,此為false
if (data.needsTransformation() || exifRotation != 0) {
synchronized (DECODE_LOCK) {
if (data.needsMatrixTransform() || exifRotation != 0) {
bitmap = transformResult(data, bitmap, exifRotation);
...
}
if (data.hasCustomTransformations()) {
bitmap = applyCustomTransformations(data.transformations, bitmap);
...
}
}
...
}
}
return bitmap;
}
4.2.4.2.1.1 load方法解析
@Override public Result load(Request request, int networkPolicy) throws IOException {
// 利用OkHttpDownloader下載圖片夷都,參看2.1 及OKHttp的使用
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;
}
...
return new Result(is, loadedFrom);
}
4.2.4.2.2 完成Bitmap加載到ImageView
// 參看2.6.1 此handler為DispatcherHandler會(huì)發(fā)送消息到為主線程處理
void dispatchComplete(BitmapHunter hunter) {
handler.sendMessage(handler.obtainMessage(HUNTER_COMPLETE, hunter));
}
case HUNTER_COMPLETE: {
BitmapHunter hunter = (BitmapHunter) msg.obj;
//
dispatcher.performComplete(hunter);
break;
}
void performComplete(BitmapHunter hunter) {
// 根據(jù)MemoryPolicy會(huì)把下載好的圖片放入到內(nèi)存緩存當(dāng)中
if (shouldWriteToMemoryCache(hunter.getMemoryPolicy())) {
cache.set(hunter.getKey(), hunter.getResult());
}
hunterMap.remove(hunter.getKey());
//
batch(hunter);
...
}
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);
}
}
case HUNTER_DELAY_NEXT_BATCH: {
//
dispatcher.performBatchComplete();
break;
}
void performBatchComplete() {
List<BitmapHunter> copy = new ArrayList<BitmapHunter>(batch);
batch.clear();
//
mainThreadHandler.sendMessage(mainThreadHandler.obtainMessage(HUNTER_BATCH_COMPLETE, copy));
logBatch(copy);
}
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;
}
void complete(BitmapHunter hunter) {
Action single = hunter.getAction();
...
if (single != null) {
//
deliverAction(result, from, single);
}
...
}
private void deliverAction(Bitmap result, LoadedFrom from, Action action) {
...
//
action.complete(result, from);
...
}
@Override public void complete(Bitmap result, Picasso.LoadedFrom from) {
...
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);
...
}
static void setBitmap(ImageView target, Context context, Bitmap bitmap,
Picasso.LoadedFrom loadedFrom, boolean noFade, boolean debugging) {
...
PicassoDrawable drawable =
new PicassoDrawable(context, bitmap, placeholder, loadedFrom, noFade, debugging);
// 終于看到了我們熟悉的ImageView.setImageDrawable()方法設(shè)置圖片
target.setImageDrawable(drawable);
}