分为两种:
目录
第一种:
1、获取其中的照片放在ImageView里面 我这是用网络上的图片,如果是本地图片 请看别人写的文章:在这(2条消息) Android自动解析html带图片,实现图文混排_android html 图片_我靠_叫我大当家的的博客-CSDN博客
其中current的意思是在Html中会有很多的图片,而我需要放的是第一张照片 那就防止他放最后一张照片 参数source 会循环出所有的照片,所以我不让他循环 只加第一张照片
int current=0;
new Thread(new Runnable() {
@Override
public void run() {
Spanned spanned = Html.fromHtml(newsBean.getContent(), new Html.ImageGetter() {
//参数source 会循环出所有的照片
@Override
public Drawable getDrawable(String source) {
System.out.println(source);
if(current==0){
new Thread(new Runnable() {
@Override
public void run() {
try {
Bitmap bitmap = BitmapFactory.decodeStream(new URL(source).openStream());
runOnUiThread(new Runnable() {
@Override
public void run() {
holder.imageView.setImageBitmap(bitmap);
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
current++;
return null;
}
},null);
}
}).start();
2、获取Html中所有的文字 简单来说就是纯文字 使用正则的方法 相等时就为空
String regFormat = "\\s*|\t|\r|\n";
String regTag = "<[^>]*>";
//newsBean.getCountent() 就是你的Html的格式
String content = newsBean.getContent().replaceAll(regFormat, "").replaceAll(regTag, "");
参考文章:(3条消息) Android提取HTML代码内的所有文字内容_toaksg的博客-CSDN博客
第二种 :
图文混合 自定义类 直接使用就可以了
public class MImageGetter implements Html.ImageGetter {
private Context c;
private TextView container;
public MImageGetter(TextView text, Context c) {
this.c = c;
this.container = text;
}
@Override
public Drawable getDrawable(String source) {
final LevelListDrawable drawable = new LevelListDrawable();
new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new URL(source);
Bitmap bitmap = BitmapFactory.decodeStream(url.openStream());
runOnUiThread(new Runnable() {
@Override
public void run() {
if(bitmap != null) {
BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
drawable.addLevel(1, 1, bitmapDrawable);
drawable.setBounds(0, 0, bitmap.getWidth(),bitmap.getHeight());
drawable.setLevel(1);
container.invalidate();
container.setText(container.getText());
}
}
});
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
return drawable;
}
}
使用
textView.setText(Html.fromHtml(extras.getString("content"),new MImageGetter(textView,this),null));
参考文章: (2条消息) Android自动解析html带图片,实现图文混排_android html 图片_我靠_叫我大当家的的博客-CSDN博客