android: support image in text.htmltext

This commit is contained in:
pengfei.zhou 2021-10-15 10:43:51 +08:00 committed by osborn
parent 3969e13d8a
commit ea79ea953a
2 changed files with 47 additions and 5 deletions

View File

@ -24,6 +24,7 @@ import android.graphics.Typeface;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.text.Html; import android.text.Html;
import android.text.Layout; import android.text.Layout;
import android.text.Spanned;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.TypedValue; import android.util.TypedValue;
import android.view.Gravity; import android.view.Gravity;
@ -32,15 +33,20 @@ import android.widget.TextView;
import androidx.core.content.res.ResourcesCompat; import androidx.core.content.res.ResourcesCompat;
import com.bumptech.glide.Glide;
import com.github.pengfeizhou.jscore.JSObject; import com.github.pengfeizhou.jscore.JSObject;
import com.github.pengfeizhou.jscore.JSValue; import com.github.pengfeizhou.jscore.JSValue;
import java.util.concurrent.Callable;
import pub.doric.DoricContext; import pub.doric.DoricContext;
import pub.doric.async.AsyncResult;
import pub.doric.extension.bridge.DoricPlugin; import pub.doric.extension.bridge.DoricPlugin;
import pub.doric.shader.richtext.CustomTagHandler; import pub.doric.shader.richtext.CustomTagHandler;
import pub.doric.shader.richtext.HtmlParser; import pub.doric.shader.richtext.HtmlParser;
import pub.doric.utils.DoricLog; import pub.doric.utils.DoricLog;
import pub.doric.utils.DoricUtils; import pub.doric.utils.DoricUtils;
import pub.doric.utils.ThreadMode;
/** /**
* @Description: widget * @Description: widget
@ -277,15 +283,51 @@ public class TextNode extends ViewNode<TextView> {
break; break;
case "htmlText": case "htmlText":
if (prop.isString()) { if (prop.isString()) {
view.setText( getDoricContext().getDriver().asyncCall(new Callable<Spanned>() {
HtmlParser.buildSpannedText(prop.asString().value(), @Override
public Spanned call() {
return HtmlParser.buildSpannedText(prop.asString().value(),
new Html.ImageGetter() { new Html.ImageGetter() {
@Override @Override
public Drawable getDrawable(String source) { 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; return null;
} }
}, },
new CustomTagHandler())); new CustomTagHandler());
}
}, ThreadMode.INDEPENDENT)
.setCallback(new AsyncResult.Callback<Spanned>() {
@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; break;
case "truncateAt": case "truncateAt":

View File

@ -30,12 +30,12 @@ import java.util.Stack;
* @CreateDate: 2020-04-14 * @CreateDate: 2020-04-14
*/ */
public class CustomTagHandler implements HtmlParser.TagHandler { public class CustomTagHandler implements HtmlParser.TagHandler {
private Stack<Integer> startIndex = new Stack<>(); private final Stack<Integer> startIndex = new Stack<>();
/** /**
* html attribute valuelike:<size value='16'></size> * html attribute valuelike:<size value='16'></size>
*/ */
private Stack<String> propertyValue = new Stack<>(); private final Stack<String> propertyValue = new Stack<>();
@Override @Override
public boolean handleTag(boolean opening, String tag, Editable output, Attributes attributes) { public boolean handleTag(boolean opening, String tag, Editable output, Attributes attributes) {