Simplified fitDrawable

This commit is contained in:
M66B 2020-04-30 10:50:08 +02:00
parent 4daedb9f97
commit 3fd2151655
1 changed files with 30 additions and 43 deletions

View File

@ -46,6 +46,8 @@ import android.text.TextUtils;
import android.util.Base64; import android.util.Base64;
import android.util.DisplayMetrics; import android.util.DisplayMetrics;
import android.view.View; import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.TextView; import android.widget.TextView;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
@ -69,8 +71,6 @@ import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.Date; import java.util.Date;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
class ImageHelper { class ImageHelper {
private static final ExecutorService executor_1 = private static final ExecutorService executor_1 =
@ -80,7 +80,6 @@ class ImageHelper {
private static final int DOWNLOAD_TIMEOUT = 15 * 1000; // milliseconds private static final int DOWNLOAD_TIMEOUT = 15 * 1000; // milliseconds
private static final int MAX_REDIRECTS = 10; private static final int MAX_REDIRECTS = 10;
private static final long FIT_DRAWABLE_TIMEOUT = 10 * 1000L; // milliseconds
private static final int SLOW_CONNECTION = 2 * 1024; // Kbps private static final int SLOW_CONNECTION = 2 * 1024; // Kbps
static Bitmap generateIdenticon(@NonNull String email, int size, int pixels, Context context) { static Bitmap generateIdenticon(@NonNull String email, int size, int pixels, Context context) {
@ -443,51 +442,39 @@ 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 (!view.isAttachedToWindow()) Rect bounds = d.getBounds();
return; int w = bounds.width();
int h = bounds.height();
Semaphore semaphore = new Semaphore(0); 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);
view.post(new Runnable() { if (a.width != 0 && a.height != 0) {
@Override w = Helper.dp2pixels(view.getContext(), a.width);
public void run() { h = Helper.dp2pixels(view.getContext(), a.height);
Rect bounds = d.getBounds(); d.setBounds(0, 0, w, h);
int w = bounds.width(); }
int h = bounds.height();
if (a.width == 0 && a.height != 0) float width = view.getContext().getResources().getDisplayMetrics().widthPixels;
a.width = Math.round(a.height * w / (float) h); View v = view;
if (a.height == 0 && a.width != 0) while (v != null) {
a.height = Math.round(a.width * h / (float) w); width -= v.getPaddingStart() + v.getPaddingEnd();
if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
if (a.width != 0 && a.height != 0) { ViewGroup.MarginLayoutParams lparam = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
w = Helper.dp2pixels(view.getContext(), a.width); width -= lparam.leftMargin + lparam.rightMargin;
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);
}
semaphore.release();
} }
});
try { ViewParent parent = v.getParent();
long now = new Date().getTime(); v = (parent instanceof View ? (View) parent : null);
long start = now; }
while (view.isAttachedToWindow() && now - start < FIT_DRAWABLE_TIMEOUT) {
if (semaphore.tryAcquire(1000, TimeUnit.MILLISECONDS)) if (w > width) {
break; float scale = width / w;
now = new Date().getTime(); w = Math.round(w * scale);
} h = Math.round(h * scale);
} catch (InterruptedException ex) { d.setBounds(0, 0, w, h);
Log.w(ex);
} }
} }