android富文本 加载带图片的html

textView加载带图片的html

在android中textView中setText中加入富文本以及webView嵌入url是android交互html最简单的两种方式;
最近在项目中涉及到富文本;
当不考虑html中图片加载时候比较简单:

Spanned spanned =  = Html.fromHtml(html);
tvContent.setText(spanned );

只需要提供html即可,但是如果html中有pic时候 则会出现以下的情况;
android富文本 加载带图片的html_第1张图片

可以看到本该出现pic的地方出现了空格;所以在填充富文本时候需要对图片处理一下,在Html中有两个方法:

 public static Spanned fromHtml(String source) {

    }

    public static Spanned fromHtml(String source, Html.ImageGetter imageGetter, Html.TagHandler tagHandler) {

    }

其中三参的方法gormHtml可以处理图片的问题,需要匿名内部类实现 ImageGetter,第三个参数置为null:

ImageGetter imgGetter = new Html.ImageGetter() {  
        public Drawable getDrawable(String source) {  

            Drawable drawable = null;  
            URL url;  
            try {  
                url = new URL(source);  
                Drawable.createFromStream(url.openStream(), "");   
            } catch (Exception e) {  
                e.printStackTrace();  
                return null;  
            }  
            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),  
                    drawable.getIntrinsicHeight());  

            return drawable;  
        }  
    };  

    CharSequence charSequence = Html.fromHtml(html, imageGetter, null);
    tvContent.setText(charSequence );

这样就很明了了,imageGetter就是根据url来获取网络图片填充富文本的,很明显是一个好事操作,所以需要开子线程,而text的setText又是跟新UI的操作;这一点必须注意,不然你的drawable会为空,text无法填充富文本;
但是在填充过程中当html中pic比较多的时候回有部分获取不到;接下来就需要你在handler中控制一下就ok~ 下面来看最终代码:

    /**
     *
     * @param imageUrl
     * @return
     */
   public Drawable getImageFromNetwork(String imageUrl) {
        URL myFileUrl = null;
        Drawable drawable = null;
        try {
            myFileUrl = new URL(imageUrl);


            HttpURLConnection conn = (HttpURLConnection) myFileUrl
                    .openConnection();
            conn.setDoInput(true);

            conn.connect();
            InputStream is = conn.getInputStream();
            drawable = Drawable.createFromStream(is, null);

            is.close();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return drawable;
    }

    Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.what == 0x101) {
                //tvContent.setText((CharSequence) msg.obj);
                if (drawable == null){
                    bindData();
                    return false;
                }
                if (ncontent != null){
                    tvContent.setText((CharSequence) msg.obj);
                    tvContent.setClickable(true);
                    tvContent.setMovementMethod(LinkMovementMethod.getInstance());
                }

            }
            return false;
        }
    });

    private void bindData() {
        tvTitle.setText(mInfo.getNtitle());
        ncontent = mInfo.getNcontent();

        new Thread(new Runnable() {
            Message msg = Message.obtain();

            @Override
            public void run() {
                Html.ImageGetter imageGetter=new Html.ImageGetter() {
                    @Override
                    public Drawable getDrawable(String source) {

                        //String source1 = source;
                        ///Content/
                        if (source.substring(0,8).equals("/Content")){
                            source = Constant.BASE_URL + source;
                        }
                        drawable = getImageFromNetwork(source);
                        if (drawable != null){
                            drawable.setBounds(0,0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
                        }else if (drawable == null){
                            //bindData();
                            return null;
                        }


                        return drawable;
                    }
                };
               //Spanned spanned = Html.fromHtml(ncontent, imageGetter, null);
                CharSequence charSequence = Html.fromHtml(ncontent, imageGetter, null);
                msg.what = 0x101;
                msg.obj = charSequence;
                if (mHandler!= null){
                    mHandler.sendMessage(msg);
                }

            }
        }).start();


        toolbarTitle.setText(mInfo.getNtitle());
        String nsubtitle = mInfo.getNtitle();
        if (nsubtitle.length() > 10){
            String nsubtitle_new = nsubtitle.substring(0, 10);
            toolbarTitle.setText(nsubtitle_new + "...");
        }else{
            toolbarTitle.setText(nsubtitle);
        }

        tvTime.setText(mInfo.getNsendtime());

    }

这样图片就显示了;希望大家在阅读之后多提建议,期待成长;

你可能感兴趣的:(android,code)