com.bumptech.glide
Class Glide

java.lang.Object
  extended by com.bumptech.glide.Glide

public class Glide
extends Object

A singleton to present a simple static interface for building requests with BitmapRequestBuilder and maintaining an Engine, BitmapPool, DiskCache and MemoryCache.


Method Summary
static
<T> ModelLoader<T,ParcelFileDescriptor>
buildFileDescriptorModelLoader(Class<T> modelClass, Context context)
          A method to build a ModelLoader for the given model class that produces ParcelFileDescriptors using a registered factory.
static
<T> ModelLoader<T,ParcelFileDescriptor>
buildFileDescriptorModelLoader(T model, Context context)
          A method to build a ModelLoader for the given model class that produces ParcelFileDescriptors using a registered factory.
static
<T,Y> ModelLoader<T,Y>
buildModelLoader(Class<T> modelClass, Class<Y> resourceClass, Context context)
          Build a ModelLoader for the given model class using registered ModelLoaderFactorys.
static
<T,Y> ModelLoader<T,Y>
buildModelLoader(T model, Class<Y> resourceClass, Context context)
          A convenience method to build a ModelLoader for a given model object using registered ModelLoaderFactorys.
static
<T> ModelLoader<T,InputStream>
buildStreamModelLoader(Class<T> modelClass, Context context)
          A method to build a ModelLoader for the given model that produces InputStreams using a registered factory.
static
<T> ModelLoader<T,InputStream>
buildStreamModelLoader(T model, Context context)
          A method to build a ModelLoader for the given model that produces InputStreams using a registered factory.
static void clear(FutureTarget<?> target)
          Cancel any pending loads Glide may have for the target and free any resources that may have been loaded into the target so they may be reused.
static void clear(Target<?> target)
          Cancel any pending loads Glide may have for the target and free any resources (such as Bitmaps) that may have been loaded for the target so they may be reused.
static void clear(View view)
          Cancel any pending loads Glide may have for the view and free any resources that may have been loaded for the view.
 void clearDiskCache()
          Clears disk cache.
 void clearMemory()
          Clears as much memory as possible.
static Glide get(Context context)
          Get the singleton.
 BitmapPool getBitmapPool()
          Returns the BitmapPool used to temporarily store Bitmaps so they can be reused to avoid garbage collections.
