Class GlideBuilder
- java.lang.Object
-
- com.bumptech.glide.GlideBuilder
-
public final class GlideBuilder extends java.lang.ObjectA builder class for setting default structural classes for Glide to use.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classGlideBuilder.LogRequestOriginsstatic classGlideBuilder.UseLifecycleInsteadOfInjectingFragmentsUse the androidx lifecycle instead of injecting custom fragments when using androidx fragments and activities.static classGlideBuilder.WaitForFramesAfterTrimMemoryThis is an internal only class that may be deleted at any time without notice.
-
Constructor Summary
Constructors Constructor Description GlideBuilder()
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description GlideBuilderaddGlobalRequestListener(RequestListener<java.lang.Object> listener)Adds a globalRequestListenerthat will be added to every request started with Glide.GlideBuildersetAnimationExecutor(GlideExecutor service)Sets theGlideExecutorto use when loading frames of animated images and particularly ofGifDrawables.GlideBuildersetArrayPool(ArrayPool arrayPool)Sets theArrayPoolimplementation to allow variable sized arrays to be stored and retrieved as needed.GlideBuildersetBitmapPool(BitmapPool bitmapPool)Sets theBitmapPoolimplementation to use to store and retrieve reusedBitmaps.GlideBuildersetConnectivityMonitorFactory(ConnectivityMonitorFactory factory)Sets theConnectivityMonitorFactoryto use to notifyRequestManagerof connectivity events.GlideBuildersetDefaultRequestOptions(Glide.RequestOptionsFactory factory)Sets a factory for the defaultRequestOptionsto use for all loads across the app and returns thisGlideBuilder.GlideBuildersetDefaultRequestOptions(RequestOptions requestOptions)Sets the defaultRequestOptionsto use for all loads across the app.<T> GlideBuildersetDefaultTransitionOptions(java.lang.Class<T> clazz, TransitionOptions<?,T> options)Sets the defaultTransitionOptionsto use when starting a request that will load a resource with the givenClass.GlideBuildersetDiskCache(DiskCache.Factory diskCacheFactory)Sets theDiskCache.Factoryimplementation to use to construct theDiskCacheto use to storeResourcedata on disk.GlideBuildersetDiskCacheExecutor(GlideExecutor service)Sets theGlideExecutorto use when retrievingResources that are currently in Glide's disk caches.GlideBuildersetImageDecoderEnabledForBitmaps(boolean isEnabled)Set totrueto make Glide useImageDecoderwhen decodingBitmaps on Android P and higher.GlideBuildersetIsActiveResourceRetentionAllowed(boolean isActiveResourceRetentionAllowed)If set totrue, allows Glide to re-capture resources that are loaded intoTargets which are subsequently de-referenced and garbage collected without being cleared.GlideBuildersetLogLevel(int logLevel)Sets a log level constant from those inLogto indicate the desired log verbosity.GlideBuildersetLogRequestOrigins(boolean isEnabled)Set totrueto make Glide populateGlideException.setOrigin(Exception)for failed requests.GlideBuildersetMemoryCache(MemoryCache memoryCache)Sets theMemoryCacheimplementation to storeResources that are not currently in use.GlideBuildersetMemorySizeCalculator(MemorySizeCalculator calculator)Sets theMemorySizeCalculatorto use to calculate maximum sizes for defaultMemoryCachesand/or defaultBitmapPools.GlideBuildersetMemorySizeCalculator(MemorySizeCalculator.Builder builder)Sets theMemorySizeCalculatorto use to calculate maximum sizes for defaultMemoryCachesand/or defaultBitmapPools.GlideBuildersetResizeExecutor(GlideExecutor service)Deprecated.GlideBuildersetSourceExecutor(GlideExecutor service)Sets theGlideExecutorto use when retrievingResources that are not already in the cache.GlideBuilderuseLifecycleInsteadOfInjectingFragments(boolean isEnabled)When given androidx Fragments and Activities, useLifecycleto track the Activity or Fragment lifecycle instead of adding customSupportRequestManagerFragments.
-
-
-
Method Detail
-
setBitmapPool
@NonNull public GlideBuilder setBitmapPool(@Nullable BitmapPool bitmapPool)
Sets theBitmapPoolimplementation to use to store and retrieve reusedBitmaps.- Parameters:
bitmapPool- The pool to use.- Returns:
- This builder.
-
setArrayPool
@NonNull public GlideBuilder setArrayPool(@Nullable ArrayPool arrayPool)
Sets theArrayPoolimplementation to allow variable sized arrays to be stored and retrieved as needed.- Parameters:
arrayPool- The pool to use.- Returns:
- This builder.
-
setMemoryCache
@NonNull public GlideBuilder setMemoryCache(@Nullable MemoryCache memoryCache)
Sets theMemoryCacheimplementation to storeResources that are not currently in use.- Parameters:
memoryCache- The cache to use.- Returns:
- This builder.
-
setDiskCache
@NonNull public GlideBuilder setDiskCache(@Nullable DiskCache.Factory diskCacheFactory)
Sets theDiskCache.Factoryimplementation to use to construct theDiskCacheto use to storeResourcedata on disk.- Parameters:
diskCacheFactory- The disk cache factory to use.- Returns:
- This builder.
-
setResizeExecutor
@Deprecated public GlideBuilder setResizeExecutor(@Nullable GlideExecutor service)
Deprecated.Sets theGlideExecutorto use when retrievingResources that are not already in the cache.The thread count defaults to the number of cores available on the device, with a maximum of 4.
Use the
GlideExecutor.newSourceExecutor()methods if you'd like to specify options for the source executor.- Parameters:
service- The ExecutorService to use.- Returns:
- This builder.
- See Also:
setDiskCacheExecutor(GlideExecutor),GlideExecutor
-
setSourceExecutor
@NonNull public GlideBuilder setSourceExecutor(@Nullable GlideExecutor service)
Sets theGlideExecutorto use when retrievingResources that are not already in the cache.The thread count defaults to the number of cores available on the device, with a maximum of 4.
Use the
GlideExecutor.newSourceExecutor()methods if you'd like to specify options for the source executor.- Parameters:
service- The ExecutorService to use.- Returns:
- This builder.
- See Also:
setDiskCacheExecutor(GlideExecutor),GlideExecutor
-
setDiskCacheExecutor
@NonNull public GlideBuilder setDiskCacheExecutor(@Nullable GlideExecutor service)
Sets theGlideExecutorto use when retrievingResources that are currently in Glide's disk caches.Defaults to a single thread which is usually the best combination of memory usage, jank, and performance, even on high end devices.
Use the
GlideExecutor.newDiskCacheExecutor()if you'd like to specify options for the disk cache executor.- Parameters:
service- TheGlideExecutorto use.- Returns:
- This builder.
- See Also:
setSourceExecutor(GlideExecutor),GlideExecutor
-
setAnimationExecutor
@NonNull public GlideBuilder setAnimationExecutor(@Nullable GlideExecutor service)
Sets theGlideExecutorto use when loading frames of animated images and particularly ofGifDrawables.Defaults to one or two threads, depending on the number of cores available.
Use the
GlideExecutor.newAnimationExecutor()methods if you'd like to specify options for the animation executor.- Parameters:
service- TheGlideExecutorto use.- Returns:
- This builder.
-
setDefaultRequestOptions
@NonNull public GlideBuilder setDefaultRequestOptions(@Nullable RequestOptions requestOptions)
Sets the defaultRequestOptionsto use for all loads across the app.Applying additional options with
RequestBuilder.apply(BaseRequestOptions)will override defaults set here.- Parameters:
requestOptions- The options to use by default.- Returns:
- This builder.
- See Also:
setDefaultRequestOptions(RequestOptionsFactory)
-
setDefaultRequestOptions
@NonNull public GlideBuilder setDefaultRequestOptions(@NonNull Glide.RequestOptionsFactory factory)
Sets a factory for the defaultRequestOptionsto use for all loads across the app and returns thisGlideBuilder.This factory will NOT be called once per load. Instead it will be called a handful of times and memoized. It's not safe to assume that this factory will be called again for every new load.
Applying additional options with
RequestBuilder.apply(BaseRequestOptions)will override defaults set here.
-
setDefaultTransitionOptions
@NonNull public <T> GlideBuilder setDefaultTransitionOptions(@NonNull java.lang.Class<T> clazz, @Nullable TransitionOptions<?,T> options)
Sets the defaultTransitionOptionsto use when starting a request that will load a resource with the givenClass.It's preferable but not required for the requested resource class to match the resource class applied here as long as the resource class applied here is assignable from the requested resource class. For example you can set a default transition for
Drawableand that default transition will be used if you subsequently start requests for specificDrawabletypes likeGifDrawableorBitmapDrawable. Specific types are always preferred so if you register a default transition for bothDrawableandBitmapDrawableand then start a request forBitmapDrawables, the transition you registered forBitmapDrawables will be used.
-
setMemorySizeCalculator
@NonNull public GlideBuilder setMemorySizeCalculator(@NonNull MemorySizeCalculator.Builder builder)
Sets theMemorySizeCalculatorto use to calculate maximum sizes for defaultMemoryCachesand/or defaultBitmapPools.- Parameters:
builder- The builder to use (will not be modified).- Returns:
- This builder.
- See Also:
setMemorySizeCalculator(MemorySizeCalculator)
-
setMemorySizeCalculator
@NonNull public GlideBuilder setMemorySizeCalculator(@Nullable MemorySizeCalculator calculator)
Sets theMemorySizeCalculatorto use to calculate maximum sizes for defaultMemoryCachesand/or defaultBitmapPools.The given
MemorySizeCalculatorwill not affect custom pools or caches provided viasetBitmapPool(BitmapPool)orsetMemoryCache(MemoryCache).- Parameters:
calculator- The calculator to use.- Returns:
- This builder.
-
setConnectivityMonitorFactory
@NonNull public GlideBuilder setConnectivityMonitorFactory(@Nullable ConnectivityMonitorFactory factory)
Sets theConnectivityMonitorFactoryto use to notifyRequestManagerof connectivity events. If not setDefaultConnectivityMonitorFactorywould be used.- Parameters:
factory- The factory to use- Returns:
- This builder.
-
setLogLevel
@NonNull public GlideBuilder setLogLevel(int logLevel)
Sets a log level constant from those inLogto indicate the desired log verbosity.The level must be one of
Log.VERBOSE,Log.DEBUG,Log.INFO,Log.WARN, orLog.ERROR.Log.VERBOSEmeans one or more lines will be logged per request, including timing logs and failures.Log.DEBUGmeans at most one line will be logged per successful request, including timing logs, although many lines may be logged for failures including multiple complete stack traces.Log.INFOmeans failed loads will be logged including multiple complete stack traces, but successful loads will not be logged at all.Log.WARNmeans only summaries of failed loads will be logged.Log.ERRORmeans only exceptional cases will be logged.All logs will be logged using the 'Glide' tag.
Many other debugging logs are available in individual classes. The log level supplied here only controls a small set of informative and well formatted logs. Users wishing to debug certain aspects of the library can look for individual
TAGvariables at the tops of classes and useadb shell setprop log.tag.TAGto enable or disable any relevant tags.- Parameters:
logLevel- The log level to use fromLog.- Returns:
- This builder.
-
setIsActiveResourceRetentionAllowed
@NonNull public GlideBuilder setIsActiveResourceRetentionAllowed(boolean isActiveResourceRetentionAllowed)
If set totrue, allows Glide to re-capture resources that are loaded intoTargets which are subsequently de-referenced and garbage collected without being cleared.Defaults to
false.Glide's resource re-use system is permissive, which means that's acceptable for callers to load resources into
Targets and then never clear theTarget. To do so, Glide usesWeakReferences to track resources that belong toTargets that haven't yet been cleared. Setting this method totrueallows Glide to also maintain a hard reference to the underlying resource so that if theTargetis garbage collected, Glide can return the underlying resource to it's memory cache so that subsequent requests will not unexpectedly re-load the resource from disk or source. As a side affect, it will take the system slightly longer to garbage collect the underlying resource because the weak reference has to be cleared and processed before the hard reference is removed. As a result, setting this method totruemay transiently increase the memory usage of an application.Leaving this method at the default
falsevalue will allow the platform to garbage collect resources more quickly, but will lead to unexpected memory cache misses if callers load resources intoTargets but never clear them.If you set this method to
trueyou must not callBitmap.recycle()or mutate any Bitmaps returned by Glide. If this method is set tofalse, recycling or mutating Bitmaps is inefficient but safe as long as you do not clear the correspondingTargetused to load theBitmap. However, if you set this method totrueand recycle or mutate any returnedBitmaps or other mutable resources, Glide may recover those resources and attempt to use them later on, resulting in crashes, graphical corruption or undefined behavior.Regardless of what value this method is set to, it's always good practice to clear
Targets when you're done with the corresponding resource. ClearingTargets allows Glide to maximize resource re-use, minimize memory overhead and minimize unexpected behavior resulting from edge cases. If you useRequestManager.clear(Target), callingBitmap.recycle()or mutatingBitmaps is not only unsafe, it's also totally unnecessary and should be avoided. In all cases, preferRequestManager.clear(Target)toBitmap.recycle().- Returns:
- This builder.
-
addGlobalRequestListener
@NonNull public GlideBuilder addGlobalRequestListener(@NonNull RequestListener<java.lang.Object> listener)
Adds a globalRequestListenerthat will be added to every request started with Glide.Multiple
RequestListeners can be added here, inRequestManagerscopes or to individualRequestBuilders.RequestListeners are called in the order they're added. Even if an earlierRequestListenerreturnstruefromRequestListener.onLoadFailed(GlideException, Object, Target, boolean)orRequestListener.onResourceReady(Object, Object, Target, DataSource, boolean), it will not prevent subsequentRequestListeners from being called.Because Glide requests can be started for any number of individual resource types, any listener added here has to accept any generic resource type in
RequestListener.onResourceReady(Object, Object, Target, DataSource, boolean). If you must base the behavior of the listener on the resource type, you will need to useinstanceofto do so. It's not safe to cast resource types without first checking withinstanceof.
-
setLogRequestOrigins
public GlideBuilder setLogRequestOrigins(boolean isEnabled)
Set totrueto make Glide populateGlideException.setOrigin(Exception)for failed requests.The exception set by this method is not printed by
GlideExceptionand can only be viewed via aRequestListenerthat reads the field viaGlideException.getOrigin().This is an experimental API that may be removed in the future.
-
setImageDecoderEnabledForBitmaps
public GlideBuilder setImageDecoderEnabledForBitmaps(boolean isEnabled)
Set totrueto make Glide useImageDecoderwhen decodingBitmaps on Android P and higher.Calls to this method on versions of Android less than Q are ignored. Although ImageDecoder was added in Android O a bug prevents it from scaling images with exif orientations until Q. See b/136096254.
Specifically
ImageDecoderwill be used in place ofDownsamplerandBitmapFactoryto decodeBitmaps. GIFs, resources, and all other types ofDrawables are not affected by this flag.This flag is experimental and may be removed without deprecation in a future version.
When this flag is enabled, Bitmap's will not be re-used when decoding images, though they may still be used as part of
Transformations becauseImageDecoderdoes not support Bitmap re-use.When this flag is enabled
Downsampler.FIX_BITMAP_SIZE_TO_REQUESTED_DIMENSIONSis ignored. All otherDownsamplerflags are obeyed, although there may be subtle behavior differences because many options are subject to the whims ofBitmapFactoryandImageDecoderwhich may not agree.
-
useLifecycleInsteadOfInjectingFragments
public GlideBuilder useLifecycleInsteadOfInjectingFragments(boolean isEnabled)
When given androidx Fragments and Activities, useLifecycleto track the Activity or Fragment lifecycle instead of adding customSupportRequestManagerFragments.This flag is experimental and will be removed without notice in a future version.
-
-