textview显示表情图片

调用的部分:

holder.message.setText(Html.fromHtml(str));

		Spanned spanned = Html.fromHtml(str, new MyImageGetter(context,
				holder.message, dm), null);
		holder.message.setText(spanned);


holder.message为textview,str为显示的String和图片

MyImageGetter.java

package com.etwod.gsyzd.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import com.etwod.gsyzd.R;
import com.etwod.gsyzd.util.UserTask.TaskCallback;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Environment;
import android.text.Html.ImageGetter;
import android.util.DisplayMetrics;
import android.widget.TextView;

public class MyImageGetter implements ImageGetter {

	private Context context;
	private TextView tv;
	private DisplayMetrics mDm;

	private DrawableLoader drawableLoader = null;
	private URI uri;

	public MyImageGetter(Context context, TextView tv, DisplayMetrics dm) {
		this.context = context;
		this.tv = tv;
		this.mDm = dm;
		drawableLoader = DrawableLoader.getInstance();
	}

	@Override
	public Drawable getDrawable(String source) {
		// TODO Auto-generated method stub
		try {
			uri = new URI(source);
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (!source.equals("") && !source.equals(null) && uri.isAbsolute()) {

			// 将source进行MD5加密并保存至本地
			final String imageName = Common.md5(source);
			final String sdcardPath = Environment.getExternalStorageDirectory()
					.toString(); // 获取SDCARD的路径

			// 最终图片保持的地址
			String savePath = sdcardPath + "/DownLoad/cache_images/"
					+ imageName;

			// 加载默认图
			Resources res = context.getResources();
			URLDrawable drawable = new URLDrawable(
					res.getDrawable(R.drawable.empty_photo));
			File file = new File(savePath);

			if (file.exists()
					&& drawableLoader.getBitmapFromMemoryCache(savePath) != null) {
				drawable = (URLDrawable) drawableLoader
						.getBitmapFromMemoryCache(savePath);
				return drawable;
			} else if (file.exists()
					&& drawableLoader.getBitmapFromMemoryCache(savePath) == null) {
				// new sdImageAsync(drawable).execute(savePath);
				sdAsyncTask sdTask = new sdAsyncTask(context, drawable);
				sdTask.registerCallback(new TaskCallback<String, Integer, Drawable>() {

					@Override
					public Drawable call(
							UserTask<String, Integer, Drawable> task,
							String[] params) throws Exception {
						// TODO Auto-generated method stub
						String savePath = params[0];
						Drawable drawable = null;
						drawable = Drawable.createFromPath(savePath);
						
						URLDrawable uDrawable = new URLDrawable(drawable);
						drawableLoader.addBitmapToMemoryCache(savePath,
								uDrawable);
						return drawable;
					}
				});
				sdTask.execute(savePath);

			} else {
				// new webImageAsync(drawable).execute(savePath, source);
				webAsyncTask webtask = new webAsyncTask(context, drawable);
				webtask.registerCallback(new TaskCallback<String, Integer, Drawable>() {

					@Override
					public Drawable call(
							UserTask<String, Integer, Drawable> task,
							String[] params) throws Exception {
						// TODO Auto-generated method stub
						String savePath = params[0];
						String url = params[1];

						Drawable drawable = null;
						InputStream in = null;
						try {
							// 获取网络图片
							HttpGet http = new HttpGet(url);
							HttpClient client = new DefaultHttpClient();
							HttpResponse response = client
									.execute(http);
							BufferedHttpEntity bufferedHttpEntity = new BufferedHttpEntity(
									response.getEntity());
							in = bufferedHttpEntity.getContent();
							
						} catch (Exception e) {
							try {
								if (in != null)
									in.close();
							} catch (Exception e2) {
								// TODO: handle exception
							}
						}
						// 获取的数据流为空时返回默认图片
						if (in == null)
							return drawable;
						// 将图片保存到SDcard
						try {
							File file = new File(savePath);
							String basePath = file.getParent();
							File basePathFile = new File(basePath);
							if (!basePathFile.exists()) {
								basePathFile.mkdirs();
							}
							// 创建文件
							file.createNewFile();
							FileOutputStream fileout = new FileOutputStream(
									file);
							byte[] buffer = new byte[4 * 1024];
							// 写入数据
							while (in.read(buffer) != -1) {
								fileout.write(buffer);
							}
							fileout.flush();
							File file2 = new File(savePath);
//							Log.e("图片大小", imageName+"----"+file2.length()+"");
							if(file2.length()>4096){
								String oo = ImageUtil.saveImg(
										ImageUtil
												.getThumbUploadPath(
														savePath,
														(int) (mDm.widthPixels - mDm.scaledDensity * 30)),
										imageName, "/DownLoad/cache_images/");
							}
							
							Drawable mDrawable = Drawable
									.createFromPath(savePath);
							URLDrawable uDrawable = new URLDrawable(mDrawable);
							drawableLoader.addBitmapToMemoryCache(savePath,
									uDrawable);
							return mDrawable;
						} catch (Exception e) {
							// TODO: handle exception
						}
						return drawable;
					}
				});
				webtask.execute(savePath, source);
			}
			return drawable;
		} else {

			return null;
		}
	}

	// 读取缓存图片
	private class sdAsyncTask extends UserTask<String, Integer, Drawable> {
		private URLDrawable drawable;

		public sdAsyncTask(Context context, URLDrawable uDrawable) {
			super(context);
			this.drawable = uDrawable;
		}

		@Override
		protected void onErrorHandle(Context context, Exception error) {
			// TODO Auto-generated method stub

		}

		@Override
		protected void onTaskFinished(Context context, Drawable result) {
			// TODO Auto-generated method stub
			if (result != null) {
				drawable.setDrawable(result);
				tv.setText(tv.getText()); // 通过这里的重新设置 TextView 的文字来更新UI
				// System.out.println("tv.getText() ==> " + tv.getText() );
			}
		}

	}

	// 读取网络图片并缓存
	private class webAsyncTask extends UserTask<String, Integer, Drawable> {
		private URLDrawable drawable;

		public webAsyncTask(Context context, URLDrawable uDrawable) {
			super(context);
			this.drawable = uDrawable;
		}

		@Override
		protected void onErrorHandle(Context context, Exception error) {
			// TODO Auto-generated method stub

		}

		@Override
		protected void onTaskFinished(Context context, Drawable result) {
			// TODO Auto-generated method stub
			if (result != null) {
				drawable.setDrawable(result);
				tv.setText(tv.getText()); // 通过这里的重新设置 TextView 的文字来更新UI
				// System.out.println("tv.getText() ==> " + tv.getText() );
			}
		}

	}

	public class URLDrawable extends BitmapDrawable {

		private Drawable drawable;

		public URLDrawable(Drawable defaultDraw) {
			setDrawable(defaultDraw);
		}

		private void setDrawable(Drawable nDrawable) {
			drawable = nDrawable;
			// textview显示区域的宽
			int tvW = (int) (mDm.widthPixels - mDm.scaledDensity * 30);
			// mDm == > DisplayMetrics{density=1.5, width=540, height=960,
			// scaledDensity=1.5, xdpi=274.32, ydpi=273.97754}
			if (drawable.getIntrinsicWidth() * mDm.scaledDensity < tvW) {
				drawable.setBounds(
						0,
						0,
						(int) (drawable.getIntrinsicWidth() * mDm.scaledDensity),
						(int) (drawable.getIntrinsicHeight() * mDm.scaledDensity));
				setBounds(
						0,
						0,
						(int) (drawable.getIntrinsicWidth() * mDm.scaledDensity),
						(int) (drawable.getIntrinsicHeight() * mDm.scaledDensity));
			} else {
				drawable.setBounds(0, 0, tvW-20,
						drawable.getIntrinsicHeight() * (tvW-20) / drawable
								.getIntrinsicWidth());
				setBounds(0, 0, tvW,
						drawable.getIntrinsicHeight() * (tvW-20) / drawable
								.getIntrinsicWidth());
			}
		}

		@Override
		public void draw(Canvas canvas) {
			// TODO Auto-generated method stub
			drawable.draw(canvas);
		}

	}
}


 

你可能感兴趣的:(textview显示表情图片)