Glide Deep Learning

7380 단어
Note: This article is only for your own learning records, mainly to help your own memory, not for others to learn from. If you want to understand Glide in depth, please move to Guo Lin's Glide series of articles.
Glide.with(this).load("").into(xxx);
with: Get requestManager.

Divided into application and non-application, the former returns the requestManager bound to the application, and the latter adds a hidden fragment to the current activity (glide cannot get the activity life cycle, so starting from the fragment, their cycles are synchronized, and the activity is destroyed. After that, the fragment can be monitored, so that glide can capture it and stop loading the picture)

load: Returns DrawableTypeRequest.

return (DrawableTypeRequest) fromString().load(string); loadGeneric in fromString, create a new DrawableTypeRequest, and pass the parameters in the past, so that the load method is called after the DrawableTypeRequest is obtained. The load method is in the parent class GenericRequestBuilder, which contains a large number of glide apis, The builder pattern builds the api. (DrawableTypeRequest is a subclass of GenericRequestBuilder)

into: Make a network request and transcode

into(glide.buildImageViewTarget(view, transcodeClass)); In parentheses, the target view obtained is roughly divided into two types: static image and dynamic image, and then enters the into method. In the into method, the transformation is performed without looking, and the request object is obtained to run and then begin, call the load in the onSizeReady method, and enter the EngineRunnable thread to view decode(), which is divided into decodeFromCache and decodeFromSource, without the cache, enter the decodeFromSource() of DecodeJob, fetcher.loadData() calls the network request in HttpUrlFetcher, and returns It is InputStream, and finally returns to decodeFromSourceData in Decodejob and then transcodes to return Resource, EngineRunnable executes onLoadComplete->onResourceReady, and static images call the onResourceReady method in ImageViewTarget to setResource (setResource in BitmapImageViewTarget)

Cache mechanism:

Process: First read from the memory cache, if not, execute the decode of the run method in EngineRunnable, which is divided into decodeFromCache() and decodeFromSource(), decodeFromCache is divided into disk cache read (result), disk original cache image (source) , if the disk cache is not used, decodeFromSource reads the image from the network, and then decodeFromSourceData writes it to the original disk cache. After the end, transformEncodeAndTranscode(decoded) caches the transcoded image, and finally calls back onLoadComplete after the image is loaded to execute the memory cache write. enter. There are many key generation conditions, id, width, height, etc. have an impact.

Memory cache:

Read: loadFromCache (LruCache) will be obtained from memory before obtaining network pictures. If it is empty, continue down loadFromActiveResources (weak reference), otherwise directly onResourceReady (two kinds of memory caches are used together to prevent pictures in use in Lruche from being recycled. ) Write: (weak reference) After the image is loaded, onLoadComplete sends a message that the handler is MSG_COMPLETE to indicate that the loading is complete, then handleResultOnMainThread is executed, and the activeResources.put method is executed in onEngineJobComplete
(LruCache) engineResource.acquire() times +1, release-1, when the acquired variable is greater than 0, it means that the image is in use, and it should be placed in the activeResources weak reference cache. After release(), if the acquired variable is equal to 0, indicating that the picture is no longer used, then the listener's onResourceReleased() method will be called to release the resource activeResources.remove(cacheKey) to remove it from the weak reference, Then cache.put(cacheKey, resource) is added to LruCache, which realizes the function of using weak reference to cache images in use, and using LruCache to cache images not in use.

Transformations: https://github.com/wasabeef/glide-transformations
Customization: 1. Configuration (meta configuration in AndroidManifest, initialization information will be configured before glide is created)
    public class MyGlideModule implements GlideModule {
    @Override
    public void applyOptions(Context context, GlideBuilder builder) {
        builder.setDiskCache(new ExternalCacheDiskCacheFactory(context));
        builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888);
    }
    @Override
    public void registerComponents(Context context, Glide glide) {
    }
}
2. Component okhttp: HttpUrlFetcher is replaced by OkHttpFetcher
 public class MyGlideModule implements GlideModule {
    ...
    @Override
    public void registerComponents(Context context, Glide glide) {
        glide.register(GlideUrl.class, InputStream.class, new OkHttpGlideUrlLoader.Factory());
    }
}
3. Progress bar
public class MyGlideModule implements GlideModule { 
    @Override 
    public void applyOptions(Context context, GlideBuilder builder) { 
    } 

    @Override 
    public void registerComponents(Context context, Glide glide) { 
        OkHttpClient.Builder builder = new OkHttpClient.Builder(); 
        builder.addInterceptor(new ProgressInterceptor()); 
        OkHttpClient okHttpClient = builder.build(); 
        glide.register(GlideUrl.class, InputStream.class, new OkHttpGlideUrlLoader.Factory(okHttpClient));
    } 
}

좋은 웹페이지 즐겨찾기