T
- The specific subclass of view wrapped by this target (e.g. ImageView
)Z
- The resource type this target will receive (e.g. Bitmap
).public abstract class CustomViewTarget<T extends android.view.View,Z> extends java.lang.Object implements Target<Z>
Target
for loading resources (Bitmap
, Drawable
etc) into View
s that provides default implementations for most methods and can determine
the size of views using a ViewTreeObserver.OnDrawListener
.Modifier and Type | Field and Description |
---|---|
protected T |
view |
SIZE_ORIGINAL
Constructor and Description |
---|
CustomViewTarget(T view)
Constructor that defaults
waitForLayout to false . |
Modifier and Type | Method and Description |
---|---|
CustomViewTarget<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 |
onDestroy()
Callback for when
Fragment.onDestroy() } or Activity.onDestroy() is called. |
void |
onLoadCleared(android.graphics.drawable.Drawable placeholder)
A mandatory lifecycle callback that is called when a load is cancelled and its resources
are freed.
|
void |
onLoadStarted(android.graphics.drawable.Drawable placeholder)
A lifecycle callback that is called when a load is started.
|
protected abstract void |
onResourceCleared(android.graphics.drawable.Drawable placeholder)
A required callback invoked when the resource is no longer valid and must be freed.
|
protected void |
onResourceLoading(android.graphics.drawable.Drawable placeholder)
An optional callback invoked when a resource load is started.
|
void |
onStart()
Callback for when
Fragment.onStart() } or Activity.onStart() is called. |
void |
onStop()
Callback for when
Fragment.onStop() } or Activity.onStop() } is called. |
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) . |
java.lang.String |
toString() |
CustomViewTarget<T,Z> |
useTagId(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.
|
CustomViewTarget<T,Z> |
waitForLayout()
Indicates that Glide should always wait for any pending layout pass before checking for the
size an
View . |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
onLoadFailed, onResourceReady
protected final T extends android.view.View view
public CustomViewTarget(@NonNull T view)
waitForLayout
to false
.protected abstract void onResourceCleared(@Nullable android.graphics.drawable.Drawable placeholder)
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.
placeholder
- The placeholder drawable to optionally show, or null.protected void onResourceLoading(@Nullable android.graphics.drawable.Drawable placeholder)
placeholder
- The placeholder drawable to optionally show, or null.Target.onLoadStarted(Drawable)
public void onStart()
LifecycleListener
Fragment.onStart()
} or Activity.onStart()
is called.onStart
in interface LifecycleListener
public void onStop()
LifecycleListener
Fragment.onStop()
} or Activity.onStop()
} is called.onStop
in interface LifecycleListener
public void onDestroy()
LifecycleListener
Fragment.onDestroy()
} or Activity.onDestroy()
is called.onDestroy
in interface LifecycleListener
@NonNull public final CustomViewTarget<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.
@NonNull public final CustomViewTarget<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.
@Deprecated public final CustomViewTarget<T,Z> useTagId(@IdRes int tagId)
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.
tagId
- The android resource id to use.@NonNull public final T getView()
View
.public final void getSize(@NonNull 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.public final void removeCallback(@NonNull SizeReadyCallback cb)
Target
removeCallback
in interface Target<Z>
cb
- The callback to remove.public final void onLoadStarted(@Nullable android.graphics.drawable.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.
onLoadStarted
in interface Target<Z>
placeholder
- The placeholder drawable to optionally show, or null.public final void onLoadCleared(@Nullable android.graphics.drawable.Drawable placeholder)
Target
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.
onLoadCleared
in interface Target<Z>
placeholder
- The placeholder drawable to optionally show, or null.public final void setRequest(@Nullable Request request)
View.setTag(Object)
.setRequest
in interface Target<Z>
request
- @Nullable public final Request getRequest()
View.getTag()
.getRequest
in interface Target<Z>
public java.lang.String toString()
toString
in class java.lang.Object