Class GlideBuilder


  • public final class GlideBuilder
    extends java.lang.Object
    A builder class for setting default structural classes for Glide to use.
    • Constructor Detail

      • GlideBuilder

        public GlideBuilder()
    • Method Detail

      • setBitmapPool

        @NonNull
        public GlideBuilder setBitmapPool​(@Nullable
                                          BitmapPool bitmapPool)
        Sets the BitmapPool implementation to use to store and retrieve reused Bitmaps.
        Parameters:
        bitmapPool - The pool to use.
        Returns:
        This builder.
      • setArrayPool

        @NonNull
        public GlideBuilder setArrayPool​(@Nullable
                                         ArrayPool arrayPool)
        Sets the ArrayPool implementation 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 the MemoryCache implementation to store Resources that are not currently in use.
        Parameters:
        memoryCache - The cache to use.
        Returns:
        This builder.
      • setDefaultTransitionOptions

        @NonNull
        public <T> GlideBuilder setDefaultTransitionOptions​(@NonNull
                                                            java.lang.Class<T> clazz,
                                                            @Nullable
                                                            TransitionOptions<?,​T> options)
        Sets the default 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 BitmapDrawables, the transition you registered for BitmapDrawables will be used.

      • setLogLevel

        @NonNull
        public GlideBuilder setLogLevel​(int logLevel)
        Sets a log level constant from those in 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.

        Parameters:
        logLevel - The log level to use from Log.
        Returns:
        This builder.
      • setIsActiveResourceRetentionAllowed

        @NonNull
        public GlideBuilder setIsActiveResourceRetentionAllowed​(boolean isActiveResourceRetentionAllowed)
        If set to true, allows Glide to re-capture resources that are loaded into Targets 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 the Target. To do so, Glide uses WeakReferences to track resources that belong to Targets 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 Targets 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 Bitmaps 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. Clearing Targets 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 Bitmaps is not only unsafe, it's also totally unnecessary and should be avoided. In all cases, prefer RequestManager.clear(Target) to Bitmap.recycle().

        Returns:
        This builder.
      • setImageDecoderEnabledForBitmaps

        public GlideBuilder setImageDecoderEnabledForBitmaps​(boolean isEnabled)
        Set to true to make Glide use ImageDecoder when decoding Bitmaps 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 Bitmaps. GIFs, resources, and all other types of Drawables 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 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.

      • useLifecycleInsteadOfInjectingFragments

        public GlideBuilder useLifecycleInsteadOfInjectingFragments​(boolean isEnabled)
        When given androidx Fragments and Activities, use Lifecycle to track the Activity or Fragment lifecycle instead of adding custom SupportRequestManagerFragments.

        This flag is experimental and will be removed without notice in a future version.