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);

 

posted @ 2022-06-23 16:46  逍遥散人95  阅读(197)  评论(0编辑  收藏  举报