gpt4 book ai didi

java - Android TextView 设置 HTML 图像后没有正方形的图像

转载 作者:行者123 更新时间:2023-11-28 03:00:22 24 4
gpt4 key购买 nike

我想在 TextView 中显示带有图像的格式化文本。
我是使用 Spannable 完成的。

TextView tv = new TextView(context);
tv.setTextIsSelectable(true);
Spannable spannedText = Spannable.Factory.getInstance().newSpannable(
Html.fromHtml(content, null, null));
tv.setText(removeUnderlines(spannedText));
tv.setMovementMethod(ArrowKeyMovementMethod.getInstance());
txtText.setMovementMethod(LinkMovementMethod.getInstance());

private Spannable removeUnderlines(Spannable p_Text) {
URLSpan[] spans = p_Text.getSpans(0, p_Text.length(), URLSpan.class);
for (URLSpan span : spans) {
int start = p_Text.getSpanStart(span);
int end = p_Text.getSpanEnd(span);
p_Text.removeSpan(span);
span = new URLSpanNoUnderline(span.getURL());
p_Text.setSpan(span, start, end, 0);
}
return p_Text;
}
public class URLSpanNoUnderline extends URLSpan {
public URLSpanNoUnderline(String p_Url) {
super(p_Url);
}
public void updateDrawState(TextPaint p_DrawState) {
super.updateDrawState(p_DrawState);
p_DrawState.setColor(ContextCompat.getColor(getContext(), R.color.dark_green));
p_DrawState.setUnderlineText(false);
}
}

看起来像这样:

enter image description here


问题是 ">[cyan square] after Image.


编辑:
HTML:

<p style="text-align: justify;">
<a href="http://star.korupciya.com/wp-content/uploads/2016/01/medium_76f2c93d933be9d3c89d0fdccaf0f3d3.jpeg">
<img alt="medium_76f2c93d933be9d3c89d0fdccaf0f3d3" class="alignnone size-full wp-image-58967" height="420" src="http://star.korupciya.com/wp-content/uploads/2016/01/medium_76f2c93d933be9d3c89d0fdccaf0f3d3.jpeg" width="630" /></a>
</p>
<p style="text-align: justify;">
<strong><span style="color: #000000;"><span style="font-size: 16px;"><span style="font-family: georgia,serif;">14. Рассел Бренд &#8211; 1000</span></span></span></strong>
</p>
<p style="text-align: justify;">
<span style="color: #000000;"><span style="font-size: 16px;"><span style="font-family: georgia,serif;">Цей актор дійсно страждає від сексуальної залежності, він навіть намагався вилікуватися від цієї недуги. Рассел вже встиг переспати з такими знаменитостями, як Кеті Перрі, Кейт Мосс, Джері Холліуелл і ще 1 000 менш відомих жінок.</span></span></span>
</p>

最佳答案

为了从 HTML 在 TextView 中绘制图像,我覆盖了 ImageGetter
首先实现Html.ImageGetter接口(interface)并添加这段代码:

private TextView tv_content;//Your TextView
@Override
public Drawable getDrawable(String source) {
source = source.replaceAll("\\\\\"", "");
Log.e(TAG, "getDrawable: " + source);

final LevelListDrawable levelListDrawable = new LevelListDrawable();
//R.drawable.cloud - temp Image, could be any drawable
Drawable empty = ContextCompat.getDrawable(getActivity(), R.drawable.cloud);
levelListDrawable.addLevel(0, 0, empty);
levelListDrawable.setBounds(0, 0, empty.getIntrinsicWidth(), empty.getIntrinsicHeight());

final int w = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 400, getResources().getDisplayMetrics());
final int h = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200, getResources().getDisplayMetrics());

if (RootActivity.userSettings.displayImages)
Glide.with(getActivity())
.load(source)
.asBitmap()
.into(new SimpleTarget<Bitmap>(w, h) {
@Override
public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
try {
levelListDrawable.addLevel(1, 1, new BitmapDrawable(getResources(), bitmap));
levelListDrawable.setLevel(1);
levelListDrawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
tv_content.invalidate();
tv_content.setHeight(tv_content.getHeight() + bitmap.getHeight());
tv_content.setEllipsize(null);
tv_content.invalidate();
} catch (Exception e) {
e.printStackTrace();
}
}
});
return levelListDrawable;
}
private Spannable removeUnderlines(Spannable p_Text) {
URLSpan[] spans = p_Text.getSpans(0, p_Text.length(), URLSpan.class);
for (URLSpan span : spans) {
int start = p_Text.getSpanStart(span);
int end = p_Text.getSpanEnd(span);
p_Text.removeSpan(span);
span = new URLSpanNoUnderline(span.getURL());
p_Text.setSpan(span, start, end, 0);
}
return p_Text;
}
public class URLSpanNoUnderline extends URLSpan {
public URLSpanNoUnderline(String p_Url) {
super(p_Url);
}
public void updateDrawState(TextPaint p_DrawState) {
super.updateDrawState(p_DrawState);
//Link color
p_DrawState.setColor(ContextCompat.getColor(getActivity(), R.color.dark_green));
p_DrawState.setUnderlineText(false);
}
}

那么你应该这样做:

tv_content.setMovementMethod(LinkMovementMethod.getInstance());

Spannable spannedText = Spannable.Factory.getInstance().newSpannable(
Html.fromHtml(yourHtmlString, this, null));
tv_content.setText(removeUnderlines(spannedText));

关于java - Android TextView 设置 HTML 图像后没有正方形的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34922790/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com