Class CustomTarget<T>
- java.lang.Object
-
- com.bumptech.glide.request.target.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
public abstract class CustomTarget<T> extends java.lang.Object implements Target<T>
A baseTargetfor loading resources (Bitmap,Drawableetc) that are used outside ofViews.If you're loading a resource into a
View, useRequestBuilder.into(ImageView), a subclass ofImageViewTarget, orCustomViewTarget. Using this class to load resources intoViews can prevent Glide from correctly cancelling any previous loads, which may result in incorrect images appearing in the view, especially in scrolling views likeandroidx.recyclerview.widget.RecyclerView.You MUST implement
Target.onLoadCleared(Drawable)and ensure that all references to any resource passed into the target inTarget.onResourceReady(Object, Transition)are removed beforeTarget.onLoadCleared(Drawable)completes. Failing to do so can result in graphical corruption, crashes caused by recycledBitmaps, and other undefined behavior. It is never safe to leaveTarget.onLoadCleared(Drawable)unimplemented or empty. Even if you do not manually clear thisTarget, Glide may do so automatically after certain lifecycle events inFragments andActivitys.This class can only be used with
Target.SIZE_ORIGINALor when the desired resource dimensions are known when theTargetis created. If you'd like to run some asynchronous process and make full use ofgetSize(SizeReadyCallback)andSizeReadyCallback, extendTargetdirectly instead of using this class.
-
-
Field Summary
-
Fields inherited from interface com.bumptech.glide.request.target.Target
SIZE_ORIGINAL
-
-
Constructor Summary
Constructors Constructor Description CustomTarget()Creates 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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description RequestgetRequest()Retrieves the current request for this target, should not be called outside of Glide.voidgetSize(SizeReadyCallback cb)A method to retrieve the size of this target.voidonDestroy()Callback for whenFragment.onDestroy()} orActivity.onDestroy()is called.voidonLoadFailed(android.graphics.drawable.Drawable errorDrawable)A mandatory lifecycle callback that is called when a load fails.voidonLoadStarted(android.graphics.drawable.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.voidremoveCallback(SizeReadyCallback cb)Removes the given callback from the pending set if it's still retained.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, wait
-
Methods inherited from interface com.bumptech.glide.request.target.Target
onLoadCleared, onResourceReady
-
-
-
-
Constructor Detail
-
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 (> 0, or == Target.SIZE_ORIGINAL).height- The requested height (> 0, or == Target.SIZE_ORIGINAL).- Throws:
java.lang.IllegalArgumentException- if width/height doesn't meet the requirement:> 0, or == Target.SIZE_ORIGINAL
-
-
Method Detail
-
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
public void onLoadStarted(@Nullable android.graphics.drawable.Drawable placeholder)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
public void onLoadFailed(@Nullable android.graphics.drawable.Drawable errorDrawable)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
public final void getSize(@NonNull SizeReadyCallback cb)Description copied from interface:TargetA method to retrieve the size of this target.
-
removeCallback
public final void removeCallback(@NonNull SizeReadyCallback cb)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
public final void setRequest(@Nullable Request request)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
@Nullable public final Request getRequest()
Description copied from interface:TargetRetrieves the current request for this target, should not be called outside of Glide.- Specified by:
getRequestin interfaceTarget<T>
-
-