T
- The specific subclass of view wrapped by this target.Z
- The resource type this target will receive.public abstract class ViewTarget<T extends View,Z> extends BaseTarget<Z>
Target
for loading Bitmap
s into View
s that
provides default implementations for most most methods and can determine the size of views using
a ViewTreeObserver.OnDrawListener
.
To detect View
reuse in ListView
or any ViewGroup
that reuses views, this class uses the View.setTag(Object)
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 View.setTag(Object)
} on a View given to this class will result in
excessive allocations and and/or IllegalArgumentException
s. If you must call View.setTag(Object)
on a view, consider using BaseTarget
or SimpleTarget
instead.
Subclasses must call super in onLoadCleared(Drawable)
Modifier and Type | Field and Description |
---|---|
protected T |
view |
SIZE_ORIGINAL
Constructor and Description |
---|
ViewTarget(T view)
Constructor that defaults
waitForLayout to false . |
ViewTarget(T view,
boolean waitForLayout)
Deprecated.
Use
waitForLayout() instead. |
Modifier and Type | Method and Description |
---|---|
ViewTarget<T,Z> |
clearOnDetach()
|
Request |
getRequest()
Returns any stored request using
View.getTag() . |
void |
getSize(SizeReadyCallback cb)
Determines the size of the view by first checking
View.getWidth() and
View.getHeight() . |
T |
getView()
Returns the wrapped
View . |
void |
onLoadCleared(Drawable placeholder)
A lifecycle callback that is called when a load is cancelled and its resources are freed.
|
void |
onLoadStarted(Drawable placeholder)
A lifecycle callback that is called when a load is started.
|
void |
removeCallback(SizeReadyCallback cb)
Removes the given callback from the pending set if it's still retained.
|
void |
setRequest(Request request)
Stores the request using
View.setTag(Object) . |
static void |
setTagId(int tagId)
Sets the android resource id to use in conjunction with
View.setTag(int, Object)
to store temporary state allowing loads to be automatically cancelled and resources re-used
in scrolling lists. |
String |
toString() |
ViewTarget<T,Z> |
waitForLayout()
Indicates that Glide should always wait for any pending layout pass before checking
for the size an
View . |
onDestroy, onLoadFailed, onStart, onStop
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
onResourceReady
public ViewTarget(T view)
waitForLayout
to false
.@Deprecated public ViewTarget(T view, boolean waitForLayout)
waitForLayout()
instead.waitForLayout
- If set to true
, Glide will always wait for any pending layout pass
before checking for the size a View. If set to false
Glide will only wait for a pending
layout pass if it's unable to resolve the size from layout parameters or an existing View size.
Because setting this parameter to true
forces Glide to wait for the layout pass to
occur before starting the load, setting this parameter to true
can cause flashing in
some cases and should be used sparingly. If layout parameters are set to fixed sizes, they will
still be used instead of the View's dimensions even if this parameter is set to true
.
This parameter is a fallback only.public final ViewTarget<T,Z> clearOnDetach()
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.
public final ViewTarget<T,Z> waitForLayout()
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.
@CallSuper public void onLoadStarted(@Nullable Drawable placeholder)
Target
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.
You must ensure that any current Drawable received in Target.onResourceReady(Object,
Transition)
is no longer displayed before redrawing the container (usually a View) or
changing its visibility.
onLoadStarted
in interface Target<Z>
onLoadStarted
in class BaseTarget<Z>
placeholder
- The placeholder drawable to optionally show, or null.@CallSuper public void getSize(SizeReadyCallback cb)
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.cb
- The callback that must be called when the size of the target has been determined@CallSuper public void removeCallback(SizeReadyCallback cb)
Target
cb
- The callback to remove.@CallSuper public void onLoadCleared(Drawable placeholder)
Target
You must ensure that any current Drawable received in Target.onResourceReady(Object,
Transition)
is no longer displayed before redrawing the container (usually a View) or
changing its visibility.
onLoadCleared
in interface Target<Z>
onLoadCleared
in class BaseTarget<Z>
placeholder
- The placeholder drawable to optionally show, or null.public void setRequest(@Nullable Request request)
View.setTag(Object)
.setRequest
in interface Target<Z>
setRequest
in class BaseTarget<Z>
request
- @Nullable public Request getRequest()
View.getTag()
.
For Glide to function correctly, Glide must be the only thing that calls View.setTag(Object)
. If the tag is cleared or put to another object type, Glide will not be
able to retrieve and cancel previous loads which will not only prevent Glide from reusing
resource, but will also result in incorrect images being loaded and lots of flashing of images
in lists. As a result, this will throw an IllegalArgumentException
if View.getTag()
} returns a non null object that is not an Request
.
getRequest
in interface Target<Z>
getRequest
in class BaseTarget<Z>
public static void setTagId(int tagId)
View.setTag(int, Object)
to store temporary state allowing loads to be automatically cancelled and resources re-used
in scrolling lists.
If no tag id is set, Glide will use View.setTag(Object)
.
Warning: prior to Android 4.0 tags were stored in a static map. Using this method prior
to Android 4.0 may cause memory leaks and isn't recommended. If you do use this method
on older versions, be sure to call RequestManager.clear(View)
on
any view you start a load into to ensure that the static state is removed.
tagId
- The android resource to use.