public final class GlideBuilder
extends java.lang.Object
Constructor and Description |
---|
GlideBuilder() |
Modifier and Type | Method and Description |
---|---|
GlideBuilder |
addGlobalRequestListener(RequestListener<java.lang.Object> listener)
Adds a global
RequestListener that will be added to every request started with Glide. |
GlideBuilder |
setAnimationExecutor(GlideExecutor service)
Sets the
GlideExecutor to use when loading frames of animated images and particularly
of GifDrawable s. |
GlideBuilder |
setArrayPool(ArrayPool arrayPool)
Sets the
ArrayPool implementation to allow variable sized arrays to be stored and
retrieved as needed. |
GlideBuilder |
setBitmapPool(BitmapPool bitmapPool)
Sets the
BitmapPool implementation to use
to store and retrieve reused Bitmap s. |
GlideBuilder |
setConnectivityMonitorFactory(ConnectivityMonitorFactory factory)
Sets the
ConnectivityMonitorFactory to use to notify RequestManager of connectivity events. |
GlideBuilder |
setDefaultRequestOptions(Glide.RequestOptionsFactory factory)
Sets a factory for the default
RequestOptions to use for all loads across the app and
returns this GlideBuilder . |
GlideBuilder |
setDefaultRequestOptions(RequestOptions requestOptions)
Sets the default
RequestOptions to use for all loads across the app. |
<T> GlideBuilder |
setDefaultTransitionOptions(java.lang.Class<T> clazz,
TransitionOptions<?,T> options)
Sets the default
TransitionOptions to use when starting a request that will load a
resource with the given Class . |
GlideBuilder |
setDiskCache(DiskCache.Factory diskCacheFactory)
Sets the
DiskCache.Factory implementation to use
to construct the DiskCache to use to store Resource data on disk. |
GlideBuilder |
setDiskCacheExecutor(GlideExecutor service)
Sets the
GlideExecutor to use when retrieving Resource s that are currently in Glide's disk caches. |
GlideBuilder |
setImageDecoderEnabledForBitmaps(boolean isEnabled)
Set to
true to make Glide use ImageDecoder when decoding
Bitmap s on Android P and higher. |
GlideBuilder |
setIsActiveResourceRetentionAllowed(boolean isActiveResourceRetentionAllowed)
If set to
true , allows Glide to re-capture resources that are loaded into Target s which are subsequently de-referenced and garbage
collected without being cleared. |
GlideBuilder |
setLogLevel(int logLevel)
Sets a log level constant from those in
Log to indicate the desired log verbosity. |
GlideBuilder |
setLogRequestOrigins(boolean isEnabled)
Set to
true to make Glide populate GlideException.setOrigin(Exception) for failed requests. |
GlideBuilder |
setMemoryCache(MemoryCache memoryCache)
Sets the
MemoryCache implementation to store
Resource s that are not currently in use. |
GlideBuilder |
setMemorySizeCalculator(MemorySizeCalculator.Builder builder)
Sets the
MemorySizeCalculator to use to calculate maximum sizes for default MemoryCaches and/or default BitmapPools . |
GlideBuilder |
setMemorySizeCalculator(MemorySizeCalculator calculator)
Sets the
MemorySizeCalculator to use to calculate maximum sizes for default MemoryCaches and/or default BitmapPools . |
GlideBuilder |
setResizeExecutor(GlideExecutor service)
Deprecated.
|
GlideBuilder |
setSourceExecutor(GlideExecutor service)
Sets the
GlideExecutor to use when retrieving Resource s that are not already in the cache. |
@NonNull public GlideBuilder setBitmapPool(@Nullable BitmapPool bitmapPool)
BitmapPool
implementation to use
to store and retrieve reused Bitmap
s.bitmapPool
- The pool to use.@NonNull public GlideBuilder setArrayPool(@Nullable ArrayPool arrayPool)
ArrayPool
implementation to allow variable sized arrays to be stored and
retrieved as needed.arrayPool
- The pool to use.@NonNull public GlideBuilder setMemoryCache(@Nullable MemoryCache memoryCache)
MemoryCache
implementation to store
Resource
s that are not currently in use.memoryCache
- The cache to use.@NonNull public GlideBuilder setDiskCache(@Nullable DiskCache.Factory diskCacheFactory)
DiskCache.Factory
implementation to use
to construct the DiskCache
to use to store Resource
data on disk.diskCacheFactory
- The disk cache factory to use.@Deprecated public GlideBuilder setResizeExecutor(@Nullable GlideExecutor service)
setSourceExecutor(GlideExecutor)
GlideExecutor
to use when retrieving Resource
s 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.
service
- The ExecutorService to use.setDiskCacheExecutor(GlideExecutor)
,
GlideExecutor
@NonNull public GlideBuilder setSourceExecutor(@Nullable GlideExecutor service)
GlideExecutor
to use when retrieving Resource
s 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.
service
- The ExecutorService to use.setDiskCacheExecutor(GlideExecutor)
,
GlideExecutor
@NonNull public GlideBuilder setDiskCacheExecutor(@Nullable GlideExecutor service)
GlideExecutor
to use when retrieving Resource
s 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.
service
- The GlideExecutor
to use.setSourceExecutor(GlideExecutor)
,
GlideExecutor
@NonNull public GlideBuilder setAnimationExecutor(@Nullable GlideExecutor service)
GlideExecutor
to use when loading frames of animated images and particularly
of GifDrawable
s.
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.
service
- The GlideExecutor
to use.@NonNull public GlideBuilder setDefaultRequestOptions(@Nullable RequestOptions requestOptions)
RequestOptions
to use for all loads across the app.
Applying additional options with RequestBuilder.apply(BaseRequestOptions)
will
override defaults set here.
requestOptions
- The options to use by default.#setDefaultRequestOptions(RequestOptionsFactory)
@NonNull public GlideBuilder setDefaultRequestOptions(@NonNull Glide.RequestOptionsFactory factory)
RequestOptions
to use for all loads across the app and
returns this GlideBuilder
.
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.
#setDefaultRequestOptions(RequestOptionsFactory)
@NonNull public <T> GlideBuilder setDefaultTransitionOptions(@NonNull java.lang.Class<T> clazz, @Nullable TransitionOptions<?,T> options)
TransitionOptions
to use when starting a request that will load a
resource with the given Class
.
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 Drawable
and that default transition will be used if you
subsequently start requests for specific Drawable
types like
GifDrawable
or BitmapDrawable
. Specific types are always preferred so if you
register a default transition for both Drawable
and BitmapDrawable
and then start a request for BitmapDrawable
s, the transition you registered for BitmapDrawable
s will be used.
@NonNull public GlideBuilder setMemorySizeCalculator(@NonNull MemorySizeCalculator.Builder builder)
MemorySizeCalculator
to use to calculate maximum sizes for default MemoryCaches
and/or default BitmapPools
.builder
- The builder to use (will not be modified).setMemorySizeCalculator(MemorySizeCalculator)
@NonNull public GlideBuilder setMemorySizeCalculator(@Nullable MemorySizeCalculator calculator)
MemorySizeCalculator
to use to calculate maximum sizes for default MemoryCaches
and/or default BitmapPools
.
The given MemorySizeCalculator
will not affect custom pools or caches provided via
setBitmapPool(BitmapPool)
or setMemoryCache(MemoryCache)
.
calculator
- The calculator to use.@NonNull public GlideBuilder setConnectivityMonitorFactory(@Nullable ConnectivityMonitorFactory factory)
ConnectivityMonitorFactory
to use to notify RequestManager
of connectivity events. If not set DefaultConnectivityMonitorFactory
would be used.factory
- The factory to use@NonNull public GlideBuilder setLogLevel(int logLevel)
Log
to indicate the desired log verbosity.
The level must be one of Log.VERBOSE
, Log.DEBUG
, Log.INFO
, Log.WARN
, or Log.ERROR
.
Log.VERBOSE
means one or more lines will be logged per request, including timing
logs and failures. Log.DEBUG
means 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.INFO
means failed loads will be logged including
multiple complete stack traces, but successful loads will not be logged at all. Log.WARN
means only summaries of failed loads will be logged. Log.ERROR
means 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 TAG
variables at the tops
of classes and use adb shell setprop log.tag.TAG
to enable or disable any relevant
tags.
logLevel
- The log level to use from Log
.@NonNull public GlideBuilder setIsActiveResourceRetentionAllowed(boolean isActiveResourceRetentionAllowed)
true
, allows Glide to re-capture resources that are loaded into Target
s 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 Target
s and then never clear the
Target
. To do so, Glide uses WeakReference
s to track resources that belong to Target
s that haven't yet been cleared. Setting this method
to true
allows Glide to also maintain a hard reference to the underlying resource so
that if the Target
is 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 to true
may transiently increase the memory usage of an application.
Leaving this method at the default false
value will allow the platform to garbage
collect resources more quickly, but will lead to unexpected memory cache misses if callers load
resources into Target
s but never clear them.
If you set this method to true
you must not call Bitmap.recycle()
or mutate any Bitmaps returned by Glide. If this method is set to false
, recycling or
mutating Bitmaps is inefficient but safe as long as you do not clear the corresponding Target
used to load the Bitmap
. However, if you set
this method to true
and recycle or mutate any returned Bitmap
s 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 Target
s when you're done with the corresponding resource.
Clearing Target
s allows Glide to maximize resource
re-use, minimize memory overhead and minimize unexpected behavior resulting from edge cases. If
you use RequestManager.clear(Target)
, calling Bitmap.recycle()
or mutating
Bitmap
s is not only unsafe, it's also totally unnecessary and should be avoided. In all
cases, prefer RequestManager.clear(Target)
to Bitmap.recycle()
.
@NonNull public GlideBuilder addGlobalRequestListener(@NonNull RequestListener<java.lang.Object> listener)
RequestListener
that will be added to every request started with Glide.
Multiple RequestListener
s can be added here, in RequestManager
scopes or to
individual RequestBuilder
s. RequestListener
s are called in the order they're
added. Even if an earlier RequestListener
returns true
from RequestListener.onLoadFailed(GlideException, Object, Target, boolean)
or RequestListener.onResourceReady(Object, Object, Target, DataSource, boolean)
, it will not
prevent subsequent RequestListener
s 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 use instanceof
to
do so. It's not safe to cast resource types without first checking with instanceof
.
public GlideBuilder setLogRequestOrigins(boolean isEnabled)
true
to make Glide populate GlideException.setOrigin(Exception)
for failed requests.
The exception set by this method is not printed by GlideException
and can only be
viewed via a RequestListener
that reads the field via GlideException.getOrigin()
.
This is an experimental API that may be removed in the future.
public GlideBuilder setImageDecoderEnabledForBitmaps(boolean isEnabled)
true
to make Glide use ImageDecoder
when decoding
Bitmap
s 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 ImageDecoder
will be used in place of Downsampler
and BitmapFactory
to decode Bitmap
s. GIFs, resources, and all other types of Drawable
s 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 Transformation
s because ImageDecoder
does not support Bitmap re-use.
When this flag is enabled Downsampler.FIX_BITMAP_SIZE_TO_REQUESTED_DIMENSIONS
is
ignored. All other Downsampler
flags are
obeyed, although there may be subtle behavior differences because many options are subject to
the whims of BitmapFactory
and ImageDecoder
which may not agree.