Android:用ImageViews替换< img> -tag [英] Android: Replace <img>-tag with ImageViews
问题描述
我在TextView中使用Html.fromHtml显示一些HTML,效果很好。
I'm showing a bit of HTML in a TextView using Html.fromHtml, which works great.
有时候这个html会有img-tags,而且我' d也想显示这些。
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个ImageViews(和用毕加索填充它们)。这样可以正常工作,但图像始终位于文本的顶部或底部。
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替换< img> -tag的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!