Picasso 源码分析:BitmapHunter

Bitmap猎人

这个名字起哦真是形象:专为搜寻Bitmap的猎人。

Paste_Image.png

BitmapHunter 从哪里来?

还记得action被分发,被执行的地方么?

void performSubmit(Action action, boolean dismissFailed) {
------
        hunter = forRequest(action.getPicasso(), this, cache, stats, action);// create
        hunter.future = service.submit(hunter);// 执行
        hunterMap.put(action.getKey(), hunter);// 该搜寻任务,记录在案!
-----
}

hunter的构造:

Picasso 源码分析:BitmapHunter_第1张图片
Paste_Image.png

只要Picasso中有一个RequestHandler能处理 该request的,就利用该handler来构建 BitmapHunter!

Picasso提供的RequestHandler:

Picasso 源码分析:BitmapHunter_第2张图片
Paste_Image.png

RequestHandler这个抽象类

Picasso 源码分析:BitmapHunter_第3张图片
Paste_Image.png

它的一些实现类:

Picasso 源码分析:BitmapHunter_第4张图片
Paste_Image.png

一些RequestHandler具体的 对请求能否处理的实现:

能否处理这个请求:注意根据请求的uri所采用的协议来实现的。

  1. NetworkRequestHandler
@Override public boolean canHandleRequest(Request data) {
    String scheme = data.uri.getScheme();
    return (SCHEME_HTTP.equals(scheme) || SCHEME_HTTPS.equals(scheme));
  }
  1. FileRequestHandler
  @Override public boolean canHandleRequest(Request data) {
    return SCHEME_FILE.equals(data.uri.getScheme());
  }
  1. AssetRequestHandler
@Override public boolean canHandleRequest(Request data) {
    Uri uri = data.uri;
    return (SCHEME_FILE.equals(uri.getScheme())
        && !uri.getPathSegments().isEmpty() && ANDROID_ASSET.equals(uri.getPathSegments().get(0)));
  }

一些RequestHandler具体的 对请求的资源加载的实现:

  1. NetworkRequestHandler
Picasso 源码分析:BitmapHunter_第5张图片
Paste_Image.png
  1. FileRequestHandler
Paste_Image.png
  1. AssetRequestHandler
Picasso 源码分析:BitmapHunter_第6张图片
Paste_Image.png

有意思的是:对网络图片的加载 Picasso抽象了一个Downloader
具体的加载由OkHttp来实现的:

Picasso 源码分析:BitmapHunter_第7张图片
Paste_Image.png

他是一个Runnable

直接 run

Picasso 源码分析:BitmapHunter_第8张图片
Paste_Image.png

hunt():

Bitmap hunt() throws IOException {
        Bitmap bitmap = null;
      
        // 1. load bitmap from memery
        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;
            }
        }

        // 2. load from requestHandler
        data.networkPolicy = retryCount == 0 ? NetworkPolicy.OFFLINE.index : networkPolicy;
        RequestHandler.Result result = requestHandler.load(data, networkPolicy);
        if (result != null) {
            loadedFrom = result.getLoadedFrom();
            exifOrientation = 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 {
                    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() || 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);
                }
            }
        }

        return bitmap;
    }

requestHandler 是什么?

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