diff --git a/app/src/main/java/eu/faircode/email/ImageHelper.java b/app/src/main/java/eu/faircode/email/ImageHelper.java index 68a3dd6161..9fc01b1fc5 100644 --- a/app/src/main/java/eu/faircode/email/ImageHelper.java +++ b/app/src/main/java/eu/faircode/email/ImageHelper.java @@ -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); } }