Android: Replace -

2019-05-19 06:36发布

问题:

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

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

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

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>

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.

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?

EDIT: 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.

回答1:

I solved it using this:

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);
        }
    }
}