android中的Html.fromHtml可以用来加载HTML的内容,fromHtml有三个参数需要设置,第一个是要显示的html内容,第二个就是要说的重点,ImageGetter,用来处理图片加载的,第三个 TagHandler可以设置为null,接下来我们来讲解下ImageGetter,网上很多的方法都是直接引用本地的图片,是同步的,比如:
private ImageGetter imageGetter = new ImageGetter() { @Override public Drawable getDrawable(String source) { String f = pic_path.substring(0, 1); String url = pic_path.substring(2); if (f.equals("1")) { try { ContentResolver cr = getActivity().getContentResolver(); Uri uri = Uri.parse(url); Bitmap bitmap = getimage(cr, uri); return getMyDrawable(bitmap); } catch (Exception e) { e.printStackTrace(); return null; } } else { return null; } } };上面的代码是我做的一个项目里面用到的引用本地图片的方法,重写imagegetter,然后用ContentResolver来读取图片转换为Bitmap,然后再进行显示,可是,很多时候会我们都需要引用的是网络图片的,那这个方法就行不通了。查找了很多资料,如果直接在里面用异步的方法来加载图片的话,显示出来的是一个正方形的点的,那么问题来了,我们应该怎么去加载网络图片呢?
首先,我们先创建一个URLDrawable,让它去继承BitmapDrawable,重写draw方法,这个有什么用呢?这个可以让你加载图片的时候显示初始的图片,也就是加载中的图片。
URLDrawable.java:
public class URLDrawable extends BitmapDrawable { // the drawable that you need to set, you could set the initial drawing // with the loading image if you need to protected Drawable drawable; @Override public void draw(Canvas canvas) { // override the draw to facilitate refresh function later if(drawable != null) { drawable.draw(canvas); } } }
URLImageParser继承ImageGetter
放源码
URLImageParser.java
public class URLImageParser implements ImageGetter { Context c; EditText container; /*** * 构建URLImageParser将执行AsyncTask,刷新容器 * @param t * @param c */ public URLImageParser(EditText t, Context c) { this.c = c; this.container = t; } public Drawable getDrawable(String source) { URLDrawable urlDrawable = new URLDrawable(); // 获得实际的源 ImageGetterAsyncTask asyncTask = new ImageGetterAsyncTask( urlDrawable); asyncTask.execute(source); //返回引用URLDrawable我将改变从src与实际图像标记 return urlDrawable; } public class ImageGetterAsyncTask extends AsyncTask<String, Void, Drawable> { URLDrawable urlDrawable; public ImageGetterAsyncTask(URLDrawable d) { this.urlDrawable = d; } @Override protected Drawable doInBackground(String... params) { String source = params[0]; return fetchDrawable(source); } @Override protected void onPostExecute(Drawable result) { // 设置正确的绑定根据HTTP调用的结果 Log.d("height",""+result.getIntrinsicHeight()); Log.d("width",""+result.getIntrinsicWidth()); urlDrawable.setBounds(0, 0, 0+result.getIntrinsicWidth(), 0+result.getIntrinsicHeight()); // 改变当前可提取的参考结果从HTTP调用 urlDrawable.drawable = result; // 绘制图像容器 URLImageParser.this.container.invalidate(); // For ICS URLImageParser.this.container.setHeight((URLImageParser.this.container.getHeight() + result.getIntrinsicHeight())); // Pre ICS URLImageParser.this.container.setEllipsize(null); } /*** * 得到Drawable的URL * @param urlString * @return */ public Drawable fetchDrawable(String urlString) { try { InputStream is = fetch(urlString); Drawable drawable = Drawable.createFromStream(is, "src"); drawable.setBounds(0, 0, 0 + drawable.getIntrinsicWidth(), 0 + drawable.getIntrinsicHeight()); return drawable; } catch (Exception e) { return null; } } 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(); } } }