-
- All Implemented Interfaces:
-
com.mobwith.imgmodule.manager.LifecycleListener,com.mobwith.imgmodule.request.target.Target
@Deprecated() public abstract class ViewTarget<T extends View, Z> extends BaseTarget<Z>
A base Target for loading android.graphics.Bitmaps into Views that provides default implementations for most most methods and can determine the size of views using a ViewTreeObserver.OnDrawListener.
To detect View reuse in android.widget.ListView or any that reuses views, this class uses the setTag method to store some metadata so that if a view is reused, any previous loads or resources from previous loads can be cancelled or reused.
Any calls to setTag} on a View given to this class will result in excessive allocations and and/or IllegalArgumentExceptions. If you must call on a view, use setTagId to specify a custom tag for ImageModule to use.
Subclasses must call super in onLoadCleared
-
-
Constructor Summary
Constructors Constructor Description ViewTarget(T view)Constructor that defaults {@code waitForLayout}to{@code false}.ViewTarget(T view, boolean waitForLayout)
-
Method Summary
Modifier and Type Method Description static voidsetTagId(int tagId)Sets the android resource id to use in conjunction with setTag tostore temporary state allowing loads to be automatically cancelled and resources re-used inscrolling lists. TgetView()Returns the wrapped View. final ViewTarget<T, Z>clearOnDetach()Clears the View's Request when the View is detached from its and restarts the Request when the View is re-attached fromits android.view.Window. final ViewTarget<T, Z>waitForLayout()Indicates that ImageModule should always wait for any pending layout pass before checking for thesize an View. voidonLoadStarted(@Nullable() Drawable placeholder)A lifecycle callback that is called when a load is started. voidgetSize(@NonNull() SizeReadyCallback cb)Determines the size of the view by first checking getWidth and getHeight. voidremoveCallback(@NonNull() SizeReadyCallback cb)Removes the given callback from the pending set if it's still retained. voidonLoadCleared(@Nullable() Drawable placeholder)A mandatory lifecycle callback that is called when a load is cancelled and its resourcesare freed. voidsetRequest(@Nullable() Request request)Stores the request using setTag. RequestgetRequest()Returns any stored request using getTag. StringtoString()-
Methods inherited from class com.mobwith.imgmodule.request.target.BaseTarget
onDestroy, onLoadFailed, onStart, onStop -
Methods inherited from class com.mobwith.imgmodule.request.target.Target
onResourceReady -
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
-
Constructor Detail
-
ViewTarget
ViewTarget(T view)
Constructor that defaults{@code waitForLayout}to{@code false}.
-
ViewTarget
ViewTarget(T view, boolean waitForLayout)
- Parameters:
waitForLayout- If set to{@code true}, ImageModule will always wait for any pending layout passbefore checking for the size a View.
-
-
Method Detail
-
setTagId
@Deprecated() static void setTagId(int tagId)
Sets the android resource id to use in conjunction with setTag tostore temporary state allowing loads to be automatically cancelled and resources re-used inscrolling lists.
If no tag id is set, ImageModule will use setTag.
Warning: prior to Android 4.0 tags were stored in a static map. Using this method prior toAndroid 4.0 may cause memory leaks and isn't recommended. If you do use this method on olderversions, be sure to call clear on any view youstart a load into to ensure that the static state is removed.
- Parameters:
tagId- The android resource to use.
-
clearOnDetach
@NonNull() final ViewTarget<T, Z> clearOnDetach()
Clears the View's Request when the View is detached from its and restarts the Request when the View is re-attached fromits android.view.Window.
This is an experimental API that may be removed in a future version.
Using this method can save memory by allowing ImageModule to more eagerly clear resources whentransitioning screens or swapping adapters in scrolling views. However it also substantiallyincreases the odds that images will not be in memory if users subsequently return to a screenwhere images were previously loaded. Whether or not this happens will depend on the number ofimages loaded in the new screen and the size of the memory cache. Increasing the size of thememory cache can improve this behavior but it largely negates the memory benefits of using thismethod.
Use this method with caution and measure your memory usage to ensure that it's actuallyimproving your memory usage in the cases you care about.
-
waitForLayout
@NonNull() final ViewTarget<T, Z> waitForLayout()
Indicates that ImageModule should always wait for any pending layout pass before checking for thesize an View.
By default, ImageModule will only wait for a pending layout pass if it's unable to resolve thesize from the LayoutParams or valid non-zero values for getWidth and getHeight.
Because calling this method forces ImageModule to wait for the layout pass to occur beforestarting loads, setting this parameter to
{@code true}can cause ImageModule to asynchronous load animage even if it's in the memory cache. The load will happen asynchronously because ImageModule hasto wait for a layout pass to occur, which won't necessarily happen in the same frame as whenthe image is requested. As a result, using this method can resulting in flashing in some casesand should be used sparingly.If the LayoutParams of the wrapped View are set to fixed sizes, they willstill be used instead of the View's dimensions even if this method is called. Thisparameter is a fallback only.
-
onLoadStarted
@CallSuper() 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.
-
getSize
@CallSuper() void getSize(@NonNull() SizeReadyCallback cb)
Determines the size of the view by first checking getWidth and getHeight. If one or both are zero, it then checks the view's LayoutParams. If one or both of the params width and height are less than or equal tozero, it then adds an ViewTreeObserver.OnPreDrawListener which waits untilthe view has been measured before calling the callback with the view's drawn width and height.
- Parameters:
cb- The callback that must be called when the size of the target has been determined
-
removeCallback
@CallSuper() void removeCallback(@NonNull() SizeReadyCallback cb)
Removes the given callback from the pending set if it's still retained.
- Parameters:
cb- The callback to remove.
-
onLoadCleared
@CallSuper() void onLoadCleared(@Nullable() Drawable placeholder)
A mandatory lifecycle callback that is called when a load is cancelled and its resourcesare freed.
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:
placeholder- The placeholder drawable to optionally show, or null.
-
setRequest
void setRequest(@Nullable() Request request)
Stores the request using setTag.
-
getRequest
@Nullable() Request getRequest()
Returns any stored request using getTag.
For ImageModule to function correctly, ImageModule must be the only thing that calls . If the tag is cleared or put to another object type, ImageModule will not beable to retrieve and cancel previous loads which will not only prevent ImageModule from reusingresource, but will also result in incorrect images being loaded and lots of flashing of imagesin lists. As a result, this will throw an IllegalArgumentException if } returns a non null object that is not an .
-
-
-
-