Class Glide
- All Implemented Interfaces:
ComponentCallbacks,ComponentCallbacks2
RequestBuilder and maintaining an Engine, BitmapPool, DiskCache and MemoryCache.-
Nested Class Summary
Nested Classes -
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 Summary
Modifier and TypeMethodDescriptionvoidClears disk cache.voidClears as much memory as possible.static voidAllows hardware Bitmaps to be used prior to the first frame in the app being drawn as soon as this method is called.static GlideGet the singleton.Returns theBitmapPoolused to temporarily storeBitmaps so they can be reused to avoid garbage collections.static FilegetPhotoCacheDir(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 FilegetPhotoCacheDir(Context context, 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.Internal method.static voidinit(Context context, GlideBuilder builder) static voidDeprecated.static booleanvoidonConfigurationChanged(Configuration newConfig) voidvoidonTrimMemory(int level) voidpreFillBitmapPool(PreFillType.Builder... bitmapAttributeBuilders) Pre-fills theBitmapPoolusing the given sizes.setMemoryCategory(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 RequestManagerDeprecated.This is equivalent to callingwith(Context)using the application context.static RequestManagerDeprecated.This method is identical to callingwith(Context)using the application context.static RequestManagerBegin a load with Glide by passing in a context.static RequestManagerstatic 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 Details
-
getPhotoCacheDir
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
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:
-
get
Get the singleton.- Returns:
- the singleton
-
init
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
-
isInitialized
public static boolean isInitialized() -
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
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
-
getContext
- Returns:
- The context associated with this instance.
-
preFillBitmapPool
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. -
clearMemory
public void clearMemory()Clears as much memory as possible. -
trimMemory
public void trimMemory(int level) Clears some memory with the exact amount depending on the given level.- See Also:
-
clearDiskCache
public void clearDiskCache()Clears disk cache.This method should always be called on a background thread, since it is a blocking call.
-
getRequestManagerRetriever
Internal method. -
setMemoryCategory
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
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
Deprecated.This is equivalent to callingwith(Context)using the application context. Use the androidx Activity class instead (ieFragmentActivity, orAppCompatActivity).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
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.This method is identical to callingwith(Context)using the application context. Prefer support Fragments andwith(Fragment)instead. 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
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
-
onTrimMemory
public void onTrimMemory(int level) - Specified by:
onTrimMemoryin interfaceComponentCallbacks2
-
onConfigurationChanged
- Specified by:
onConfigurationChangedin interfaceComponentCallbacks
-
onLowMemory
public void onLowMemory()- Specified by:
onLowMemoryin interfaceComponentCallbacks
-
init(Context, GlideBuilder)to get a singleton compatible with Glide's generated API.