日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Android中Textview顯示帶html文本

下面著重說一下TextView顯示“img”標(biāo)簽,也許看到這里,大家都會想到就是構(gòu)建ImageGetter,重載一下其 public Drawable getDrawable(String source)方法,獲取該路徑的圖片。

例如:

 
 
  1. final Html.ImageGetter imageGetter = new Html.ImageGetter() { 
  2.         public Drawable getDrawable(String source) { 
  3.             return drawable; 
  4.         }; 
  5.   
  6.     }; 

下面來說下public Drawable getDrawable(String source)這個方法,source就是圖片路徑!

例如:

 
 
  1. final String sText = "測試圖片信息:
    "; 
  2. tView.setText(Html.fromHtml(sText, imageGetter, null)); 

則source就是img的src的值,既是:http://pic004.cnblogs.com/news/201211/20121108_091749_1.jpg這個圖片路徑。

當(dāng)然這個 這個路徑既可以是網(wǎng)絡(luò)圖片,也可以本地圖片,項目資源圖片。

例如:本地圖片   項目資源圖片

但是不同的路徑,ImageGetter的重載處理方法都不一樣,下面來一一介紹各種的處理方式.

***種:本地圖片

 
 
  1. final String sText2 = "測試圖片信息:"; 
  2. tView.setText(Html.fromHtml(sText2, imageGetter, null)); 
  3.   
  4. final Html.ImageGetter imageGetter = new Html.ImageGetter() { 
  5.   
  6.     public Drawable getDrawable(String source) { 
  7.         Drawable drawable=null; 
  8.     drawable=Drawable.createFromPath(source); 
  9.         drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); 
  10.     return drawable;  }; 

第二種:項目資源圖片

 
 
  1. final String sText1 = "測試圖片信息:";tView.setText(Html.fromHtml(sText1, imageGetter, null)); 
  2.   
  3. final Html.ImageGetter imageGetter = new Html.ImageGetter() { 
  4.   
  5.     public Drawable getDrawable(String source) { 
  6.         Drawable drawable=null; 
  7.     int rId=Integer.parseInt(source); 
  8.     drawable=getResources().getDrawable(rId); 
  9.     drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); 
  10.     return drawable;    }; 

第三種:網(wǎng)絡(luò)圖片

 
 
  1. final String sText = "測試圖片信息:
    "; 
  2. tView.setText(Html.fromHtml(sText, imageGetter, null)); 
  3.   
  4. final Html.ImageGetter imageGetter = new Html.ImageGetter() { 
  5.   
  6.     public Drawable getDrawable(String source) { 
  7.         Drawable drawable=null; 
  8.     URL url; 
  9.     try { 
  10.         url = new URL(source); 
  11.         drawable = Drawable.createFromStream(url.openStream(), ""); 
  12.     } catch (Exception e) { 
  13.         e.printStackTrace(); 
  14.         return null; 
  15.     } 
  16.     drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());             
  17.     return drawable;     }; 

通過這三個方式,可以看出,不同的圖片路徑,得到圖片的處理方式不同,大家也能一目了然的看出來ImageGetter是干什么的了,就是得到img中src所需的圖片!

提醒一點:獲取圖片以后,一定要設(shè)置圖片的邊界,界線,即:drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());,不然獲取圖片后,Textview不能顯示圖片。

通過以上三種方式,是能可以顯示出來圖片,但是我發(fā)現(xiàn)了一個問題,就是第三種,顯示網(wǎng)絡(luò)圖片,我用Android2.3的系統(tǒng),可以顯示圖片出來,并且如 果圖片比較大,應(yīng)用會卡的現(xiàn)象,肯定是因為使用主線程去獲取網(wǎng)絡(luò)圖片造成的,但如果我用android4.0以上的系統(tǒng)運(yùn)行,則不能顯示圖片,只顯示小方 框。

究其原因,是在4.0的系統(tǒng)上執(zhí)行的時候報錯了,異常是:android.os.NetworkOnMainThreadException 經(jīng)過查文檔,原來是4.0系統(tǒng)不允許主線程(UI線程)訪問網(wǎng)絡(luò),因此導(dǎo)致了其異常。說白了就是在主線程上訪問網(wǎng)絡(luò),會造成主線程掛起,系統(tǒng)不允許使用 了。


網(wǎng)頁名稱:Android中Textview顯示帶html文本
轉(zhuǎn)載來于:http://www.5511xx.com/article/dhiccch.html