Package 

Class CustomTarget

  • All Implemented Interfaces:
    com.mobwith.imgmodule.manager.LifecycleListener , com.mobwith.imgmodule.request.target.Target

    
    public abstract class CustomTarget<T>
     implements Target<T>
                        

    A base Target for loading resources (Bitmap, Drawable etc) that are used outside of Views.

    If you're loading a resource into a View, use , a subclass of ImageViewTarget, or CustomViewTarget. Using this class to load resources into Views can prevent ImageModule 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 onLoadCleared and ensure that all references to any resource passed into the target in onResourceReady are removed before onLoadCleared 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 onLoadCleared unimplemented or empty. Even if you do not manually clear this Target, ImageModule may do so automatically after certain lifecycle events in androidx.fragment.app.Fragments and android.app.Activitys.

    This class can only be used with 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 and SizeReadyCallback, extend Target directly instead of using this class.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private Request request
    • Constructor Summary

      Constructors 
      Constructor Description
      CustomTarget() Creates a new CustomTarget that will attempt to load the resource in its original size.
      CustomTarget(int width, int height) Creates a new {@code CustomTarget} that will return the given {@code width} and {@code height} as the requested size (unless overridden by in the request).
    • Method Summary

      Modifier and Type Method Description
      final Request getRequest() Retrieves the current request for this target, should not be called outside of ImageModule.
      final void setRequest(@Nullable() Request request) Sets the current request for this target to retain, should not be called outside of ImageModule.
      void onStart() Callback for when onStart} or is called.
      void onStop() Callback for when onStop} or } is called.
      void onDestroy() Callback for when onDestroy} or is called.
      void onLoadStarted(@Nullable() Drawable placeholder) A lifecycle callback that is called when a load is started.
      void onLoadFailed(@Nullable() Drawable errorDrawable) A mandatory lifecycle callback that is called when a load fails.
      final void getSize(@NonNull() SizeReadyCallback cb) A method to retrieve the size of this target.
      final void removeCallback(@NonNull() SizeReadyCallback cb) Removes the given callback from the pending set if it's still retained.
      • Methods inherited from class com.mobwith.imgmodule.request.target.Target

        onLoadCleared, onResourceReady
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • CustomTarget

        CustomTarget()
        Creates a new CustomTarget that will attempt to load the resource in its original size.
      • CustomTarget

        CustomTarget(int width, int height)
        Creates a new {@code CustomTarget} that will return the given {@code width} and {@code height} as the requested size (unless overridden by in the request).
        Parameters:
        width - The requested width (> 0, or == Target.SIZE_ORIGINAL).
        height - The requested height (> 0, or == Target.SIZE_ORIGINAL).
    • Method Detail

      • getRequest

        @Nullable() final Request getRequest()

        Retrieves the current request for this target, should not be called outside of ImageModule.

      • setRequest

         final void setRequest(@Nullable() Request request)

        Sets the current request for this target to retain, should not be called outside of ImageModule.

      • onStop

         void onStop()

        Callback for when onStop} or } is called.

      • onLoadStarted

         void onLoadStarted(@Nullable() Drawable placeholder)

        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 failbefore 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 maycause a call here.

        Parameters:
        placeholder - The placeholder drawable to optionally show, or null.
      • onLoadFailed

         void onLoadFailed(@Nullable() Drawable errorDrawable)

        A mandatory lifecycle callback that is called when a load fails.

        Note - This may be called before onLoadStarted if the model object is null.

        You must ensure that any current Drawable received in onResourceReady is no longer used before redrawing the container (usually a View) or changing itsvisibility.

        Parameters:
        errorDrawable - The error drawable to optionally show, or null.
      • getSize

         final void getSize(@NonNull() SizeReadyCallback cb)

        A method to retrieve the size of this target.

        Parameters:
        cb - The callback that must be called when the size of the target has been determined
      • removeCallback

         final void removeCallback(@NonNull() SizeReadyCallback cb)

        Removes the given callback from the pending set if it's still retained.

        Parameters:
        cb - The callback to remove.