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

标签 java android html textview spannable

我想在 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/

相关文章:

javascript - 如何让 IE 10 显示指针而不是选择列表的 I-bar?

java - 我想打印除最后一个以外的所有字母,然后是最后两个,然后是最后 3 个

java - 为什么junit测试环境无法创建bean?

Android Jetpack 导航库和 onActivityResult

Java readline() 保持套接字打开

javascript - 如何清除模糊时的无效日期字段? (AngularJS)

javascript - rgba 在 IE8 中不工作如何解决这个问题

Java 字符串输入输出

java - 使用 SQL 查询进行 Spring Boot 组件测试

java - 是否可以使用 Web 服务器 api 中的 MpAndroid 显示图表?