查看了下網上關於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();}}@Overridepublic Drawable getDrawable(String source) {// TODO Auto-generated method stubURLDrawable 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;}@Overrideprotected void onPostExecute(Drawable result) {if (result != null) {urlDrawable.drawable = result;URLImageParser.this.tv_image.invalidate();// Log.i("-->", "執行3");}}@Overrideprotected void onPreExecute() {urlDrawable.setBounds(0, 0,0 + mDefaultDrawable.getIntrinsicWidth(),0 + mDefaultDrawable.getIntrinsicHeight());urlDrawable.drawable = mDefaultDrawable;URLImageParser.this.tv_image.invalidate();super.onPreExecute();}@Overrideprotected Drawable doInBackground(String... params) {// TODO Auto-generated method stubString source = params[0];// 圖片URLreturn 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())));elsedrawable.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;@Overridepublic void draw(Canvas canvas) { if (drawable != null) { drawable.draw(canvas); }}}
此是解決方案中的部分,如果有好的解決方案,請粘貼出來,讓我參考 研究!謝謝