標籤:android 非同步 圖片 edittext html
android中的Html.fromHtml可以用來載入HTML的內容,fromHtml有三個參數需要設定,第一個是要顯示的html內容,第二個就是要說的重點,ImageGetter,用來處理圖片載入的,第三個 TagHandler可以設定為null,接下來我們來講解下ImageGetter,網上很多的方法都是直接引用本地的圖片,是同步的,比如:
private ImageGetter imageGetter = new ImageGetter() {@Overridepublic 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); } }}
接下來就是重寫ImageGetter
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(); } }}
代碼裡的注釋也非常的清楚明了了,這裡就不用重複說明了,最重要的就是要重寫onPostExecute,這個方法是載入完畢之後UI的重新整理用的,需要對drawable進行重繪才能在EditText中顯示出來,而且不會和文字的位置重疊。是不是非常的簡單?
Android EditText載入HTML內容(內容包含網狀圖片)