Class Glide
- java.lang.Object
-
- com.bumptech.glide.Glide
-
- All Implemented Interfaces:
android.content.ComponentCallbacks,android.content.ComponentCallbacks2
public class Glide extends java.lang.Object implements android.content.ComponentCallbacks2A singleton to present a simple static interface for building requests withRequestBuilderand maintaining anEngine,BitmapPool,DiskCacheandMemoryCache.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceGlide.RequestOptionsFactoryCreates a new instance ofRequestOptions.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidclearDiskCache()Clears disk cache.voidclearMemory()Clears as much memory as possible.static voidenableHardwareBitmaps()Allows hardware Bitmaps to be used prior to the first frame in the app being drawn as soon as this method is called.static Glideget(android.content.Context context)Get the singleton.ArrayPoolgetArrayPool()BitmapPoolgetBitmapPool()Returns theBitmapPoolused to temporarily storeBitmaps so they can be reused to avoid garbage collections.android.content.ContextgetContext()static java.io.FilegetPhotoCacheDir(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.static java.io.FilegetPhotoCacheDir(android.content.Context context, 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.RegistrygetRegistry()RequestManagerRetrievergetRequestManagerRetriever()Internal method.static voidinit(android.content.Context context, GlideBuilder builder)static voidinit(Glide glide)Deprecated.Useinit(Context, GlideBuilder)to get a singleton compatible with Glide's generated API.voidonConfigurationChanged(android.content.res.Configuration newConfig)voidonLowMemory()voidonTrimMemory(int level)voidpreFillBitmapPool(PreFillType.Builder... bitmapAttributeBuilders)Pre-fills theBitmapPoolusing the given sizes.MemoryCategorysetMemoryCategory(MemoryCategory memoryCategory)Adjusts Glide's current and maximum memory usage based on the givenMemoryCategory.static voidtearDown()voidtrimMemory(int level)Clears some memory with the exact amount depending on the given level.static RequestManagerwith(android.app.Activity activity)Deprecated.TODO(judds): Figure out the end state and list it here.static RequestManagerwith(android.app.Fragment fragment)Deprecated.Prefer support Fragments andwith(Fragment)instead,Fragmentwill be deprecated.static RequestManagerwith(android.content.Context context)Begin a load with Glide by passing in a context.static RequestManagerwith(android.view.View view)Begin a load with Glide that will be tied to the lifecycle of theFragment,Fragment, orActivitythat contains the View.static RequestManagerwith(androidx.fragment.app.Fragment fragment)Begin a load with Glide that will be tied to the givenFragment's lifecycle and that uses the givenFragment's default options.static RequestManagerwith(androidx.fragment.app.FragmentActivity activity)Begin a load with Glide that will tied to the giveFragmentActivity's lifecycle and that uses the givenFragmentActivity's default options.
-
-
-
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.Useinit(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 theBitmapPoolused to temporarily storeBitmaps so they can be reused to avoid garbage collections.Note - Using this pool directly can lead to undefined behavior and strange drawing errors. Any
Bitmapadded to the pool must not be currently in use in any other part of the application. AnyBitmapadded 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
Bitmapremoved 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 andTransformations. 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 theBitmapPoolusing 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 ofBuildersrepresenting individual sizes and configurations ofBitmaps 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.
-
setMemoryCategory
@NonNull public MemoryCategory setMemoryCategory(@NonNull MemoryCategory memoryCategory)
Adjusts Glide's current and maximum memory usage based on the givenMemoryCategory.The default
MemoryCategoryisMemoryCategory.NORMAL.MemoryCategory.HIGHincreases Glide's maximum memory usage by up to 50% andMemoryCategory.LOWdecreases Glide's maximum memory usage by 50%. This method should be used to temporarily increase or decrease memory usage for a single Activity or part of the app. UseGlideBuilder.setMemoryCache(MemoryCache)to put a permanent memory size if you want to change the default.- Returns:
- the previous MemoryCategory used by Glide.
-
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 withwith(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 withwith(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 givenActivity's lifecycle and that uses the givenActivity'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 giveFragmentActivity's lifecycle and that uses the givenFragmentActivity'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 givenFragment's lifecycle and that uses the givenFragment'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 andwith(Fragment)instead,Fragmentwill be deprecated. See https://github.com/android/android-ktx/pull/161#issuecomment-363270555.Begin a load with Glide that will be tied to the givenFragment's lifecycle and that uses the givenFragment'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 theFragment,Fragment, orActivitythat contains the View.A
FragmentorFragmentis assumed to contain a View if the View is a child of the View returned by theFragment.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
Fragmentclasses, calling this method will produce noisy logs fromFragmentManager. Consider avoiding entirely or using theFragments from the support library instead.If the support
FragmentActivityclass is used, this method will only attempt to discover supportFragments. Any non-supportFragments attached to theFragmentActivitywill 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:
onTrimMemoryin interfaceandroid.content.ComponentCallbacks2
-
onConfigurationChanged
public void onConfigurationChanged(android.content.res.Configuration newConfig)
- Specified by:
onConfigurationChangedin interfaceandroid.content.ComponentCallbacks
-
onLowMemory
public void onLowMemory()
- Specified by:
onLowMemoryin interfaceandroid.content.ComponentCallbacks
-
-