Android中Textview顯示帶html文本二-------【Textview顯示本地圖片】


Textview可以顯示基本的HTML標籤,如果不知道那些標籤,可以查看Android中Textview顯示帶html文本一-------【HTML標籤】

下面着重說一下Textview顯示“img”標籤,也許看到這裏,大家都會想到就是構建ImageGetter,重載一下其 public Drawable getDrawable(String source)方法,獲取該路徑的圖片。

例如:

final Html.ImageGetter imageGetter = new Html.ImageGetter() {
        public Drawable getDrawable(String source) {
            return drawable;
        };

    };

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

例如:

final String sText = "測試圖片信息:<br><img src=\"http://pic004.cnblogs.com/news/201211/20121108_091749_1.jpg\" /><img src=\"http://pic004.cnblogs.com/news/201211/20121108_091749_1.jpg\" />";
tView.setText(Html.fromHtml(sText, imageGetter, null));

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

當然這個<img src=路徑/> 這個路徑既可以是網絡圖片,也可以本地圖片,項目資源圖片

例如:本地圖片<img src=\""/sdcard/images/test.jpg"\"/>   項目資源圖片 <img src=\""+R.drawable.market_none_image+"\"/>

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

第一種:本地圖片

複製代碼
final String sText2 = "測試圖片信息:<img src=\"/mnt/sdcard/temp/1.jpg\" />";
tView.setText(Html.fromHtml(sText2, imageGetter, null));

final Html.ImageGetter imageGetter = new Html.ImageGetter() {

    public Drawable getDrawable(String source) {
        Drawable drawable=null;
    drawable=Drawable.createFromPath(source);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
    return drawable;  };
}
複製代碼

第二種:項目資源圖片

複製代碼
final String sText1 = "測試圖片信息:<img src=\""+R.drawable.market_none_image+"\" />";tView.setText(Html.fromHtml(sText1, imageGetter, null));

final Html.ImageGetter imageGetter = new Html.ImageGetter() {

    public Drawable getDrawable(String source) {
        Drawable drawable=null;
    int rId=Integer.parseInt(source);
    drawable=getResources().getDrawable(rId);
    drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
    return drawable;    };
}
複製代碼

第三種:網絡圖片

複製代碼
final String sText = "測試圖片信息:<br><img src=\"http://pic004.cnblogs.com/news/201211/20121108_091749_1.jpg\" />";
tView.setText(Html.fromHtml(sText, imageGetter, null));

final Html.ImageGetter imageGetter = 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) {
        e.printStackTrace();
        return null;
    }
    drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());            
    return drawable;     };
}
複製代碼

 

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

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

通過以上三種方式,是能可以顯示出來圖片,但是我發現了一個問題,就是第三種,顯示網絡圖片,我用android2.3的系統,可以顯示圖片出來,並且如果圖片比較大,應用會卡的現象,肯定是因爲使用主線程去獲取網絡圖片造成的,但如果我用android4.0以上的系統運行,則不能顯示圖片,只顯示小方框。

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

具體處理方式看下篇:Android中Textview顯示帶html文本三-------【Textview顯示網絡圖片】

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