Class CustomViewTarget<T extends 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 View,Z> extends Object implements Target<Z>
A base 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 Details

    • view

      protected final T extends View view
  • Constructor Details

    • CustomViewTarget

      public CustomViewTarget(@NonNull T view)
      Constructor that defaults waitForLayout to false.
  • Method Details

    • onResourceCleared

      protected abstract void onResourceCleared(@Nullable 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 Drawable placeholder)
      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: LifecycleListener
      Callback for when Fragment.onStart()} or Activity.onStart() is called.
      Specified by:
      onStart in interface LifecycleListener
    • onStop

      public void onStop()
      Description copied from interface: LifecycleListener
      Callback for when Fragment.onStop()} or Activity.onStop()} is called.
      Specified by:
      onStop in interface LifecycleListener
    • onDestroy

      public void onDestroy()
      Description copied from interface: LifecycleListener
      Callback for when Fragment.onDestroy()} or Activity.onDestroy() is called.
      Specified by:
      onDestroy in interface LifecycleListener
    • waitForLayout

      @NonNull public final CustomViewTarget<T,Z> waitForLayout()
      Indicates that Glide should always wait for any pending layout pass before checking for the size an View.

      By default, Glide will only wait for a pending layout pass if it's unable to resolve the size from the ViewGroup.LayoutParams or valid non-zero values for View.getWidth() and View.getHeight().

      Because calling this method forces Glide to wait for the layout pass to occur before starting loads, setting this parameter to true can 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.LayoutParams of the wrapped View are set to fixed sizes, they will still be used instead of the View's dimensions even if this method is called. This parameter is a fallback only.

    • clearOnDetach

      @NonNull public final CustomViewTarget<T,Z> clearOnDetach()
      Clears the View's Request when the View is detached from its Window and restarts the Request when the View is re-attached from its Window.

      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 uses View.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 wrapped View.
    • getSize

      public final void getSize(@NonNull SizeReadyCallback cb)
      Determines the size of the view by first checking View.getWidth() and View.getHeight(). If one or both are zero, it then checks the view's ViewGroup.LayoutParams. If one or both of the params width and height are less than or equal to zero, it then adds an ViewTreeObserver.OnPreDrawListener which waits until the view has been measured before calling the callback with the view's drawn width and height.
      Specified by:
      getSize in interface Target<T extends View>
      Parameters:
      cb - The callback that must be called when the size of the target has been determined
    • removeCallback

      public final void removeCallback(@NonNull SizeReadyCallback cb)
      Description copied from interface: Target
      Removes the given callback from the pending set if it's still retained.
      Specified by:
      removeCallback in interface Target<T extends View>
      Parameters:
      cb - The callback to remove.
    • onLoadStarted

      public final void onLoadStarted(@Nullable Drawable placeholder)
      Description copied from interface: Target
      A 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:
      onLoadStarted in interface Target<T extends View>
      Parameters:
      placeholder - The placeholder drawable to optionally show, or null.
    • onLoadCleared

      public final void onLoadCleared(@Nullable Drawable placeholder)
      Description copied from interface: Target
      A 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:
      onLoadCleared in interface Target<T extends View>
      Parameters:
      placeholder - The placeholder drawable to optionally show, or null.
    • setRequest

      public final void setRequest(@Nullable Request request)
      Stores the request using View.setTag(Object).
      Specified by:
      setRequest in interface Target<T extends View>
      Parameters:
      request -
    • getRequest

      @Nullable public final Request getRequest()
      Returns any stored request using View.getTag().
      Specified by:
      getRequest in interface Target<T extends View>
    • toString

      public String toString()
      Overrides:
      toString in class Object