Class Glide

  • All Implemented Interfaces:
    android.content.ComponentCallbacks, android.content.ComponentCallbacks2

    public class Glide
    extends java.lang.Object
    implements android.content.ComponentCallbacks2
    A singleton to present a simple static interface for building requests with RequestBuilder and maintaining an Engine, BitmapPool, DiskCache and MemoryCache.
    • Field Summary

      • Fields inherited from interface android.content.ComponentCallbacks2

        TRIM_MEMORY_BACKGROUND, TRIM_MEMORY_COMPLETE, TRIM_MEMORY_MODERATE, TRIM_MEMORY_RUNNING_CRITICAL, TRIM_MEMORY_RUNNING_LOW, TRIM_MEMORY_RUNNING_MODERATE, TRIM_MEMORY_UI_HIDDEN
    • Method Detail

      • getPhotoCacheDir

        @Nullable
        public static java.io.File getPhotoCacheDir​(@NonNull
                                                    android.content.Context context)
        Returns a directory with a default name in the private cache directory of the application to use to store retrieved media and thumbnails.
        Parameters:
        context - A context.
        See Also:
        getPhotoCacheDir(android.content.Context, String)
      • getPhotoCacheDir

        @Nullable
        public static java.io.File getPhotoCacheDir​(@NonNull
                                                    android.content.Context context,
                                                    @NonNull
                                                    java.lang.String cacheName)
        Returns a directory with the given name in the private cache directory of the application to use to store retrieved media and thumbnails.
        Parameters:
        context - A context.
        cacheName - The name of the subdirectory in which to store the cache.
        See Also:
        getPhotoCacheDir(android.content.Context)
      • get

        @NonNull
        public static Glide get​(@NonNull
                                android.content.Context context)
        Get the singleton.
        Returns:
        the singleton
      • init

        @Deprecated
        public static void init​(Glide glide)
        Deprecated.
        Use init(Context, GlideBuilder) to get a singleton compatible with Glide's generated API.

        This method will be removed in a future version of Glide.

      • init

        public static void init​(@NonNull
                                android.content.Context context,
                                @NonNull
                                GlideBuilder builder)
      • enableHardwareBitmaps

        public static void enableHardwareBitmaps()
        Allows hardware Bitmaps to be used prior to the first frame in the app being drawn as soon as this method is called.

        If you use this method in non-test code, your app will experience native crashes on some versions of Android if you try to decode a hardware Bitmap. This method is only useful for testing.

      • tearDown

        public static void tearDown()
      • getBitmapPool

        @NonNull
        public BitmapPool getBitmapPool()
        Returns the BitmapPool used to temporarily store Bitmaps so they can be reused to avoid garbage collections.

        Note - Using this pool directly can lead to undefined behavior and strange drawing errors. Any Bitmap added to the pool must not be currently in use in any other part of the application. Any Bitmap added to the pool must be removed from the pool before it is added a second time.

        Note - To make effective use of the pool, any Bitmap removed from the pool must eventually be re-added. Otherwise the pool will eventually empty and will not serve any useful purpose.

        The primary reason this object is exposed is for use in custom ResourceDecoders and Transformations. Use outside of these classes is not generally recommended.

      • getArrayPool

        @NonNull
        public ArrayPool getArrayPool()
      • getContext

        @NonNull
        public android.content.Context getContext()
        Returns:
        The context associated with this instance.
      • preFillBitmapPool

        public void preFillBitmapPool​(@NonNull
                                      PreFillType.Builder... bitmapAttributeBuilders)
        Pre-fills the BitmapPool using the given sizes.

        Enough Bitmaps are added to completely fill the pool, so most or all of the Bitmaps currently in the pool will be evicted. Bitmaps are allocated according to the weights of the given sizes, where each size gets (weight / prefillWeightSum) percent of the pool to fill.

        Note - Pre-filling is done asynchronously using and MessageQueue.IdleHandler. Any currently running pre-fill will be cancelled and replaced by a call to this method.

        This method should be used with caution, overly aggressive pre-filling is substantially worse than not pre-filling at all. Pre-filling should only be started in onCreate to avoid constantly clearing and re-filling the BitmapPool. Rotation should be carefully considered as well. It may be worth calling this method only when no saved instance state exists so that pre-filling only happens when the Activity is first created, rather than on every rotation.

        Parameters:
        bitmapAttributeBuilders - The list of Builders representing individual sizes and configurations of Bitmaps to be pre-filled.
      • clearMemory

        public void clearMemory()
        Clears as much memory as possible.
        See Also:
        ComponentCallbacks.onLowMemory(), ComponentCallbacks.onLowMemory()
      • trimMemory

        public void trimMemory​(int level)
        Clears some memory with the exact amount depending on the given level.
        See Also:
        ComponentCallbacks2.onTrimMemory(int)
      • clearDiskCache

        public void clearDiskCache()
        Clears disk cache.

        This method should always be called on a background thread, since it is a blocking call.

      • getRequestManagerRetriever

        @NonNull
        public RequestManagerRetriever getRequestManagerRetriever()
        Internal method.
      • with

        @NonNull
        public static RequestManager with​(@NonNull
                                          android.content.Context context)
        Begin a load with Glide by passing in a context.

        Any requests started using a context will only have the application level options applied and will not be started or stopped based on lifecycle events. In general, loads should be started at the level the result will be used in. If the resource will be used in a view in a child fragment, the load should be started with with(android.app.Fragment)} using that child fragment. Similarly, if the resource will be used in a view in the parent fragment, the load should be started with with(android.app.Fragment) using the parent fragment. In the same vein, if the resource will be used in a view in an activity, the load should be started with with(android.app.Activity)}.

        This method is appropriate for resources that will be used outside of the normal fragment or activity lifecycle (For example in services, or for notification thumbnails).

        Parameters:
        context - Any context, will not be retained.
        Returns:
        A RequestManager for the top level application that can be used to start a load.
        See Also:
        with(android.app.Activity), with(android.app.Fragment), with(androidx.fragment.app.Fragment), with(androidx.fragment.app.FragmentActivity)
      • with

        @NonNull
        @Deprecated
        public static RequestManager with​(@NonNull
                                          android.app.Activity activity)
        Deprecated.
        TODO(judds): Figure out the end state and list it here.
        Begin a load with Glide that will be tied to the given Activity's lifecycle and that uses the given Activity's default options.
        Parameters:
        activity - The activity to use.
        Returns:
        A RequestManager for the given activity that can be used to start a load.
      • with

        @NonNull
        public static RequestManager with​(@NonNull
                                          androidx.fragment.app.FragmentActivity activity)
        Begin a load with Glide that will tied to the give FragmentActivity's lifecycle and that uses the given FragmentActivity's default options.
        Parameters:
        activity - The activity to use.
        Returns:
        A RequestManager for the given FragmentActivity that can be used to start a load.
      • with

        @NonNull
        public static RequestManager with​(@NonNull
                                          androidx.fragment.app.Fragment fragment)
        Begin a load with Glide that will be tied to the given Fragment's lifecycle and that uses the given Fragment's default options.
        Parameters:
        fragment - The fragment to use.
        Returns:
        A RequestManager for the given Fragment that can be used to start a load.
      • with

        @Deprecated
        @NonNull
        public static RequestManager with​(@NonNull
                                          android.app.Fragment fragment)
        Deprecated.
        Prefer support Fragments and with(Fragment) instead, Fragment will be deprecated. See https://github.com/android/android-ktx/pull/161#issuecomment-363270555.
        Begin a load with Glide that will be tied to the given Fragment's lifecycle and that uses the given Fragment's default options.
        Parameters:
        fragment - The fragment to use.
        Returns:
        A RequestManager for the given Fragment that can be used to start a load.
      • with

        @NonNull
        public static RequestManager with​(@NonNull
                                          android.view.View view)
        Begin a load with Glide that will be tied to the lifecycle of the Fragment, Fragment, or Activity that contains the View.

        A Fragment or Fragment is assumed to contain a View if the View is a child of the View returned by the Fragment.getView()} method.

        This method will not work if the View is not attached. Prefer the Activity and Fragment variants unless you're loading in a View subclass.

        This method may be inefficient aways and is definitely inefficient for large hierarchies. Consider memoizing the result after the View is attached or again, prefer the Activity and Fragment variants whenever possible.

        When used in Applications that use the non-support Fragment classes, calling this method will produce noisy logs from FragmentManager. Consider avoiding entirely or using the Fragments from the support library instead.

        If the support FragmentActivity class is used, this method will only attempt to discover support Fragments. Any non-support Fragments attached to the FragmentActivity will be ignored.

        Parameters:
        view - The view to search for a containing Fragment or Activity from.
        Returns:
        A RequestManager that can be used to start a load.
      • getRegistry

        @NonNull
        public Registry getRegistry()
      • onTrimMemory

        public void onTrimMemory​(int level)
        Specified by:
        onTrimMemory in interface android.content.ComponentCallbacks2
      • onConfigurationChanged

        public void onConfigurationChanged​(android.content.res.Configuration newConfig)
        Specified by:
        onConfigurationChanged in interface android.content.ComponentCallbacks
      • onLowMemory

        public void onLowMemory()
        Specified by:
        onLowMemory in interface android.content.ComponentCallbacks