Mobile Development 43 min read

Comprehensive Guide to Glide Image Loading Process and Configuration in Android

This article provides an in‑depth overview of Glide 4.11, covering its basic usage, the four‑step request lifecycle, six‑step image loading flow, caching mechanisms, initialization, configuration options, and common interview‑style questions for Android developers.

Sohu Tech Products
Sohu Tech Products
Sohu Tech Products
Comprehensive Guide to Glide Image Loading Process and Configuration in Android

This article explains how to use Glide (version 4.11) for image loading in Android applications.

Preface

1. Glide Basic Usage

Glide is added as a dependency and three core methods— with() , load() , and into() —are used to start a request.

2. Content Overview

Image loading can be divided into three high‑level steps: request initiation, decoding, and displaying the image.

1. Four‑step Decode Task Startup

This section explains what happens from calling into() to the run() method of DecodeJob , covering Request, Target, Engine, and DecodeJob objects.

2. Six‑step Image Loading

The six steps include model acquisition, raw data retrieval, resource decoding, transformation, transcoding, and finally delivering the result to the target.

3. Glide Cache Principles

Glide uses a three‑level cache (memory, disk, source) to reduce network traffic and memory usage.

4. Glide Initialization and Configuration

Glide is initialized via Glide.with() , which creates a RequestManager and sets up the GlideBuilder . Configuration can be customized in an AppGlideModule by modifying the Registry, thread pools, and memory calculators.

5. Glide Image Loading Options

Glide provides many request options such as placeholder() , error() , fallback() , override() , fitCenter() , centerCrop() , centerInside() , transform() , skipMemoryCache() , diskCacheStrategy() , priority() , sizeMultiplier() , encodeFormat() , encodeQuality() , frame() , format() , timeout() , transition() , dontAnimate() , apply() , listener() , asBitmap() , asFile() , submit() , downloadOnly() , preload() , etc.

1. Four‑step Decode Task Startup

The process involves Request, Target, Engine, and DecodeJob objects.

1.1 Request

1.1.1 RequestBuilder

1. into()

Calling into() creates a Request and passes it to the request manager.

2. Model

The image source passed to load() is wrapped as a Model object.

3. Request Options

Options such as centerCrop() are defined in BaseRequestOptions .

1.1.2 RequestManager

RequestManager binds the request to a lifecycle, monitors network state, creates request builders, and starts requests.

1. Bind Lifecycle

Each Context creates a RequestManagerFragment to receive lifecycle callbacks.

2. Network State Listener

A RequestManagerConnectivityListener restarts requests when connectivity changes.

3. Create Request Builder

load() creates a RequestBuilder for various data types (Bitmap, Drawable, String, Uri, URL, File, resourceId, byte[], Object).

4. Start Request

The manager tracks targets and runs the request via RequestTracker and TargetTracker .

1.1.3 Request States

SingleRequest has six states: PENDING, CLEARED, WAITING_FOR_SIZE, RUNNING, COMPLETE, FAILED.

1.1.4 Placeholder Images

Glide supports three placeholders: placeholder , error , and fallback .

1.1.5 Request‑Related Questions

Which class provides the into() method?

Who restarts requests on network changes?

What are the request states and how do they transition?

What placeholder types exist and when are they shown?

1.2 Target

Targets represent the destination view (e.g., ImageView) and have several implementations.

1.2.1 ImageViewTarget

1. SizeDeterminer

Calculates the request size by removing view padding.

2. OnPreDrawListener

Used when size cannot be obtained immediately.

3. setResource()

BitmapImageViewTarget uses setImageBitmap() ; DrawableImageViewTarget uses setImageDrawable() .

1.2.2 RequestFutureTarget

Used with submit() to obtain the loaded resource on a background thread.

1.2.3 CustomTarget

Handles non‑view targets such as preloads, app widgets, GIFs, and notifications.

1.2.4 Target‑Related Questions

How does ImageViewTarget obtain request dimensions?

Why must submit() be called off the UI thread?

Which target is used for preloading and what does it do after receiving the resource?

1.3 Engine

The Engine coordinates loading, caching, and decoding.

1.3.1 Engine.load()

Creates an EngineKey , checks memory cache, and if missing starts a new EngineJob and DecodeJob .

1.3.2 Key

Key objects uniquely identify cached resources; implementations include DataCacheKey , ResourceCacheKey , AndroidResourceSignature , ObjectKey , and EngineKey .

1.3.3 Resource

Resources wrap loaded data; BitmapResource uses a BitmapPool for reuse.

1.3.4 Engine‑Related Questions

What does Engine.load() do first?

Why must Key override equals() and hashCode() ?

Which class recycles resources?

Which Key is used for Drawable resources?

1.4 DecodeJob

