This commit is contained in:
M66B 2020-01-25 18:56:53 +01:00
parent c6d38bf032
commit 3249068a8a
1 changed files with 32 additions and 41 deletions

View File

@ -39,7 +39,6 @@ import android.graphics.drawable.Drawable;
import android.graphics.drawable.LevelListDrawable; import android.graphics.drawable.LevelListDrawable;
import android.os.Build; import android.os.Build;
import android.os.Handler; import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Base64; import android.util.Base64;
import android.util.DisplayMetrics; import android.util.DisplayMetrics;
@ -392,51 +391,43 @@ class ImageHelper {
} }
private static void fitDrawable(final Drawable d, final AnnotatedSource a, final View view) { private static void fitDrawable(final Drawable d, final AnnotatedSource a, final View view) {
if (Looper.getMainLooper().getThread() == Thread.currentThread()) { Semaphore semaphore = new Semaphore(0);
Log.w("fitDrawable UI thread");
_fitDrawble(d, a, view);
} else {
Semaphore semaphore = new Semaphore(0);
view.post(new Runnable() { view.post(new Runnable() {
@Override @Override
public void run() { public void run() {
_fitDrawble(d, a, view); Rect bounds = d.getBounds();
semaphore.release(); int w = bounds.width();
int h = bounds.height();
if (a.width == 0 && a.height != 0)
a.width = Math.round(a.height * w / (float) h);
if (a.height == 0 && a.width != 0)
a.height = Math.round(a.width * h / (float) w);
if (a.width != 0 && a.height != 0) {
w = Helper.dp2pixels(view.getContext(), a.width);
h = Helper.dp2pixels(view.getContext(), a.height);
d.setBounds(0, 0, w, h);
} }
});
try { float width = view.getWidth();
if (!semaphore.tryAcquire(FIT_DRAWABLE_TIMEOUT, TimeUnit.MILLISECONDS)) if (w > width) {
Log.e("fitDrawable failed timeout=" + FIT_DRAWABLE_TIMEOUT); float scale = width / w;
} catch (InterruptedException ex) { w = Math.round(w * scale);
Log.w(ex); h = Math.round(h * scale);
d.setBounds(0, 0, w, h);
}
semaphore.release();
} }
} });
}
private static void _fitDrawble(Drawable d, AnnotatedSource a, View view) { try {
Rect bounds = d.getBounds(); if (!semaphore.tryAcquire(FIT_DRAWABLE_TIMEOUT, TimeUnit.MILLISECONDS))
int w = bounds.width(); Log.e("fitDrawable failed timeout=" + FIT_DRAWABLE_TIMEOUT);
int h = bounds.height(); } catch (InterruptedException ex) {
Log.w(ex);
if (a.width == 0 && a.height != 0)
a.width = Math.round(a.height * w / (float) h);
if (a.height == 0 && a.width != 0)
a.height = Math.round(a.width * h / (float) w);
if (a.width != 0 && a.height != 0) {
w = Helper.dp2pixels(view.getContext(), a.width);
h = Helper.dp2pixels(view.getContext(), a.height);
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);
} }
} }