Android HTML.fromHtml解析圖片標籤

轉自http://blog.csdn.net/u010418593/article/details/9324101


在之前Html類支持的HTML標籤文章中瞭解到當解析到<img>標籤時就會回調getDrawable()方法,並需要返回一個Drawable對象;當前我們需要定義類並實現ImageGetter接口以及在getDrawable方法中做相應的處理,下面我們則來看看具體該如何處理;

具體代碼:

[java] view plaincopy
  1. /** 
  2.  * ImageGetter接口的使用 
  3.  * @author Susie 
  4.  */  
  5. public class ImgLabelActivity extends Activity {  
  6.   
  7.     private static final String TAG = "ImgLabelActivity";  
  8.     /**本地圖片*/  
  9.     private TextView mTvOne;  
  10.     /**項目資源圖片*/  
  11.     private TextView mTvTwo;  
  12.     /**網絡圖片*/  
  13.     private TextView mTvThree;  
  14.     /**網絡圖片name*/  
  15.     private String picName = "networkPic.jpg";  
  16.     /**網絡圖片Getter*/  
  17.     private NetworkImageGetter mImageGetter;  
  18.     /**網絡圖片路徑*/  
  19.     private String htmlThree = "網絡圖片測試:" + "<img src='http://img.my.csdn.net/uploads/201307/14/1373780364_7576.jpg'>";  
  20.       
  21.     @Override  
  22.     public void onCreate(Bundle savedInstanceState) {  
  23.         super.onCreate(savedInstanceState);  
  24.         setContentView(R.layout.activity_img_label);  
  25.           
  26.         mTvOne = (TextView) this.findViewById(R.id.tv_img_label_one);  
  27.         String htmlOne = "本地圖片測試:" + "<img src='/mnt/sdcard/imgLabel.jpg'>";  
  28.         mTvOne.setText(Html.fromHtml(htmlOne, new LocalImageGetter(), null));  
  29.   
  30.         mTvTwo = (TextView) this.findViewById(R.id.tv_img_label_two);  
  31.         String htmlTwo = "項目圖片測試:" + "<img src=\""+R.drawable.imagepro+"\">";  
  32.         mTvTwo.setText(Html.fromHtml(htmlTwo, new ProImageGetter(), null));  
  33.        
  34.           
  35.         mTvThree = (TextView) this.findViewById(R.id.tv_img_label_three);  
  36.         mImageGetter = new NetworkImageGetter();  
  37.         mTvThree.setText(Html.fromHtml(htmlThree, mImageGetter, null));  
  38.     }  
  39.     /** 
  40.      * 本地圖片 
  41.      * @author Susie 
  42.      */  
  43.     private final class LocalImageGetter implements Html.ImageGetter{  
  44.           
  45.         @Override  
  46.         public Drawable getDrawable(String source) {  
  47.             // 獲取本地圖片  
  48.             Drawable drawable = Drawable.createFromPath(source);  
  49.             // 必須設爲圖片的邊際,不然TextView顯示不出圖片  
  50.             drawable.setBounds(00, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());  
  51.             // 將其返回  
  52.             return drawable;  
  53.         }  
  54.     }  
  55.     /** 
  56.      * 項目資源圖片 
  57.      * @author Susie 
  58.      */  
  59.     private final class ProImageGetter implements Html.ImageGetter{  
  60.   
  61.         @Override  
  62.         public Drawable getDrawable(String source) {  
  63.             // 獲取到資源id  
  64.             int id = Integer.parseInt(source);  
  65.             Drawable drawable = getResources().getDrawable(id);  
  66.             drawable.setBounds(00, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());  
  67.             return drawable;  
  68.         }  
  69.     }  
  70.     /** 
  71.      * 網絡圖片 
  72.      * @author Susie 
  73.      */  
  74.     private final class NetworkImageGetter implements Html.ImageGetter{  
  75.   
  76.         @Override  
  77.         public Drawable getDrawable(String source) {  
  78.               
  79.             Drawable drawable = null;  
  80.             // 封裝路徑  
  81.             File file = new File(Environment.getExternalStorageDirectory(), picName);  
  82.             // 判斷是否以http開頭  
  83.             if(source.startsWith("http")) {  
  84.                 // 判斷路徑是否存在  
  85.                 if(file.exists()) {  
  86.                     // 存在即獲取drawable  
  87.                     drawable = Drawable.createFromPath(file.getAbsolutePath());  
  88.                     drawable.setBounds(00, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());  
  89.                 } else {  
  90.                     // 不存在即開啓異步任務加載網絡圖片  
  91.                     AsyncLoadNetworkPic networkPic = new AsyncLoadNetworkPic();  
  92.                     networkPic.execute(source);  
  93.                 }  
  94.             }  
  95.             return drawable;  
  96.         }  
  97.     }  
  98.     /** 
  99.      * 加載網絡圖片異步類 
  100.      * @author Susie 
  101.      */  
  102.     private final class AsyncLoadNetworkPic extends AsyncTask<String, Integer, Void>{  
  103.   
  104.         @Override  
  105.         protected Void doInBackground(String... params) {  
  106.             // 加載網絡圖片  
  107.             loadNetPic(params);  
  108.             return null;  
  109.         }  
  110.           
  111.         @Override  
  112.         protected void onPostExecute(Void result) {  
  113.             super.onPostExecute(result);  
  114.             // 當執行完成後再次爲其設置一次  
  115.             mTvThree.setText(Html.fromHtml(htmlThree, mImageGetter, null));  
  116.         }  
  117.         /**加載網絡圖片*/  
  118.         private void loadNetPic(String... params) {  
  119.             String path = params[0];  
  120.               
  121.             File file = new File(Environment.getExternalStorageDirectory(), picName);  
  122.               
  123.             InputStream in = null;  
  124.               
  125.             FileOutputStream out = null;  
  126.               
  127.             try {  
  128.                 URL url = new URL(path);  
  129.                   
  130.                 HttpURLConnection connUrl = (HttpURLConnection) url.openConnection();  
  131.                   
  132.                 connUrl.setConnectTimeout(5000);  
  133.                   
  134.                 connUrl.setRequestMethod("GET");  
  135.                   
  136.                 if(connUrl.getResponseCode() == 200) {  
  137.                       
  138.                     in = connUrl.getInputStream();  
  139.                       
  140.                     out = new FileOutputStream(file);  
  141.                       
  142.                     byte[] buffer = new byte[1024];  
  143.                       
  144.                     int len;  
  145.                       
  146.                     while((len = in.read(buffer))!= -1){  
  147.                         out.write(buffer, 0, len);  
  148.                     }  
  149.                 } else {  
  150.                     Log.i(TAG, connUrl.getResponseCode() + "");  
  151.                 }  
  152.             } catch (Exception e) {  
  153.                 e.printStackTrace();  
  154.             } finally {  
  155.                   
  156.                 if(in != null) {  
  157.                     try {  
  158.                         in.close();  
  159.                     } catch (IOException e) {  
  160.                         e.printStackTrace();  
  161.                     }  
  162.                 }  
  163.                 if(out != null) {  
  164.                     try {  
  165.                         out.close();  
  166.                     } catch (IOException e) {  
  167.                         e.printStackTrace();  
  168.                     }  
  169.                 }  
  170.             }  
  171.         }  
  172.     }  
  173. }  



需要注意的是:

在獲取到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>



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章