Android TextView Html ImageGetter 图片位置不对

来源:互联网 发布:我的危险妻子 知乎 编辑:程序博客网 时间:2024/05/13 04:21

TextView 显示Html的方法:

textView.setText(Html.fromHtml(string))
 

Tags supported by Android

  • <p>
  • <div> handled exactly like <p>
  • <br>
  • <b>
  • <i>
  • <strong> 
  • <em> 
  • <u>
  • <tt>
  • <dfn>
  • <sub>
  • <sup>
  • <blockquote>
  • <cite>
  • <big>
  • <small>
  • <font size="..." color="..." face="...">
  • <h1><h2><h3><h4><h5><h6>
  • <a href="...">
  • <img src="...">

推荐一个开源项目,支持Html的TextView,添加了几个Tags的支持:
https://github.com/sufficientlysecure/html-textview

Extended support by HtmlTextView

  • <ul>
  • <ol>
  • <li>
  • <code>
  • <center>
  • <strike>
使用这个项目基本能正常显示以上的Html tags。

但是显示图片位置不对,相信很多人遇到这个问题。

TextView显示图片都要自己写个ImageGetter的类,如果图片位置不对,可以试试这行代码,在onPostExecute里面加上
 
((HtmlTextView)container).setText(((HtmlTextView) container).getText());

完整类:
 
package org.sufficientlysecure.htmltextview;import android.content.Context;import android.content.Intent;import android.content.res.Resources;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.drawable.BitmapDrawable;import android.graphics.drawable.Drawable;import android.os.AsyncTask;import android.text.Html.ImageGetter;import android.util.Log;import android.view.View;import com.example.administrator.bbcodetest.R;import java.io.IOException;import java.io.InputStream;import java.net.URI;import java.net.URL;public class HtmlRemoteImageGetter implements ImageGetter {    Context c;    View container;    URI baseUri;    /**     * Construct the URLImageParser which will execute AsyncTask and refresh the container     */    public HtmlRemoteImageGetter(View t, Context c, String baseUrl) {        this.c = c;        this.container = t;        if (baseUrl != null) {            this.baseUri = URI.create(baseUrl);        }    }    public Drawable getDrawable(String source) {        UrlDrawable urlDrawable = new UrlDrawable();        ImageGetterAsyncTask asyncTask = new ImageGetterAsyncTask(urlDrawable);        asyncTask.execute(source);        // return reference to URLDrawable which will asynchronously load the image specified in the src tag        return urlDrawable;    }    public class ImageGetterAsyncTask extends AsyncTask<String, Void, Drawable> {        UrlDrawable urlDrawable;        String source;        public ImageGetterAsyncTask(UrlDrawable d) {            this.urlDrawable = d;        }        @Override        protected void onPreExecute() {            super.onPreExecute();        }        @Override        protected void onPostExecute(final Drawable result) {            if (result == null) {                Log.w(HtmlTextView.TAG, "Drawable result is null! (source: " + source + ")");                return;            }            // set the correct bound according to the result from HTTP call//            urlDrawable.setBounds(0, 0, 0 + result.getIntrinsicWidth(), 0 + result.getIntrinsicHeight());            // change the reference of the current drawable to the result from the HTTP call            urlDrawable.drawable = result;            // redraw the image by invalidating the container            urlDrawable.invalidateSelf();            HtmlRemoteImageGetter.this.container.invalidate();            // important            <span style="color:#ff0000;">((HtmlTextView)container).setText(((HtmlTextView) container).getText());</span>        }@Override        protected Drawable doInBackground(String... params) {            source = params[0];            return fetchDrawable(source);        }        /**         * Get the Drawable from URL         */        public Drawable fetchDrawable(String urlString) {            try {                InputStream is = fetch(urlString);                Bitmap bitmap= BitmapFactory.decodeStream(is);                Drawable drawable = new BitmapDrawable(c.getResources(), bitmap);                int width = bitmap.getWidth();                int height = bitmap.getHeight();                int newWidth = width;                int newHeight = height;                if( width > container.getWidth() ) {                    newWidth = container.getWidth();                    newHeight = (newWidth * height) / width;                }                drawable.setBounds(0, 0, 0 + newWidth, 0 + newHeight);                urlDrawable.setBounds(0, 0, newWidth, newHeight);                return drawable;            } catch (Exception e) {                return null;            }        }        private InputStream fetch(String urlString) throws IOException {            URL url;            if (baseUri != null) {                url = baseUri.resolve(urlString).toURL();            } else {                url = URI.create(urlString).toURL();            }            return (InputStream) url.getContent();        }    }    @SuppressWarnings("deprecation")    public class UrlDrawable extends BitmapDrawable {        protected Drawable drawable;        @Override        public void draw(Canvas canvas) {            // override the draw to facilitate refresh function later            if (drawable != null) {                drawable.draw(canvas);            }        }    }} 

 

 
0 0
原创粉丝点击