From ea79ea953a55c66994ce3712410f5c8190753e27 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Fri, 15 Oct 2021 10:43:51 +0800 Subject: [PATCH] android: support image in text.htmltext --- .../main/java/pub/doric/shader/TextNode.java | 48 +++++++++++++++++-- .../shader/richtext/CustomTagHandler.java | 4 +- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/doric-android/doric/src/main/java/pub/doric/shader/TextNode.java b/doric-android/doric/src/main/java/pub/doric/shader/TextNode.java index d7f23200..756c93b7 100644 --- a/doric-android/doric/src/main/java/pub/doric/shader/TextNode.java +++ b/doric-android/doric/src/main/java/pub/doric/shader/TextNode.java @@ -24,6 +24,7 @@ import android.graphics.Typeface; import android.graphics.drawable.Drawable; import android.text.Html; import android.text.Layout; +import android.text.Spanned; import android.text.TextUtils; import android.util.TypedValue; import android.view.Gravity; @@ -32,15 +33,20 @@ import android.widget.TextView; import androidx.core.content.res.ResourcesCompat; +import com.bumptech.glide.Glide; import com.github.pengfeizhou.jscore.JSObject; import com.github.pengfeizhou.jscore.JSValue; +import java.util.concurrent.Callable; + import pub.doric.DoricContext; +import pub.doric.async.AsyncResult; import pub.doric.extension.bridge.DoricPlugin; import pub.doric.shader.richtext.CustomTagHandler; import pub.doric.shader.richtext.HtmlParser; import pub.doric.utils.DoricLog; import pub.doric.utils.DoricUtils; +import pub.doric.utils.ThreadMode; /** * @Description: widget @@ -277,15 +283,51 @@ public class TextNode extends ViewNode { break; case "htmlText": if (prop.isString()) { - view.setText( - HtmlParser.buildSpannedText(prop.asString().value(), + getDoricContext().getDriver().asyncCall(new Callable() { + @Override + public Spanned call() { + return HtmlParser.buildSpannedText(prop.asString().value(), new Html.ImageGetter() { @Override public Drawable getDrawable(String source) { + try { + Drawable drawable = Glide.with(view) + .asDrawable() + .load(source) + .submit() + .get(); + drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); + return drawable; + } catch (Exception e) { + e.printStackTrace(); + } return null; } }, - new CustomTagHandler())); + new CustomTagHandler()); + } + }, ThreadMode.INDEPENDENT) + .setCallback(new AsyncResult.Callback() { + @Override + public void onResult(final Spanned result) { + view.post(new Runnable() { + @Override + public void run() { + view.setText(result); + } + }); + } + + @Override + public void onError(Throwable t) { + + } + + @Override + public void onFinish() { + + } + }); } break; case "truncateAt": diff --git a/doric-android/doric/src/main/java/pub/doric/shader/richtext/CustomTagHandler.java b/doric-android/doric/src/main/java/pub/doric/shader/richtext/CustomTagHandler.java index e47d9ddc..ae254de3 100644 --- a/doric-android/doric/src/main/java/pub/doric/shader/richtext/CustomTagHandler.java +++ b/doric-android/doric/src/main/java/pub/doric/shader/richtext/CustomTagHandler.java @@ -30,12 +30,12 @@ import java.util.Stack; * @CreateDate: 2020-04-14 */ public class CustomTagHandler implements HtmlParser.TagHandler { - private Stack startIndex = new Stack<>(); + private final Stack startIndex = new Stack<>(); /** * html attribute value,like: */ - private Stack propertyValue = new Stack<>(); + private final Stack propertyValue = new Stack<>(); @Override public boolean handleTag(boolean opening, String tag, Editable output, Attributes attributes) {