Android TextView加載圖片

 1   ImageGetter imageGetter = new ImageGetter() {
 2 
 3                             @Override
 4                             public Drawable getDrawable(String source) {
 5                                 URL url;
 6                                 Drawable drawable = null;
 7                                 try {
 8                                     url = new URL(source);
 9                                     drawable = Drawable.createFromStream(url.openStream(), null);
10                                     if (drawable != null) {
11                                         int w = drawable.getIntrinsicWidth();
12                                         int h = drawable.getIntrinsicHeight();
13                                         //對圖片大小進行等比例放大 此處寬高可自行調整
14                                         if (w < h && h > 0) {
15                                             float scale = (400.0f / h);
16                                             w = (int) (scale * w);
17                                             h = (int) (scale * h);
18                                         } else if (w > h && w > 0) {
19                                             float scale = (1000.0f / w);
20                                             w = (int) (scale * w);
21                                             h = (int) (scale * h);
22                                         }
23                                         drawable.setBounds(0, 0, w, h);
24                                     }
25                                 } catch (Exception e) {
26                                     e.printStackTrace();
27                                 }
28                                 return drawable;
29                             }
30                         };
31                         CharSequence test = Html.fromHtml(html, imageGetter, null);
32 
33                         txtContent.setText(test);

 

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