Android 解析Html格式

分为两种:

目录

第一种 我需要的是获取其中的一张照片或者全是文字

第二种 我需要的是全部解析Html 让图文混合


第一种:

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博客

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值