static File getPhotoCacheDir(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 File getPhotoCacheDir(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.
static boolean isSetup()
          Deprecated. Use GlideModule instead. Scheduled to be removed in Glide 4.0.
 void preFillBitmapPool(PreFillType.Builder... bitmapAttributeBuilders)
          Pre-fills the BitmapPool using the given sizes.
<T,Y> void
register(Class<T> modelClass, Class<Y> resourceClass, ModelLoaderFactory<T,Y> factory)
          Use the given factory to build a ModelLoader for models of the given class.
 void setMemoryCategory(MemoryCategory memoryCategory)
          Adjusts Glide's current and maximum memory usage based on the given MemoryCategory.
static void setup(GlideBuilder builder)
          Deprecated. Use GlideModule instead. Scheduled to be removed in Glide 4.0.
 void trimMemory(int level)
          Clears some memory with the exact amount depending on the given level.
<T,Y> void
unregister(Class<T> modelClass, Class<Y> resourceClass)
          Deprecated. Use register(Class, Class, com.bumptech.glide.load.model.ModelLoaderFactory) to replace a registered loader rather than simply removing it.
static RequestManager with(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(Context context)
          Begin a load with Glide by passing in a context.
static RequestManager with(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(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(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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getPhotoCacheDir

public static File getPhotoCacheDir(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

public static File getPhotoCacheDir(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.

Parameters:
context - A context.
cacheName - The name of the subdirectory in which to store the cache.
See Also:
getPhotoCacheDir(android.content.Context)

get

public static Glide get(Context context)
Get the singleton.

Returns:
the singleton

isSetup

@Deprecated
public static boolean isSetup()
Deprecated. Use GlideModule instead. Scheduled to be removed in Glide 4.0.

Returns false if the Glide singleton has not yet been created and can therefore be setup using setup(GlideBuilder).

See Also:
setup(GlideBuilder)

setup

@Deprecated
public static void setup(GlideBuilder builder)
Deprecated. Use GlideModule instead. Scheduled to be removed in Glide 4.0.

Creates the Glide singleton using the given builder. Can be used to set options like cache sizes and locations.

Parameters:
builder - The builder.
Throws:
IllegalArgumentException - if the Glide singleton has already been created.
See Also:
isSetup()

getBitmapPool

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.


preFillBitmapPool

public void preFillBitmapPool(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.


setMemoryCategory

public void setMemoryCategory(MemoryCategory memoryCategory)
Adjusts Glide's current and maximum memory usage based on the given 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 useage for a single Activity or part of the app. Use GlideBuilder.setMemoryCache(MemoryCache) to set a permanent memory size if you want to change the default.


clear

public static void clear(Target<?> target)
Cancel any pending loads Glide may have for the target and free any resources (such as Bitmaps) that may have been loaded for the target so they may be reused.

Parameters:
target - The Target to cancel loads for.

clear

public static void clear(FutureTarget<?> target)
Cancel any pending loads Glide may have for the target and free any resources that may have been loaded into the target so they may be reused.

Parameters:
target - The target to cancel loads for.

clear

public static void clear(View view)
Cancel any pending loads Glide may have for the view and free any resources that may have been loaded for the view.

Note that this will only work if View.setTag(Object) is not called on this view outside of Glide.

Parameters:
view - The view to cancel loads and free resources for.
Throws:
IllegalArgumentException - if an object other than Glide's metadata is set as the view's tag.
See Also:
clear(Target).

register

public <T,Y> void register(Class<T> modelClass,
                           Class<Y> resourceClass,
                           ModelLoaderFactory<T,Y> factory)
Use the given factory to build a ModelLoader for models of the given class. Generally the best use of this method is to replace one of the default factories or add an implementation for other similar low level models. Typically the RequestManager.using(com.bumptech.glide.load.model.stream.StreamModelLoader) or RequestManager.using(com.bumptech.glide.load.model.file_descriptor.FileDescriptorModelLoader) syntax is preferred because it directly links the model with the ModelLoader being used to load it. Any factory replaced by the given factory will have its ModelLoaderFactory.teardown()} method called.

Note - If a factory already exists for the given class, it will be replaced. If that factory is not being used for any other model class, ModelLoaderFactory.teardown() will be called.

Note - The factory must not be an anonymous inner class of an Activity or another object that cannot be retained statically.

Type Parameters:
T - The type of the model.
Y - the type of the resource.
Parameters:
modelClass - The model class.
resourceClass - The resource class the model loader will translate the model type into.
factory - The factory to use.
See Also:
RequestManager.using(com.bumptech.glide.load.model.file_descriptor.FileDescriptorModelLoader), RequestManager.using(com.bumptech.glide.load.model.stream.StreamModelLoader)

unregister

@Deprecated
public <T,Y> void unregister(Class<T> modelClass,
                                        Class<Y> resourceClass)
Deprecated. Use register(Class, Class, com.bumptech.glide.load.model.ModelLoaderFactory) to replace a registered loader rather than simply removing it.

Removes any ModelLoaderFactory registered for the given model and resource classes if one exists. If a ModelLoaderFactory is removed, its ModelLoaderFactory.teardown()} method will be called.

Type Parameters:
T - The type of the model.
Y - The type of the resource.
Parameters:
modelClass - The model class.
resourceClass - The resource class.

buildModelLoader

public static <T,Y> ModelLoader<T,Y> buildModelLoader(Class<T> modelClass,
                                                      Class<Y> resourceClass,
                                                      Context context)
Build a ModelLoader for the given model class using registered ModelLoaderFactorys.

Type Parameters:
T - The type of the model.
Y - The type of the resource.
Parameters:
modelClass - The class to get a ModelLoader for.
resourceClass - The resource class to get a ModelLoader for.
context - Any context.
Returns:
A new ModelLoader for the given model class.
See Also:
buildModelLoader(Object, Class, Context), buildStreamModelLoader(Class, Context), buildFileDescriptorModelLoader(Class, Context)

buildModelLoader

public static <T,Y> ModelLoader<T,Y> buildModelLoader(T model,
                                                      Class<Y> resourceClass,
                                                      Context context)
A convenience method to build a ModelLoader for a given model object using registered ModelLoaderFactorys.

Type Parameters:
T - The type of the model.
Y - The type of the resource.
Parameters:
model - A non null model object whose class we will get a ModelLoader for.
resourceClass - The resource class to get a ModelLoader for.
context - Any context.
Returns:
A new ModelLoader for the given model and resource classes, or null if model is null.
See Also:
buildModelLoader(Class, Class, Context)

buildStreamModelLoader

public static <T> ModelLoader<T,InputStream> buildStreamModelLoader(Class<T> modelClass,
                                                                    Context context)
A method to build a ModelLoader for the given model that produces InputStreams using a registered factory.

See Also:
buildModelLoader(Class, Class, android.content.Context)

buildStreamModelLoader

public static <T> ModelLoader<T,InputStream> buildStreamModelLoader(T model,
                                                                    Context context)
A method to build a ModelLoader for the given model that produces InputStreams using a registered factory.

See Also:
buildModelLoader(Object, Class, Context)

buildFileDescriptorModelLoader

public static <T> ModelLoader<T,ParcelFileDescriptor> buildFileDescriptorModelLoader(Class<T> modelClass,
                                                                                     Context context)
A method to build a ModelLoader for the given model class that produces ParcelFileDescriptors using a registered factory.

See Also:
buildModelLoader(Class, Class, android.content.Context)

buildFileDescriptorModelLoader

public static <T> ModelLoader<T,ParcelFileDescriptor> buildFileDescriptorModelLoader(T model,
                                                                                     Context context)
A method to build a ModelLoader for the given model class that produces ParcelFileDescriptors using a registered factory.

See Also:
buildModelLoader(Object, Class, android.content.Context)

with

public static RequestManager with(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(android.support.v4.app.Fragment), with(android.support.v4.app.FragmentActivity)

with

public static RequestManager with(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.

Parameters:
activity - The activity to use.
Returns:
A RequestManager for the given activity that can be used to start a load.

with

public static RequestManager with(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

public static RequestManager with(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

public static RequestManager with(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.