获取远程文章内容时,显示图片的两种方式

第一种:

通过Html.fromHtml(String,ImageGetter,tagHandler)

CharSequence text = Html.fromHtml(capter, new ImageGetter() {                    
                    @Override
                    public Drawable getDrawable(String source) {
                        Drawable drawable = null;
                        try {
                            drawable = Drawable.createFromStream(new URL(source).openStream(),"image");//从远程获取图片
                            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());//要设置边界信息  Drawable本身是没有边界信息的
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        return drawable;
                }}, null);

capter_view= (TextView)findViewById(R.id.bookcapter);

capter_view.setText(text);//就可以显示出img的图片信息了(这些操作都不要放在UI线程里做,因为图片下载可能比较费时。)

第二种:

这个需要服务端配合,服务端需要将图片和文字信息在服务端分割开,同时给index来标示位置(为了解决内容分割后不会交叉,客户端可以按index按原来的顺序来排列显示)

服务端返回的是以图片img标签分割的json数组(这个操作android端应该也可以做)

客户端遍历json数组显示

如果是img的类型则创建ImageView来显示img

如果是text的类型则创建TextView来显示text

这块操作可以封装成 继承LinearLayout 的自定义TestView控件

然后把json数组转成List<HashMap<key,value>> datas;

模拟TextView的setText方法,我们也可以自定义一个setText(),当然你也可以定义其他名称,这个方法不是重写,所以随便自定义。

在这个方法里遍历数据生成ImageView或TextView

最后通过调用TestView.setText(datas);

生成ImageView后,把ImageView传给新线程,在线程里远程获取图片drawable后,再ImageView.setImageDrawable(drawable)

 

原文地址:https://www.cnblogs.com/jshen/p/4008062.html