android: fix when load image from remote server cause non ui thread touch view (#561)

This commit is contained in:
osborn 2022-12-10 14:25:36 +08:00 committed by GitHub
parent 9be6bb2051
commit 0668e0fe86
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -32,16 +32,12 @@ import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.NinePatchDrawable;
import android.os.Build;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Pair;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.vectordrawable.graphics.drawable.Animatable2Compat;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.DataSource;
@ -65,6 +61,10 @@ import java.io.File;
import java.nio.ByteBuffer;
import java.security.MessageDigest;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.vectordrawable.graphics.drawable.Animatable2Compat;
import pub.doric.DoricContext;
import pub.doric.async.AsyncResult;
import pub.doric.extension.bridge.DoricMethod;
@ -428,15 +428,33 @@ public class ImageNode extends ViewNode<ImageView> {
if (doricResource != null) {
doricResource.fetch().setCallback(new AsyncResult.Callback<byte[]>() {
@Override
public void onResult(byte[] imageData) {
loadIntoTarget(Glide.with(getContext()).load(imageData));
public void onResult(final byte[] imageData) {
if (Looper.getMainLooper() != Looper.myLooper()) {
mView.post(new Runnable() {
@Override
public void run() {
loadIntoTarget(Glide.with(getContext()).load(imageData));
}
});
} else {
loadIntoTarget(Glide.with(getContext()).load(imageData));
}
}
@Override
public void onError(Throwable t) {
t.printStackTrace();
DoricLog.e("Cannot load resource %s, %s", resource.toString(), t.getLocalizedMessage());
view.setImageDrawable(null);
if (Looper.getMainLooper() != Looper.myLooper()) {
mView.post(new Runnable() {
@Override
public void run() {
view.setImageDrawable(null);
}
});
} else {
view.setImageDrawable(null);
}
}
@Override