Html类ImageGetter接口

来源:互联网 发布:linux shell编程工具 编辑:程序博客网 时间:2024/05/20 14:25

在之前Html类支持的HTML标签文章中了解到当解析到<img>标签时就会回调getDrawable()方法,并需要返回一个Drawable对象;当前我们需要定义类并实现ImageGetter接口以及在getDrawable方法中做相应的处理,下面我们则来看看具体该如何处理;

具体代码:

[java] view plain copy
  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了;

在加载网络图片时需要开启子线程去访问网络并将图片存储到本地,之后再次为其设置一次;

原文出处
0 0
原创粉丝点击