Class CustomTarget<T>
- Type Parameters:
T- The type of resource that will be loaded (e.g.Bitmap).
- All Implemented Interfaces:
LifecycleListener,Target<T>
- Direct Known Subclasses:
AppWidgetTarget,NotificationTarget,PreloadTarget
Target for loading resources (Bitmap, Drawable
etc) that are used outside of Views.
If you're loading a resource into a View, use RequestBuilder.into(ImageView), a subclass of ImageViewTarget, or
CustomViewTarget. Using this class to load resources into Views can prevent Glide
from correctly cancelling any previous loads, which may result in incorrect images appearing in
the view, especially in scrolling views like androidx.recyclerview.widget.RecyclerView.
You MUST implement Target.onLoadCleared(Drawable) and ensure that all references to
any resource passed into the target in Target.onResourceReady(Object, Transition) are removed
before Target.onLoadCleared(Drawable) completes. Failing to do so can result in graphical
corruption, crashes caused by recycled Bitmaps, and other undefined behavior. It is never
safe to leave Target.onLoadCleared(Drawable) unimplemented or empty. Even if you do not
manually clear this Target, Glide may do so automatically after certain lifecycle events
in Fragments and Activitys.
This class can only be used with Target.SIZE_ORIGINAL or when the desired resource
dimensions are known when the Target is created. If you'd like to run some asynchronous
process and make full use of getSize(SizeReadyCallback) and SizeReadyCallback,
extend Target directly instead of using this class.
-
Field Summary
Fields inherited from interface com.bumptech.glide.request.target.Target
SIZE_ORIGINAL -
Constructor Summary
ConstructorsConstructorDescriptionCreates a newCustomTargetthat will attempt to load the resource in its original size.CustomTarget(int width, int height) Creates a newCustomTargetthat will return the givenwidthandheightas the requested size (unless overridden byBaseRequestOptions.override(int)in the request). -
Method Summary
Modifier and TypeMethodDescriptionfinal RequestRetrieves the current request for this target, should not be called outside of Glide.final voidA method to retrieve the size of this target.voidCallback for whenFragment.onDestroy()} orActivity.onDestroy()is called.voidonLoadFailed(Drawable errorDrawable) A mandatory lifecycle callback that is called when a load fails.voidonLoadStarted(Drawable placeholder) A lifecycle callback that is called when a load is started.voidonStart()Callback for whenFragment.onStart()} orActivity.onStart()is called.voidonStop()Callback for whenFragment.onStop()} orActivity.onStop()} is called.final voidRemoves the given callback from the pending set if it's still retained.final voidsetRequest(Request request) Sets the current request for this target to retain, should not be called outside of Glide.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.bumptech.glide.request.target.Target
onLoadCleared, onResourceReady
-
Constructor Details
-
CustomTarget
public CustomTarget()Creates a newCustomTargetthat will attempt to load the resource in its original size.This constructor can cause very memory inefficient loads if the resource is large and can cause OOMs. It's provided as a convenience for when you'd like to specify dimensions with
BaseRequestOptions.override(int). In all other cases, preferCustomTarget(int, int). -
CustomTarget
public CustomTarget(int width, int height) Creates a newCustomTargetthat will return the givenwidthandheightas the requested size (unless overridden byBaseRequestOptions.override(int)in the request).- Parameters:
width- The requested width in pixels (> 0, or == Target.SIZE_ORIGINAL).height- The requested height in pixels (> 0, or == Target.SIZE_ORIGINAL).- Throws:
IllegalArgumentException- if width/height doesn't meet the requirement:> 0, or == Target.SIZE_ORIGINAL
-
-
Method Details
-
onStart
public void onStart()Description copied from interface:LifecycleListenerCallback for whenFragment.onStart()} orActivity.onStart()is called.- Specified by:
onStartin interfaceLifecycleListener
-
onStop
public void onStop()Description copied from interface:LifecycleListenerCallback for whenFragment.onStop()} orActivity.onStop()} is called.- Specified by:
onStopin interfaceLifecycleListener
-
onDestroy
public void onDestroy()Description copied from interface:LifecycleListenerCallback for whenFragment.onDestroy()} orActivity.onDestroy()is called.- Specified by:
onDestroyin interfaceLifecycleListener
-
onLoadStarted
Description copied from interface:TargetA lifecycle callback that is called when a load is started.Note - This may not be called for every load, it is possible for example for loads to fail before the load starts (when the model object is null).
Note - This method may be called multiple times before any other lifecycle method is called. Loads can be paused and restarted due to lifecycle or connectivity events and each restart may cause a call here.
- Specified by:
onLoadStartedin interfaceTarget<T>- Parameters:
placeholder- The placeholder drawable to optionally show, or null.
-
onLoadFailed
Description copied from interface:TargetA mandatory lifecycle callback that is called when a load fails.Note - This may be called before
Target.onLoadStarted(android.graphics.drawable.Drawable)if the model object is null.You must ensure that any current Drawable received in
Target.onResourceReady(Object, Transition)is no longer used before redrawing the container (usually a View) or changing its visibility.- Specified by:
onLoadFailedin interfaceTarget<T>- Parameters:
errorDrawable- The error drawable to optionally show, or null.
-
getSize
Description copied from interface:TargetA method to retrieve the size of this target. -
removeCallback
Description copied from interface:TargetRemoves the given callback from the pending set if it's still retained.- Specified by:
removeCallbackin interfaceTarget<T>- Parameters:
cb- The callback to remove.
-
setRequest
Description copied from interface:TargetSets the current request for this target to retain, should not be called outside of Glide.- Specified by:
setRequestin interfaceTarget<T>
-
getRequest
Description copied from interface:TargetRetrieves the current request for this target, should not be called outside of Glide.- Specified by:
getRequestin interfaceTarget<T>
-