0
# Glide Request Configuration System
1
2
The Glide request configuration system provides comprehensive control over how images are loaded, cached, transformed, and displayed. At its core is the `RequestOptions` class, which offers an immutable, fluent API for configuring all aspects of image requests.
3
4
## RequestOptions Class
5
6
The `RequestOptions` class is the primary configuration interface for Glide requests. It provides a builder pattern for setting up request parameters and can be applied globally or per-request.
7
8
```java { .api }
9
public class RequestOptions {
10
/**
11
* Creates a new RequestOptions instance
12
*/
13
public static RequestOptions new() {
14
// Implementation creates new instance
15
}
16
17
/**
18
* Creates RequestOptions with placeholder
19
* @param placeholderDrawable The placeholder drawable
20
*/
21
public static RequestOptions placeholderOf(Drawable placeholderDrawable) {
22
// Implementation creates instance with placeholder
23
}
24
25
/**
26
* Creates RequestOptions with error drawable
27
* @param errorDrawable The error drawable
28
*/
29
public static RequestOptions errorOf(Drawable errorDrawable) {
30
// Implementation creates instance with error drawable
31
}
32
33
/**
34
* Creates RequestOptions with format
35
* @param format The decode format preference
36
*/
37
public static RequestOptions formatOf(DecodeFormat format) {
38
// Implementation creates instance with format
39
}
40
41
/**
42
* Creates RequestOptions with priority
43
* @param priority The request priority level
44
*/
45
public static RequestOptions priorityOf(Priority priority) {
46
// Implementation creates instance with priority
47
}
48
49
/**
50
* Creates RequestOptions with disk cache strategy
51
* @param strategy The caching strategy
52
*/
53
public static RequestOptions diskCacheStrategyOf(DiskCacheStrategy strategy) {
54
// Implementation creates instance with strategy
55
}
56
57
/**
58
* Creates RequestOptions with size override
59
* @param width Target width in pixels
60
* @param height Target height in pixels
61
*/
62
public static RequestOptions overrideOf(int width, int height) {
63
// Implementation creates instance with size override
64
}
65
}
66
```
67
68
## Core Configuration Methods
69
70
### Placeholder and Error Handling
71
72
Control what displays during loading states and error conditions:
73
74
```java { .api }
75
/**
76
* Sets the placeholder drawable to display while loading
77
* @param placeholderDrawable Drawable to show during loading
78
*/
79
public RequestOptions placeholder(Drawable placeholderDrawable);
80
81
/**
82
* Sets placeholder using resource ID
83
* @param placeholderResourceId Resource ID for placeholder
84
*/
85
public RequestOptions placeholder(@DrawableRes int placeholderResourceId);
86
87
/**
88
* Sets the error drawable for failed loads
89
* @param errorDrawable Drawable to show on error
90
*/
91
public RequestOptions error(Drawable errorDrawable);
92
93
/**
94
* Sets error drawable using resource ID
95
* @param errorResourceId Resource ID for error drawable
96
*/
97
public RequestOptions error(@DrawableRes int errorResourceId);
98
99
/**
100
* Sets fallback drawable for null models
101
* @param fallbackDrawable Drawable for null/empty sources
102
*/
103
public RequestOptions fallback(Drawable fallbackDrawable);
104
105
/**
106
* Sets fallback using resource ID
107
* @param fallbackResourceId Resource ID for fallback
108
*/
109
public RequestOptions fallback(@DrawableRes int fallbackResourceId);
110
```
111
112
### Size and Scaling Configuration
113
114
Control image dimensions and scaling behavior:
115
116
```java { .api }
117
/**
118
* Overrides the target size for the request
119
* @param width Target width in pixels
120
* @param height Target height in pixels
121
*/
122
public RequestOptions override(int width, int height);
123
124
/**
125
* Sets a multiplier for the target size
126
* @param sizeMultiplier Multiplier (0.1f to 1.0f)
127
*/
128
public RequestOptions sizeMultiplier(float sizeMultiplier);
129
```
130
131
### Priority and Timeout Settings
132
133
Control request execution priority and timing:
134
135
```java { .api }
136
/**
137
* Sets the priority for this request
138
* @param priority Priority level for execution
139
*/
140
public RequestOptions priority(Priority priority);
141
142
/**
143
* Sets timeout for the request
144
* @param timeoutMs Timeout in milliseconds
145
*/
146
public RequestOptions timeout(int timeoutMs);
147
```
148
149
### Format and Quality Options
150
151
Control image format and compression:
152
153
```java { .api }
154
/**
155
* Sets the decode format preference
156
* @param format Preferred bitmap configuration
157
*/
158
public RequestOptions format(DecodeFormat format);
159
160
/**
161
* Sets the class to decode into
162
* @param resourceClass Target class (Bitmap, Drawable, etc.)
163
*/
164
public <T> RequestOptions decode(Class<T> resourceClass);
165
166
/**
167
* Sets bitmap compress format for encoding
168
* @param format Compression format (JPEG, PNG, WEBP)
169
*/
170
public RequestOptions encodeFormat(Bitmap.CompressFormat format);
171
172
/**
173
* Sets encoding quality (0-100)
174
* @param quality Quality percentage
175
*/
176
public RequestOptions encodeQuality(int quality);
177
```
178
179
### Memory and Disk Cache Controls
180
181
Manage caching behavior:
182
183
```java { .api }
184
/**
185
* Sets disk cache strategy
186
* @param strategy How to cache on disk
187
*/
188
public RequestOptions diskCacheStrategy(DiskCacheStrategy strategy);
189
190
/**
191
* Controls memory cache usage
192
* @param skip True to skip memory cache
193
*/
194
public RequestOptions skipMemoryCache(boolean skip);
195
196
/**
197
* Only load from cache, don't fetch
198
* @param onlyCache True to use cache only
199
*/
200
public RequestOptions onlyRetrieveFromCache(boolean onlyCache);
201
```
202
203
### Video Frame Extraction
204
205
Configure video frame loading:
206
207
```java { .api }
208
/**
209
* Sets the time position for video frame extraction
210
* @param frameTimeMicros Time in microseconds
211
*/
212
public RequestOptions frame(long frameTimeMicros);
213
```
214
215
### Advanced Configuration
216
217
Additional configuration options:
218
219
```java { .api }
220
/**
221
* Controls unlimited source generators pool usage
222
* @param flag True to use unlimited pool
223
*/
224
public RequestOptions useUnlimitedSourceGeneratorsPool(boolean flag);
225
```
226
227
## Enum Types
228
229
### Priority Levels
230
231
```java { .api }
232
public enum Priority {
233
/**
234
* Lowest priority - loaded when system is idle
235
*/
236
LOW,
237
238
/**
239
* Normal priority - default for most requests
240
*/
241
NORMAL,
242
243
/**
244
* High priority - important images
245
*/
246
HIGH,
247
248
/**
249
* Immediate priority - critical images
250
*/
251
IMMEDIATE
252
}
253
```
254
255
### Decode Formats
256
257
```java { .api }
258
public enum DecodeFormat {
259
/**
260
* Prefer ARGB_8888 for quality (default)
261
*/
262
PREFER_ARGB_8888,
263
264
/**
265
* Prefer RGB_565 for memory efficiency
266
*/
267
PREFER_RGB_565,
268
269
/**
270
* Use system default format
271
*/
272
DEFAULT
273
}
274
```
275
276
### Disk Cache Strategies
277
278
```java { .api }
279
public enum DiskCacheStrategy {
280
/**
281
* Cache both original and transformed resources
282
*/
283
ALL,
284
285
/**
286
* Cache nothing to disk
287
*/
288
NONE,
289
290
/**
291
* Cache only original untransformed data
292
*/
293
DATA,
294
295
/**
296
* Cache only transformed resources
297
*/
298
RESOURCE,
299
300
/**
301
* Intelligently cache based on data source (default)
302
*/
303
AUTOMATIC
304
}
305
```
306
307
## Configuration Chaining and Application
308
309
### Creating and Applying Options
310
311
```java
312
// Create base options
313
RequestOptions baseOptions = new RequestOptions()
314
.placeholder(R.drawable.loading)
315
.error(R.drawable.error)
316
.diskCacheStrategy(DiskCacheStrategy.ALL)
317
.priority(Priority.HIGH);
318
319
// Apply to specific request
320
Glide.with(context)
321
.load(imageUrl)
322
.apply(baseOptions)
323
.into(imageView);
324
325
// Chain additional options
326
Glide.with(context)
327
.load(imageUrl)
328
.apply(baseOptions)
329
.override(300, 300)
330
.format(DecodeFormat.PREFER_RGB_565)
331
.into(imageView);
332
```
333
334
### Global Default Options
335
336
```java
337
// Set global defaults in GlideModule
338
public class MyGlideModule extends AppGlideModule {
339
@Override
340
public void applyOptions(Context context, GlideBuilder builder) {
341
RequestOptions defaultOptions = new RequestOptions()
342
.format(DecodeFormat.PREFER_RGB_565)
343
.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
344
.timeout(30000);
345
346
builder.setDefaultRequestOptions(defaultOptions);
347
}
348
}
349
```
350
351
## Practical Usage Examples
352
353
### Performance-Optimized Configuration
354
355
```java
356
// Memory-efficient configuration
357
RequestOptions memoryEfficientOptions = new RequestOptions()
358
.format(DecodeFormat.PREFER_RGB_565) // Use less memory
359
.sizeMultiplier(0.8f) // Scale down slightly
360
.diskCacheStrategy(DiskCacheStrategy.RESOURCE) // Cache processed images
361
.skipMemoryCache(false); // Keep memory caching
362
363
Glide.with(context)
364
.load(imageUrl)
365
.apply(memoryEfficientOptions)
366
.into(imageView);
367
```
368
369
### High-Quality Configuration
370
371
```java
372
// High-quality configuration
373
RequestOptions highQualityOptions = new RequestOptions()
374
.format(DecodeFormat.PREFER_ARGB_8888) // Best quality
375
.encodeFormat(Bitmap.CompressFormat.PNG) // Lossless compression
376
.encodeQuality(100) // Maximum quality
377
.diskCacheStrategy(DiskCacheStrategy.ALL) // Cache everything
378
.priority(Priority.HIGH); // High priority loading
379
380
Glide.with(context)
381
.load(imageUrl)
382
.apply(highQualityOptions)
383
.into(imageView);
384
```
385
386
### Video Thumbnail Configuration
387
388
```java
389
// Extract frame from video
390
RequestOptions videoThumbnailOptions = new RequestOptions()
391
.frame(1000000) // 1 second in microseconds
392
.placeholder(R.drawable.video_placeholder)
393
.error(R.drawable.video_error)
394
.override(200, 150) // Thumbnail size
395
.diskCacheStrategy(DiskCacheStrategy.RESOURCE);
396
397
Glide.with(context)
398
.load(videoUri)
399
.apply(videoThumbnailOptions)
400
.into(thumbnailView);
401
```
402
403
### List/RecyclerView Optimized
404
405
```java
406
// Optimized for list scrolling
407
RequestOptions listItemOptions = new RequestOptions()
408
.placeholder(new ColorDrawable(Color.LTGRAY)) // Lightweight placeholder
409
.format(DecodeFormat.PREFER_RGB_565) // Memory efficient
410
.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC) // Smart caching
411
.priority(Priority.NORMAL) // Standard priority
412
.timeout(10000); // Shorter timeout
413
414
// In adapter
415
Glide.with(holder.itemView.getContext())
416
.load(item.imageUrl)
417
.apply(listItemOptions)
418
.into(holder.imageView);
419
```
420
421
### Cache-First Configuration
422
423
```java
424
// Prefer cached content
425
RequestOptions cacheFirstOptions = new RequestOptions()
426
.onlyRetrieveFromCache(true) // Only use cache
427
.fallback(R.drawable.offline_placeholder) // Show when no cache
428
.skipMemoryCache(false) // Use memory cache
429
.diskCacheStrategy(DiskCacheStrategy.ALL);
430
431
Glide.with(context)
432
.load(imageUrl)
433
.apply(cacheFirstOptions)
434
.into(imageView);
435
```
436
437
### Progressive Loading Configuration
438
439
```java
440
// Progressive loading with thumbnail
441
RequestOptions progressiveOptions = new RequestOptions()
442
.placeholder(R.drawable.loading)
443
.sizeMultiplier(0.1f); // Small thumbnail first
444
445
RequestOptions fullSizeOptions = new RequestOptions()
446
.diskCacheStrategy(DiskCacheStrategy.ALL)
447
.priority(Priority.HIGH);
448
449
Glide.with(context)
450
.load(imageUrl)
451
.thumbnail(
452
Glide.with(context)
453
.load(imageUrl)
454
.apply(progressiveOptions)
455
)
456
.apply(fullSizeOptions)
457
.into(imageView);
458
```
459
460
## Configuration Best Practices
461
462
### Memory Management
463
464
1. **Use RGB_565 for non-transparent images** to save 50% memory
465
2. **Set appropriate size multipliers** for scaled-down contexts
466
3. **Configure memory cache appropriately** for your app's needs
467
4. **Use disk caching strategically** based on content type
468
469
### Performance Optimization
470
471
1. **Set timeouts** to prevent hanging requests
472
2. **Use priority levels** to load critical images first
473
3. **Configure appropriate cache strategies** for different content types
474
4. **Skip memory cache for large images** that won't be reused
475
476
### Quality vs. Size Trade-offs
477
478
1. **Use PREFER_ARGB_8888** only when transparency or quality is critical
479
2. **Adjust encode quality** based on content importance
480
3. **Consider Progressive JPEG** for large images
481
4. **Use appropriate compression formats** (WEBP when possible)
482
483
### Error Handling
484
485
1. **Always provide error drawables** for network-dependent content
486
2. **Use fallback drawables** for null/empty sources
487
3. **Provide meaningful placeholders** that match expected content
488
4. **Consider offline scenarios** with cache-only options
489
490
The RequestOptions system provides fine-grained control over every aspect of image loading in Glide. By understanding these configuration options and applying them appropriately, you can optimize your app's performance, memory usage, and user experience while maintaining high image quality where needed.