Skip to content
This repository has been archived by the owner on Apr 30, 2024. It is now read-only.

Fixes #6 Defer load until first layout pass #16

Merged
merged 1 commit into from
Dec 2, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import android.graphics.Rect;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.ViewTreeObserver;
import java.io.File;
import java.io.OutputStream;
import java.util.concurrent.Future;
Expand Down Expand Up @@ -67,12 +68,38 @@ public LoadRequest using(@Nullable BitmapLoader bitmapLoader) {
* @param model Model used by {@link BitmapLoader} to load desired {@link Bitmap}
*/
public void load(@Nullable Object model) {
if (cropView.getWidth() == 0 && cropView.getHeight() == 0) {
// Defer load until layout pass
deferLoad(model);
return;
}
performLoad(model);
}

void performLoad(Object model) {
if (bitmapLoader == null) {
bitmapLoader = resolveBitmapLoader(cropView);
}

bitmapLoader.load(model, cropView);
}

void deferLoad(final Object model) {
if (!cropView.getViewTreeObserver().isAlive()) {
return;
}
cropView.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (cropView.getViewTreeObserver().isAlive()) {
//noinspection deprecation
cropView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
performLoad(model);
}
}
);
}
}

public static class CropRequest {
Expand Down