public class Glide
extends java.lang.Object
implements android.content.ComponentCallbacks2
RequestBuilder
and maintaining an Engine
, BitmapPool
, DiskCache
and MemoryCache
.Modifier and Type | Class and Description |
---|---|
static interface |
Glide.RequestOptionsFactory
Creates a new instance of
RequestOptions . |
Modifier and Type | Method and Description |
---|---|
void |
clearDiskCache()
Clears disk cache.
|
void |
clearMemory()
Clears as much memory as possible.
|
static Glide |
get(android.content.Context context)
Get the singleton.
|
ArrayPool |
getArrayPool() |
BitmapPool |
getBitmapPool()
Returns the
BitmapPool used to
temporarily store Bitmap s so they can be reused to avoid garbage
collections. |
android.content.Context |
getContext() |
static java.io.File |
getPhotoCacheDir(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.File |
getPhotoCacheDir(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.
|
Registry |
getRegistry() |
RequestManagerRetriever |
getRequestManagerRetriever()
Internal method.
|
static void |
init(android.content.Context context,
GlideBuilder builder) |
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. |
void |
onConfigurationChanged(android.content.res.Configuration newConfig) |
void |
onLowMemory() |
void |
onTrimMemory(int level) |
void |
preFillBitmapPool(PreFillType.Builder... bitmapAttributeBuilders)
Pre-fills the
BitmapPool using the given sizes. |
MemoryCategory |
setMemoryCategory(MemoryCategory memoryCategory)
Adjusts Glide's current and maximum memory usage based on the given
MemoryCategory . |
static void |
tearDown() |
void |
trimMemory(int level)
Clears some memory with the exact amount depending on the given level.
|
static RequestManager |
with(android.app.Activity activity)
Begin a load with Glide that will be tied to the given
Activity 's lifecycle
and that uses the given Activity 's default options. |
static RequestManager |
with(android.content.Context context)
Begin a load with Glide by passing in a context.
|
static RequestManager |
with(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. |
static RequestManager |
with(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. |
static RequestManager |
with(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. |
static RequestManager |
with(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. |
@Nullable public static java.io.File getPhotoCacheDir(@NonNull android.content.Context context)
context
- A context.getPhotoCacheDir(android.content.Context, String)
@Nullable public static java.io.File getPhotoCacheDir(@NonNull android.content.Context context, @NonNull java.lang.String cacheName)
context
- A context.cacheName
- The name of the subdirectory in which to store the cache.getPhotoCacheDir(android.content.Context)
@NonNull public static Glide get(@NonNull android.content.Context context)
@Deprecated public static void init(Glide glide)
init(Context, GlideBuilder)
to get a singleton compatible with Glide's
generated API.
This method will be removed in a future version of Glide.
public static void init(@NonNull android.content.Context context, @NonNull GlideBuilder builder)
public static void tearDown()
@NonNull public BitmapPool getBitmapPool()
BitmapPool
used to
temporarily store Bitmap
s 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 ResourceDecoder
s and Transformation
s.
Use outside of these classes is not generally recommended.
@NonNull public ArrayPool getArrayPool()
@NonNull public android.content.Context getContext()
public void preFillBitmapPool(@NonNull PreFillType.Builder... bitmapAttributeBuilders)
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.
bitmapAttributeBuilders
- The list of Builders
representing individual
sizes and configurations of Bitmap
s to be pre-filled.public void clearMemory()
ComponentCallbacks.onLowMemory()
,
ComponentCallbacks.onLowMemory()
public void trimMemory(int level)
ComponentCallbacks2.onTrimMemory(int)
public void clearDiskCache()
This method should always be called on a background thread, since it is a blocking call.
@NonNull public RequestManagerRetriever getRequestManagerRetriever()
@NonNull public MemoryCategory setMemoryCategory(@NonNull MemoryCategory memoryCategory)
MemoryCategory
.
The default MemoryCategory
is MemoryCategory.NORMAL
. MemoryCategory.HIGH
increases Glide's maximum memory usage by up to 50% and MemoryCategory.LOW
decreases 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. Use
GlideBuilder.setMemoryCache(MemoryCache)
to put a permanent memory size if you want to
change the default.
@NonNull public static RequestManager with(@NonNull android.content.Context 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).
context
- Any context, will not be retained.with(android.app.Activity)
,
with(android.app.Fragment)
,
with(androidx.fragment.app.Fragment)
,
with(androidx.fragment.app.FragmentActivity)
@NonNull public static RequestManager with(@NonNull android.app.Activity activity)
Activity
's lifecycle
and that uses the given Activity
's default options.activity
- The activity to use.@NonNull public static RequestManager with(@NonNull androidx.fragment.app.FragmentActivity activity)
FragmentActivity
's lifecycle and that uses the given FragmentActivity
's default options.activity
- The activity to use.@NonNull public static RequestManager with(@NonNull androidx.fragment.app.Fragment fragment)
Fragment
's
lifecycle and that uses the given Fragment
's default options.fragment
- The fragment to use.@Deprecated @NonNull public static RequestManager with(@NonNull android.app.Fragment fragment)
with(Fragment)
instead, Fragment
will be deprecated. See
https://github.com/android/android-ktx/pull/161#issuecomment-363270555.Fragment
's lifecycle
and that uses the given Fragment
's default options.fragment
- The fragment to use.@NonNull public static RequestManager with(@NonNull android.view.View view)
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 Fragment
s from the support library instead.
If the support FragmentActivity
class is used, this method will only attempt to
discover support Fragment
s. Any non-support Fragment
s attached to
the FragmentActivity
will be ignored.
view
- The view to search for a containing Fragment or Activity from.@NonNull public Registry getRegistry()
public void onTrimMemory(int level)
onTrimMemory
in interface android.content.ComponentCallbacks2
public void onConfigurationChanged(android.content.res.Configuration newConfig)
onConfigurationChanged
in interface android.content.ComponentCallbacks
public void onLowMemory()
onLowMemory
in interface android.content.ComponentCallbacks