Class CustomViewTarget<T extends android.view.View,Z>
- java.lang.Object
-
- com.bumptech.glide.request.target.CustomViewTarget<T,Z>
-
- Type Parameters:
T- The specific subclass of view wrapped by this target (e.g.ImageView)Z- The resource type this target will receive (e.g.Bitmap).
- All Implemented Interfaces:
LifecycleListener,Target<Z>
public abstract class CustomViewTarget<T extends android.view.View,Z> extends java.lang.Object implements Target<Z>
A baseTargetfor loading resources (Bitmap,Drawableetc) intoViews that provides default implementations for most methods and can determine the size of views using aViewTreeObserver.OnDrawListener.
-
-
Field Summary
Fields Modifier and Type Field Description protected Tview-
Fields inherited from interface com.bumptech.glide.request.target.Target
SIZE_ORIGINAL
-
-
Constructor Summary
Constructors Constructor Description CustomViewTarget(T view)Constructor that defaultswaitForLayouttofalse.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description CustomViewTarget<T,Z>clearOnDetach()RequestgetRequest()Returns any stored request usingView.getTag().voidgetSize(SizeReadyCallback cb)Determines the size of the view by first checkingView.getWidth()andView.getHeight().TgetView()Returns the wrappedView.voidonDestroy()Callback for whenFragment.onDestroy()} orActivity.onDestroy()is called.voidonLoadCleared(android.graphics.drawable.Drawable placeholder)A mandatory lifecycle callback that is called when a load is cancelled and its resources are freed.voidonLoadStarted(android.graphics.drawable.Drawable placeholder)A lifecycle callback that is called when a load is started.protected abstract voidonResourceCleared(android.graphics.drawable.Drawable placeholder)A required callback invoked when the resource is no longer valid and must be freed.protected voidonResourceLoading(android.graphics.drawable.Drawable placeholder)An optional callback invoked when a resource 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)Stores the request usingView.setTag(Object).java.lang.StringtoString()CustomViewTarget<T,Z>useTagId(int tagId)Deprecated.Using this method prevents clearing the target from working properly.CustomViewTarget<T,Z>waitForLayout()Indicates that Glide should always wait for any pending layout pass before checking for the size anView.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface com.bumptech.glide.request.target.Target
onLoadFailed, onResourceReady
-
-
-
-
Field Detail
-
view
protected final T extends android.view.View view
-
-
Constructor Detail
-
CustomViewTarget
public CustomViewTarget(@NonNull T view)Constructor that defaultswaitForLayouttofalse.
-
-
Method Detail
-
onResourceCleared
protected abstract void onResourceCleared(@Nullable android.graphics.drawable.Drawable placeholder)A required callback invoked when the resource is no longer valid and must be freed.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. Not doing so will result in crashes in your app.- Parameters:
placeholder- The placeholder drawable to optionally show, or null.
-
onResourceLoading
protected void onResourceLoading(@Nullable android.graphics.drawable.Drawable placeholder)An optional callback invoked when a resource load is started.- Parameters:
placeholder- The placeholder drawable to optionally show, or null.- See Also:
Target.onLoadStarted(Drawable)
-
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
-
waitForLayout
@NonNull public final CustomViewTarget<T,Z> waitForLayout()
Indicates that Glide should always wait for any pending layout pass before checking for the size anView.By default, Glide will only wait for a pending layout pass if it's unable to resolve the size from the
ViewGroup.LayoutParamsor valid non-zero values forView.getWidth()andView.getHeight().Because calling this method forces Glide to wait for the layout pass to occur before starting loads, setting this parameter to
truecan cause Glide to asynchronous load an image even if it's in the memory cache. The load will happen asynchronously because Glide has to wait for a layout pass to occur, which won't necessarily happen in the same frame as when the image is requested. As a result, using this method can resulting in flashing in some cases and should be used sparingly.If the
ViewGroup.LayoutParamsof the wrappedVieware set to fixed sizes, they will still be used instead of theView's dimensions even if this method is called. This parameter is a fallback only.
-
clearOnDetach
@NonNull public final CustomViewTarget<T,Z> clearOnDetach()
Clears theView'sRequestwhen theViewis detached from itsWindowand restarts theRequestwhen theViewis re-attached from itsWindow.This is an experimental API that may be removed in a future version.
Using this method can save memory by allowing Glide to more eagerly clear resources when transitioning screens or swapping adapters in scrolling views. However it also substantially increases the odds that images will not be in memory if users subsequently return to a screen where images were previously loaded. Whether or not this happens will depend on the number of images loaded in the new screen and the size of the memory cache. Increasing the size of the memory cache can improve this behavior but it largely negates the memory benefits of using this method.
Use this method with caution and measure your memory usage to ensure that it's actually improving your memory usage in the cases you care about.
-
useTagId
@Deprecated public final CustomViewTarget<T,Z> useTagId(@IdRes int tagId)
Deprecated.Using this method prevents clearing the target from working properly. Glide uses its own internal tag id so this method should not be necessary. This method is currently a no-op.Override the android resource id to store temporary state allowing loads to be automatically cancelled and resources re-used in scrolling lists.Unlike
ViewTarget, it is not necessary to set a custom tag id if your app usesView.setTag(Object). It is only necessary if loading several Glide resources into the same view, for example one foreground and one background view.- Parameters:
tagId- The android resource id to use.
-
getView
@NonNull public final T getView()
Returns the wrappedView.
-
getSize
public final void getSize(@NonNull SizeReadyCallback cb)Determines the size of the view by first checkingView.getWidth()andView.getHeight(). If one or both are zero, it then checks the view'sViewGroup.LayoutParams. If one or both of the params width and height are less than or equal to zero, it then adds anViewTreeObserver.OnPreDrawListenerwhich waits until the view has been measured before calling the callback with the view's drawn width and height.
-
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 extends android.view.View>- Parameters:
cb- The callback to remove.
-
onLoadStarted
public final 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 extends android.view.View>- Parameters:
placeholder- The placeholder drawable to optionally show, or null.
-
onLoadCleared
public final void onLoadCleared(@Nullable android.graphics.drawable.Drawable placeholder)Description copied from interface:TargetA mandatory lifecycle callback that is called when a load is cancelled and its resources are freed.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:
onLoadClearedin interfaceTarget<T extends android.view.View>- Parameters:
placeholder- The placeholder drawable to optionally show, or null.
-
setRequest
public final void setRequest(@Nullable Request request)Stores the request usingView.setTag(Object).- Specified by:
setRequestin interfaceTarget<T extends android.view.View>- Parameters:
request-
-
getRequest
@Nullable public final Request getRequest()
Returns any stored request usingView.getTag().- Specified by:
getRequestin interfaceTarget<T extends android.view.View>
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-