Wait until measured

This commit is contained in:
M66B 2019-10-05 11:15:16 +02:00
parent 5548f2fbdb
commit 6928b18c66
1 changed files with 30 additions and 7 deletions

View File

@ -58,6 +58,8 @@ import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
class ImageHelper {
private static final float MIN_LUMINANCE = 0.33f;
@ -369,7 +371,7 @@ class ImageHelper {
}
}
private static void fitDrawable(Drawable d, AnnotatedSource a, View view) {
private static void fitDrawable(final Drawable d, AnnotatedSource a, final View view) {
Rect bounds = d.getBounds();
int w = bounds.width();
int h = bounds.height();
@ -385,12 +387,33 @@ class ImageHelper {
d.setBounds(0, 0, w, h);
}
float width = view.getWidth();
if (w > width) {
float scale = width / w;
w = Math.round(w * scale);
h = Math.round(h * scale);
d.setBounds(0, 0, w, h);
final Semaphore semaphore = new Semaphore(0);
view.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
view.removeOnLayoutChangeListener(this);
Rect bounds = d.getBounds();
int w = bounds.width();
int h = bounds.height();
float width = view.getWidth();
if (w > width) {
float scale = width / w;
w = Math.round(w * scale);
h = Math.round(h * scale);
d.setBounds(0, 0, w, h);
}
semaphore.release();
}
});
try {
semaphore.tryAcquire(2500, TimeUnit.MILLISECONDS);
} catch (InterruptedException ex) {
Log.e(ex);
}
}