Skip to content

Commit

Permalink
Add a waitForLayout method to ViewTarget to make it easier to use.
Browse files Browse the repository at this point in the history
  • Loading branch information
sjudd committed Nov 23, 2017
1 parent 9b07412 commit 05b8854
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public abstract class ViewTarget<T extends View, Z> extends BaseTarget<Z> {
* 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);
}

/**
Expand All @@ -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();
}
}

/**
Expand Down Expand Up @@ -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}.
*
* <p>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()}.
*
* <p>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.
*
* <p>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<T, Z> waitForLayout() {
sizeDeterminer.waitForLayout = true;
return this;
}

@CallSuper
@Override
public void onLoadStarted(@Nullable Drawable placeholder) {
Expand Down Expand Up @@ -284,14 +314,13 @@ static final class SizeDeterminer {
@Nullable
static Integer maxDisplayLength;
private final View view;
private final boolean waitForLayout;
private final List<SizeReadyCallback> 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.
Expand Down

0 comments on commit 05b8854

Please sign in to comment.