Android:用 ImageViews 替换 <img>-tag [英] Android: Replace <img>-tag with ImageViews

查看:28
本文介绍了Android:用 ImageViews 替换 <img>-tag的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Html.fromHtml 在 TextView 中显示一些 HTML,效果很好.

I'm showing a bit of HTML in a TextView using Html.fromHtml, which works great.

有时这个 html 会有 img-tags,我也想显示这些.

Sometimes this html will have img-tags, and I'd also like to display these.

我试过 Android HTML ImageGetter as AsyncTask 但这似乎很慢且不可预测(大小图片).

I tried Android HTML ImageGetter as AsyncTask but this seemed slow and unpredictable (size of the image).

示例 HTML(可能不同...):

Example HTML (it can differ...):

<h2>title</h2><br /><p>TEXT</p>
<p style="text-align: center;">Anyways, fokea?.<img src="http://xxxximages/1048268-11-1426170876314.jpg" alt="" /><br /><br /><br /></p>
<p>Jo, veldig mye blomster og duftelys. elt feil sk. Luksus!</p>
<p style="text-align: center;"><img src="http://xxxx/images/1048268-11-1426171000272.jpg" alt="" /></p>
<p><strong>Håper dere har det hakket bedre enn meg.</strong> </p>

我考虑过使用 JSOUP 来获取所有 img-tags,然后创建 X 数量的 ImageView(并用 Picasso 填充它们).这工作正常,但图像总是在文本的顶部或底部.

I thought about using JSOUP to get all img-tags, and then create X amounts of ImageViews (and populating them with Picasso). This works OK, but the images are always either on top or the bottom of text.

替换每个 img-tag 的最佳解决方案是什么,并根据文本在正确的位置为每个 img-tag 创建一个新的 ImageView?

What would be the best solution for replacing every img-tag and for each one, at the correct place according to the text, create a new ImageView?

好吧,既然没有人有任何建议,我就做了这个又快又脏的.

Well, since no-one had any suggestions, I made this quick and dirty one.

ArrayList<String> lines = new ArrayList<>();
    Scanner scanner = new Scanner(content);
    while (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        lines.add(line);
    }
    scanner.close();


 for(int i = 0; i < lines.size(); i++) {
        Document doc = Jsoup.parse(lines.get(i));
        Elements imgs = doc.select("img");
        if(imgs.size() == 0) {
            TextView textView = new TextView(this);
            textView.setText(Html.fromHtml(lines.get(i)));
            maincontainer.addView(textView);
        } else {
            for(Element el : imgs) {
                Element img = el.select("img").first();
                String image = img.absUrl("src");
                ImageView imageView = new ImageView(this);
                imageView.setPadding(0, 10, 0, 10);
                imageView.setAdjustViewBounds(true);
                Picasso.with(getApplicationContext()).load(image).into(imageView);
                maincontainer.addView(imageView);
            }
        }
    }

虽然我确信代码远非最佳,但它看起来和工作起来都很棒.

It looks and works fantastic, although I'm sure the code is far from optimal.

推荐答案

我用这个解决了:

ArrayList<String> lines = new ArrayList<>();
Scanner scanner = new Scanner(content);
while (scanner.hasNextLine()) {
    String line = scanner.nextLine();
    lines.add(line);
}
scanner.close();

for(int i = 0; i < lines.size(); i++) {
    Document doc = Jsoup.parse(lines.get(i));
    Elements imgs = doc.select("img");
    if(imgs.size() == 0) {
        TextView textView = new TextView(this);
        textView.setText(Html.fromHtml(lines.get(i)));
        maincontainer.addView(textView);
    } else {
        for(Element el : imgs) {
            Element img = el.select("img").first();
            String image = img.absUrl("src");
            ImageView imageView = new ImageView(this);
            imageView.setPadding(0, 10, 0, 10);
            imageView.setAdjustViewBounds(true);
            Picasso.with(getApplicationContext()).load(image).into(imageView);
            maincontainer.addView(imageView);
        }
    }
}

这篇关于Android:用 ImageViews 替换 &lt;img&gt;-tag的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