Class CustomViewTarget<T extends View,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>
Target for loading resources (Bitmap, Drawable
etc) into Views that provides default implementations for most methods and can determine
the size of views using a ViewTreeObserver.OnDrawListener.-
Field Summary
FieldsFields inherited from interface com.bumptech.glide.request.target.Target
SIZE_ORIGINAL -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal CustomViewTarget<T,Z> final RequestReturns any stored request usingView.getTag().final voidDetermines the size of the view by first checkingView.getWidth()andView.getHeight().final TgetView()Returns the wrappedView.voidCallback for whenFragment.onDestroy()} orActivity.onDestroy()is called.final voidonLoadCleared(Drawable placeholder) A mandatory lifecycle callback that is called when a load is cancelled and its resources are freed.final voidonLoadStarted(Drawable placeholder) A lifecycle callback that is called when a load is started.protected abstract voidonResourceCleared(Drawable placeholder) A required callback invoked when the resource is no longer valid and must be freed.protected voidonResourceLoading(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.final voidRemoves the given callback from the pending set if it's still retained.final voidsetRequest(Request request) Stores the request usingView.setTag(Object).toString()final CustomViewTarget<T,Z> useTagId(int tagId) Deprecated.Using this method prevents clearing the target from working properly.final CustomViewTarget<T,Z> 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, waitMethods inherited from interface com.bumptech.glide.request.target.Target
onLoadFailed, onResourceReady
-
Field Details
-
view
-
-
Constructor Details
-
CustomViewTarget
Constructor that defaultswaitForLayouttofalse.
-
-
Method Details
-
onResourceCleared
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
An optional callback invoked when a resource load is started.- Parameters:
placeholder- The placeholder drawable to optionally show, or null.- See Also:
-
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
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
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.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
Returns the wrappedView. -
getSize
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
Description copied from interface:TargetRemoves the given callback from the pending set if it's still retained.- Specified by:
removeCallbackin interfaceTarget<T extends View>- Parameters:
cb- The callback to remove.
-
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 extends View>- Parameters:
placeholder- The placeholder drawable to optionally show, or null.
-
onLoadCleared
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 View>- Parameters:
placeholder- The placeholder drawable to optionally show, or null.
-
setRequest
Stores the request usingView.setTag(Object).- Specified by:
setRequestin interfaceTarget<T extends View>- Parameters:
request-
-
getRequest
Returns any stored request usingView.getTag().- Specified by:
getRequestin interfaceTarget<T extends View>
-
toString
-