轉自http://blog.csdn.net/u010418593/article/details/9324101
在之前Html類支持的HTML標籤文章中瞭解到當解析到<img>標籤時就會回調getDrawable()方法,並需要返回一個Drawable對象;當前我們需要定義類並實現ImageGetter接口以及在getDrawable方法中做相應的處理,下面我們則來看看具體該如何處理;
具體代碼:
- /**
- * ImageGetter接口的使用
- * @author Susie
- */
- public class ImgLabelActivity extends Activity {
- private static final String TAG = "ImgLabelActivity";
- /**本地圖片*/
- private TextView mTvOne;
- /**項目資源圖片*/
- private TextView mTvTwo;
- /**網絡圖片*/
- private TextView mTvThree;
- /**網絡圖片name*/
- private String picName = "networkPic.jpg";
- /**網絡圖片Getter*/
- private NetworkImageGetter mImageGetter;
- /**網絡圖片路徑*/
- private String htmlThree = "網絡圖片測試:" + "<img src='http://img.my.csdn.net/uploads/201307/14/1373780364_7576.jpg'>";
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_img_label);
- mTvOne = (TextView) this.findViewById(R.id.tv_img_label_one);
- String htmlOne = "本地圖片測試:" + "<img src='/mnt/sdcard/imgLabel.jpg'>";
- mTvOne.setText(Html.fromHtml(htmlOne, new LocalImageGetter(), null));
- mTvTwo = (TextView) this.findViewById(R.id.tv_img_label_two);
- String htmlTwo = "項目圖片測試:" + "<img src=\""+R.drawable.imagepro+"\">";
- mTvTwo.setText(Html.fromHtml(htmlTwo, new ProImageGetter(), null));
- mTvThree = (TextView) this.findViewById(R.id.tv_img_label_three);
- mImageGetter = new NetworkImageGetter();
- mTvThree.setText(Html.fromHtml(htmlThree, mImageGetter, null));
- }
- /**
- * 本地圖片
- * @author Susie
- */
- private final class LocalImageGetter implements Html.ImageGetter{
- @Override
- public Drawable getDrawable(String source) {
- // 獲取本地圖片
- Drawable drawable = Drawable.createFromPath(source);
- // 必須設爲圖片的邊際,不然TextView顯示不出圖片
- drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
- // 將其返回
- return drawable;
- }
- }
- /**
- * 項目資源圖片
- * @author Susie
- */
- private final class ProImageGetter implements Html.ImageGetter{
- @Override
- public Drawable getDrawable(String source) {
- // 獲取到資源id
- int id = Integer.parseInt(source);
- Drawable drawable = getResources().getDrawable(id);
- drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
- return drawable;
- }
- }
- /**
- * 網絡圖片
- * @author Susie
- */
- private final class NetworkImageGetter implements Html.ImageGetter{
- @Override
- public Drawable getDrawable(String source) {
- Drawable drawable = null;
- // 封裝路徑
- File file = new File(Environment.getExternalStorageDirectory(), picName);
- // 判斷是否以http開頭
- if(source.startsWith("http")) {
- // 判斷路徑是否存在
- if(file.exists()) {
- // 存在即獲取drawable
- drawable = Drawable.createFromPath(file.getAbsolutePath());
- drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
- } else {
- // 不存在即開啓異步任務加載網絡圖片
- AsyncLoadNetworkPic networkPic = new AsyncLoadNetworkPic();
- networkPic.execute(source);
- }
- }
- return drawable;
- }
- }
- /**
- * 加載網絡圖片異步類
- * @author Susie
- */
- private final class AsyncLoadNetworkPic extends AsyncTask<String, Integer, Void>{
- @Override
- protected Void doInBackground(String... params) {
- // 加載網絡圖片
- loadNetPic(params);
- return null;
- }
- @Override
- protected void onPostExecute(Void result) {
- super.onPostExecute(result);
- // 當執行完成後再次爲其設置一次
- mTvThree.setText(Html.fromHtml(htmlThree, mImageGetter, null));
- }
- /**加載網絡圖片*/
- private void loadNetPic(String... params) {
- String path = params[0];
- File file = new File(Environment.getExternalStorageDirectory(), picName);
- InputStream in = null;
- FileOutputStream out = null;
- try {
- URL url = new URL(path);
- HttpURLConnection connUrl = (HttpURLConnection) url.openConnection();
- connUrl.setConnectTimeout(5000);
- connUrl.setRequestMethod("GET");
- if(connUrl.getResponseCode() == 200) {
- in = connUrl.getInputStream();
- out = new FileOutputStream(file);
- byte[] buffer = new byte[1024];
- int len;
- while((len = in.read(buffer))!= -1){
- out.write(buffer, 0, len);
- }
- } else {
- Log.i(TAG, connUrl.getResponseCode() + "");
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if(in != null) {
- try {
- in.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if(out != null) {
- try {
- out.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
- }
需要注意的是:
在獲取到drawable時需要爲其設置邊界,如沒有設置的話TextView就不能顯示該drawable了;
在加載網絡圖片時需要開啓子線程去訪問網絡並將圖片存儲到本地,之後再次爲其設置一次;
Android TextView 支持的HTML標籤
<a href="...">
<b>
<big>
<blockquote>
<br>
<cite>
<dfn>
<div align="...">
<em>
<font size="..." color="..." face="...">
<h1>
<h2>
<h3>
<h4>
<h5>
<h6>
<i>
<img src="...">
<p>
<small>
<strike>
<strong>
<sub>
<sup>
<tt>
<u>