TextView加载带图片的Html解决方案

查看了下网上关于TextView加载带图片标签的Html解决方案若干,发现很多都是讲下面这种:
1:重写ImageGetter,直接获取网络图片(这是比较通用的做法,但是遇到大图片或者网络不好的情况下,会阻塞主进程)

ImageGetter imgGetter = new Html.ImageGetter() {
        public Drawable getDrawable(String source) {
              Drawable drawable = null;
              URL url;  
              try {   
                  url = new URL(source);  
                  drawable = Drawable.createFromStream(url.openStream(), "");  //获取网路图片
              } catch (Exception e) {  
                  return null;  
              }  
              drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
              return drawable; 
        }
	};

 并设置TextView

TextView notice_content = (TextView) findViewById(R.id.notice_content);
notice_content.setText(Html.fromHtml(getIntent().getStringExtra(CONTENT),imgGetter,null));

2:重写ImageGetter,异步加载图片(这是比较流畅的方法,设置一个默认图片,异步加载出来,如果使用Drawable作为对象接收图片,手机会自动缩放图片,这不是我想要的,我需要的是真实大小图片(图片大于手机分辨率等比例缩放))

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;


import android.content.Context;
import android.content.res.Resources.NotFoundException;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.text.Html.ImageGetter;
import android.util.DisplayMetrics;
import android.view.Display;
import android.widget.TextView;

public class URLImageParser implements ImageGetter {
	Context c;
	TextView tv_image;
	private Drawable mDefaultDrawable;

	public URLImageParser(TextView t, Context c) {
		this.tv_image = t;
		this.c = c;

		try {
			mDefaultDrawable = c.getResources().getDrawable(
					R.drawable.activity_detail_title_default);
			// Log.i("-->", "执行");
		} catch (NotFoundException e) {
			mDefaultDrawable = null;
			// Log.i("-->", "执行1");
			e.printStackTrace();
		}
	}

	@Override
	public Drawable getDrawable(String source) {
		// TODO Auto-generated method stub
		URLDrawable urlDrawable = new URLDrawable();
		// main3.b.add(source);
		try {
			/*
			 * mDefaultDrawable.setBounds(0, 0, 0 +
			 * mDefaultDrawable.getIntrinsicWidth(),
			 * mDefaultDrawable.getIntrinsicHeight());
			 */
			urlDrawable.drawable = mDefaultDrawable;
			URLImageParser.this.tv_image.invalidate();
		} catch (Exception e) {
			e.printStackTrace();
		}
		/*
		 * urlDrawable.setBounds(0, 0, 0 + mDefaultDrawable.getIntrinsicWidth(),
		 * mDefaultDrawable.getIntrinsicHeight());
		 */
		ImageGetterAsyncTask asyncTask = new ImageGetterAsyncTask(urlDrawable);
		asyncTask.execute(source);
		return urlDrawable;
	}

	public class ImageGetterAsyncTask extends AsyncTask<String, Void, Drawable> {
		URLDrawable urlDrawable;

		public ImageGetterAsyncTask(URLDrawable d) {
			this.urlDrawable = d;
		}

		@Override
		protected void onPostExecute(Drawable result) {
			if (result != null) {
				urlDrawable.drawable = result;
				URLImageParser.this.tv_image.invalidate();
				// Log.i("-->", "执行3");
			}
		}

		@Override
		protected void onPreExecute() {
			urlDrawable.setBounds(0, 0,
					0 + mDefaultDrawable.getIntrinsicWidth(),
					0 + mDefaultDrawable.getIntrinsicHeight());
			urlDrawable.drawable = mDefaultDrawable;
			URLImageParser.this.tv_image.invalidate();
			super.onPreExecute();
		}

		@Override
		protected Drawable doInBackground(String... params) {
			// TODO Auto-generated method stub
			String source = params[0];// 图片URL
			return fetchDrawable(source);
		}

		// 获取URL的Drawable对象
		public Drawable fetchDrawable(String urlString) {
			BitmapDrawable bitmap = null;
			Drawable drawable = null;
			try {
				InputStream is = fetch(urlString);
				bitmap = (BitmapDrawable) BitmapDrawable.createFromStream(is,"src");
				drawable = bitmap;
				DisplayMetrics metrics = Constants.metrics;
				if(bitmap.getBitmap().getWidth()>metrics.widthPixels||bitmap.getBitmap().getHeight()>metrics.heightPixels)
					//进行等比例缩放程序
					drawable.setBounds(0, 0, metrics.widthPixels, ((int)(metrics.widthPixels*bitmap.getBitmap().getHeight()/bitmap.getBitmap().getWidth())));
				else
					drawable.setBounds(0,0,bitmap.getBitmap().getWidth(),bitmap.getBitmap().getHeight());
			} catch (Exception e) {
				return null;
			}
			return drawable;
		}

		private InputStream fetch(String urlString)
				throws MalformedURLException, IOException {
			DefaultHttpClient httpClient = new DefaultHttpClient();
			HttpGet request = new HttpGet(urlString);
			HttpResponse response = httpClient.execute(request);
			return response.getEntity().getContent();
		}
	}
}

URLDrawable类

import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

public class URLDrawable extends BitmapDrawable {
	protected Drawable drawable;
	@Override
	public void draw(Canvas canvas) {
	  
	  if (drawable != null) {
	   drawable.draw(canvas);
	  }
	}
}

此是解决方案中的部分,如果有好的解决方案,请粘贴出来,让我参考 研究!谢谢


你可能感兴趣的:(TextView加载带图片的Html解决方案)