Android: Replace lt;imggt;-tag with ImageViews(Android:用 ImageViews 替换 lt;imggt;-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="aHR0cDovL3h4eHhpbWFnZXMvMTA0ODI2OC0xMS0xNDI2MTcwODc2MzE0LmpwZw==" alt="IiAvJmd0OyZsdDticiAvJmd0OyZsdDticiAvJmd0OyZsdDticiAvJmd0OyZsdDsvcCZndDsKJmx0O3AmZ3Q7Sm8sIHZlbGRpZyBteWUgYmxvbXN0ZXIgb2cgZHVmdGVseXMuIGVsdCBmZWlsIHNrLiBMdWtzdXMhJmx0Oy9wJmd0OwombHQ7cCBzdHlsZT0="text-align: center;"><img src="aHR0cDovL3h4eHgvaW1hZ2VzLzEwNDgyNjgtMTEtMTQyNjE3MTAwMDI3Mi5qcGc=" alt="IiAvJmd0OyZsdDsvcCZndDsKJmx0O3AmZ3Q7Jmx0O3N0cm9uZyZndDtIw6VwZXIgZGVyZSBoYXIgZGV0IGhha2tldCBiZWRyZSBlbm4gbWVnLiZsdDsvc3Ryb25nJmd0OyAmbHQ7L3AmZ3Q7CjwvY29kZT48L3ByZT4KPHAgY2xhc3M9"cn">我考虑过使用 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 替换 <img>-tag的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android:用 ImageViews 替换 <img>-tag
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- Android - 拆分 Drawable 2022-01-01