DecodeJob runs on a background thread to fetch data, decode it, apply transformations, and hand it to the Engine.

1.4.1 runWrapped()

Executes based on three run reasons: INITIALIZE, SWITCH_TO_SOURCE_SERVICE, and DECODE_DATA.

1.4.2 Decode Stages

Stages include INITIALIZE, RESOURCE_CACHE, DATA_CACHE, SOURCE, ENCODE, and FINISH.

1.4.3 DataFetcherGenerator Implementations

SourceGenerator, DataCacheGenerator, and ResourceCacheGenerator retrieve data from various sources.

1.4.4 DecodeJob‑Related Questions

What run reasons does DecodeJob use?

What are the stages of data retrieval?

Which classes implement DataFetcherGenerator?

How many encoding strategies does Glide have?

2. Six‑step Image Loading

The six concepts are Model, Data, Resource, TransformedResource, TranscodedResource, and Target.

2.1 ModelLoader

Creates LoadData objects that contain a Model key and a DataFetcher.

2.2 ResourceDecoder

Decodes raw data (e.g., InputStream) into a Resource such as a Bitmap.

2.3 Transformation

Applies visual changes like centerCrop() using a Matrix and Canvas.drawBitmap() .

2.4 ResourceTranscoder

Converts a Resource from one type to another (e.g., Bitmap to BitmapDrawable).

2.5 ResourceEncoder

Encodes the final resource for disk storage (e.g., StreamEncoder ).

2.6 Image Loading Questions

What are the steps of image loading?

What generic parameters does ModelLoader have?

Which class ultimately performs decoding?

Which class performs the actual transformation?

How does Glide encode image data?

3. Glide Cache Principles

Glide employs memory, disk, and source caches to improve performance and reduce bandwidth.

3.1 Memory Cache

Implemented by LruResourceCache , which uses an LRU LinkedHashMap .

3.2 Disk Cache

Implemented by DiskLruCacheWrapper and DiskCacheAdapter . Disk cache size defaults to 250 MB.

3.2.1 DiskLruCache

Stores cached files in Entry objects; writes are performed via an Editor and Writer .

3.2.2 DiskCache.Factory

Provides internal and external cache factories.

3.3 DiskCacheStrategy

Five strategies (AUTOMATIC, ALL, NONE, RESOURCE, DATA) control what is cached.

3.4 BitmapPool

Reuses Bitmaps to reduce memory churn; implemented by LruBitmapPool using LruPoolStrategy .

3.5 ArrayPool

Reuses byte arrays for stream encoding.

3.6 Cache‑Related Questions

What are the two special aspects of LruCache?

What are three characteristics of DiskLruCache?

How many DiskCacheStrategy types exist?

4. Glide Initialization and Configuration

4.1 Initialization Flow

Calling Glide.with() obtains a RequestManager , which may trigger initializeGlide() to create the Glide instance.

4.1.1 with()

Creates a RequestManagerRetriever and a Glide singleton if needed.

4.1.2 initializeGlide()

Applies options from AppGlideModule , creates the instance, registers components, and adds component callbacks.

4.2 Registry

Holds ModelLoaderRegistry, EncoderRegistry, ResourceDecoderRegistry, ResourceEncoderRegistry, DataRewinderRegistry, and ImageHeaderRegistry.

4.3 GlideBuilder

Configurable fields include thread pools, memory calculators, connectivity monitor, request options, bitmap pool, array pool, memory cache, and disk cache factory.

4.3.1 Thread Pools

Four pools: SourceExecutor, unlimitedSourceExecutor, DiskCacheExecutor, AnimationExecutor. Custom pools can be built via GlideExecutor.Builder .

4.3.2 MemorySizeCalculator

Calculates sizes for BitmapPool, ArrayPool, and MemoryCache based on screen size and a multiplier (default 0.4).

4.4 Initialization Questions

What is a Registry?

Where can you replace Glide’s data‑loading logic?

Where can you modify Glide’s memory‑size calculation?

How many thread pools does Glide have?

How are BitmapPool and MemoryCache sizes computed?

5. Glide Image Loading Options

Options include placeholder, error, fallback, override, fitCenter, centerCrop, centerInside, transform, dontTransform, skipMemoryCache, diskCacheStrategy, onlyRetrieveFromCache, priority, sizeMultiplier, encodeFormat, encodeQuality, frame, format, timeout, transition, dontAnimate, apply, listener, asBitmap, asFile, submit, downloadOnly, download, and preload.

References

10大开源框架源码解析

Glide最新版V4使用指南

Android图片加载框架最全解析(八),带你全面了解Glide 4的用法

HashMap的key是对象

performanceAndroidcachingGlideImage Loading
Sohu Tech Products
Written by

Sohu Tech Products

A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.