From 05b885422624170dd39c89ef55b802f3b63ffc44 Mon Sep 17 00:00:00 2001 From: Sam Judd Date: Fri, 3 Nov 2017 23:11:43 -0700 Subject: [PATCH] Add a waitForLayout method to ViewTarget to make it easier to use. --- .../request/target/BitmapImageViewTarget.java | 6 ++- .../BitmapThumbnailImageViewTarget.java | 5 +++ .../target/DrawableImageViewTarget.java | 6 ++- .../DrawableThumbnailImageViewTarget.java | 5 +++ .../glide/request/target/ImageViewTarget.java | 5 +++ .../target/ThumbnailImageViewTarget.java | 5 +++ .../glide/request/target/ViewTarget.java | 41 ++++++++++++++++--- 7 files changed, 65 insertions(+), 8 deletions(-) diff --git a/library/src/main/java/com/bumptech/glide/request/target/BitmapImageViewTarget.java b/library/src/main/java/com/bumptech/glide/request/target/BitmapImageViewTarget.java index bf5b9da5ce..386a6a1cf7 100644 --- a/library/src/main/java/com/bumptech/glide/request/target/BitmapImageViewTarget.java +++ b/library/src/main/java/com/bumptech/glide/request/target/BitmapImageViewTarget.java @@ -14,8 +14,12 @@ public BitmapImageViewTarget(ImageView view) { super(view); } + /** + * @deprecated Use {@link #waitForLayout()} instead. + */ // Public API. - @SuppressWarnings("unused") + @SuppressWarnings({"unused", "deprecation"}) + @Deprecated public BitmapImageViewTarget(ImageView view, boolean waitForLayout) { super(view, waitForLayout); } diff --git a/library/src/main/java/com/bumptech/glide/request/target/BitmapThumbnailImageViewTarget.java b/library/src/main/java/com/bumptech/glide/request/target/BitmapThumbnailImageViewTarget.java index 5d5f03eeea..90b03a62fb 100644 --- a/library/src/main/java/com/bumptech/glide/request/target/BitmapThumbnailImageViewTarget.java +++ b/library/src/main/java/com/bumptech/glide/request/target/BitmapThumbnailImageViewTarget.java @@ -15,6 +15,11 @@ public BitmapThumbnailImageViewTarget(ImageView view) { super(view); } + /** + * @deprecated Use {@link #waitForLayout()} instead. + */ + @SuppressWarnings("deprecation") + @Deprecated public BitmapThumbnailImageViewTarget(ImageView view, boolean waitForLayout) { super(view, waitForLayout); } diff --git a/library/src/main/java/com/bumptech/glide/request/target/DrawableImageViewTarget.java b/library/src/main/java/com/bumptech/glide/request/target/DrawableImageViewTarget.java index 524a273d74..ca9c6528d0 100644 --- a/library/src/main/java/com/bumptech/glide/request/target/DrawableImageViewTarget.java +++ b/library/src/main/java/com/bumptech/glide/request/target/DrawableImageViewTarget.java @@ -13,8 +13,12 @@ public DrawableImageViewTarget(ImageView view) { super(view); } + /** + * @deprecated Use {@link #waitForLayout()} instead. + */ // Public API. - @SuppressWarnings("unused") + @SuppressWarnings({"unused", "deprecation"}) + @Deprecated public DrawableImageViewTarget(ImageView view, boolean waitForLayout) { super(view, waitForLayout); } diff --git a/library/src/main/java/com/bumptech/glide/request/target/DrawableThumbnailImageViewTarget.java b/library/src/main/java/com/bumptech/glide/request/target/DrawableThumbnailImageViewTarget.java index 2a105ee01a..40ff2dad43 100644 --- a/library/src/main/java/com/bumptech/glide/request/target/DrawableThumbnailImageViewTarget.java +++ b/library/src/main/java/com/bumptech/glide/request/target/DrawableThumbnailImageViewTarget.java @@ -13,6 +13,11 @@ public DrawableThumbnailImageViewTarget(ImageView view) { super(view); } + /** + * @deprecated Use {@link #waitForLayout()} instead. + */ + @Deprecated + @SuppressWarnings("deprecation") public DrawableThumbnailImageViewTarget(ImageView view, boolean waitForLayout) { super(view, waitForLayout); } diff --git a/library/src/main/java/com/bumptech/glide/request/target/ImageViewTarget.java b/library/src/main/java/com/bumptech/glide/request/target/ImageViewTarget.java index d08d0da7a7..2210547f9d 100644 --- a/library/src/main/java/com/bumptech/glide/request/target/ImageViewTarget.java +++ b/library/src/main/java/com/bumptech/glide/request/target/ImageViewTarget.java @@ -25,6 +25,11 @@ public ImageViewTarget(ImageView view) { super(view); } + /** + * @deprecated Use {@link #waitForLayout()} instead. + */ + @SuppressWarnings({"deprecation"}) + @Deprecated public ImageViewTarget(ImageView view, boolean waitForLayout) { super(view, waitForLayout); } diff --git a/library/src/main/java/com/bumptech/glide/request/target/ThumbnailImageViewTarget.java b/library/src/main/java/com/bumptech/glide/request/target/ThumbnailImageViewTarget.java index 076e166426..043d31daff 100644 --- a/library/src/main/java/com/bumptech/glide/request/target/ThumbnailImageViewTarget.java +++ b/library/src/main/java/com/bumptech/glide/request/target/ThumbnailImageViewTarget.java @@ -28,6 +28,11 @@ public ThumbnailImageViewTarget(ImageView view) { super(view); } + /** + * @deprecated Use {@link #waitForLayout()} insetad. + */ + @Deprecated + @SuppressWarnings({"deprecation"}) public ThumbnailImageViewTarget(ImageView view, boolean waitForLayout) { super(view, waitForLayout); } diff --git a/library/src/main/java/com/bumptech/glide/request/target/ViewTarget.java b/library/src/main/java/com/bumptech/glide/request/target/ViewTarget.java index 00e041656c..47743b9455 100644 --- a/library/src/main/java/com/bumptech/glide/request/target/ViewTarget.java +++ b/library/src/main/java/com/bumptech/glide/request/target/ViewTarget.java @@ -57,7 +57,8 @@ public abstract class ViewTarget extends BaseTarget { * Constructor that defaults {@code waitForLayout} to {@code false}. */ public ViewTarget(T view) { - this(view, false /*waitForLayout*/); + this.view = Preconditions.checkNotNull(view); + sizeDeterminer = new SizeDeterminer(view); } /** @@ -69,12 +70,17 @@ public ViewTarget(T view) { * 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 {@code true}. * This parameter is a fallback only. + * + * @deprecated Use {@link #waitForLayout()} instead. */ // Public API. @SuppressWarnings("WeakerAccess") + @Deprecated public ViewTarget(T view, boolean waitForLayout) { - this.view = Preconditions.checkNotNull(view); - sizeDeterminer = new SizeDeterminer(view, waitForLayout); + this(view); + if (waitForLayout) { + waitForLayout(); + } } /** @@ -124,6 +130,30 @@ public void onViewDetachedFromWindow(View v) { return this; } + /** + * Indicates that Glide should always wait for any pending layout pass before checking + * for the size an {@link View}. + * + *

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

Because calling this method forces Glide to wait for the layout pass to occur before + * starting loads, setting this parameter to {@code 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 {@link LayoutParams} of the wrapped {@link View} are set to fixed sizes, they will + * still be used instead of the {@link View}'s dimensions even if this method is called. This + * parameter is a fallback only. + */ + public final ViewTarget waitForLayout() { + sizeDeterminer.waitForLayout = true; + return this; + } + @CallSuper @Override public void onLoadStarted(@Nullable Drawable placeholder) { @@ -284,14 +314,13 @@ static final class SizeDeterminer { @Nullable static Integer maxDisplayLength; private final View view; - private final boolean waitForLayout; private final List cbs = new ArrayList<>(); + private boolean waitForLayout; @Nullable private SizeDeterminerLayoutListener layoutListener; - SizeDeterminer(View view, boolean waitForLayout) { + SizeDeterminer(View view) { this.view = view; - this.waitForLayout = waitForLayout; } // Use the maximum to avoid depending on the device's current orientation.