Android图片加载与缓存开源框架:Android Glide

Android Glide是一个开源的图片加载和缓存处理的第三方框架。和Android的Picasso库类似,个人感觉比Android Picasso好用。Android Glide使自身内部已经实现了缓存策略,使得开发者摆脱Android图片加载的琐碎事务,专注逻辑业务的代码。Android Glide使用便利,短短几行简单明晰的代码,即可完成大多数图片从网络(或者本地)加载、显示的功能需求。

使用Android Glide,需要先下载Android Glide的库,Android Glide在github上的项目主页:

https://github.com/bumptech/glide 。

实际的项目使用只需要到Glide的releases页面把jar包下载后导入到本地的libs里面即可直接使用。Glide的releases的页面地址:https://github.com/bumptech/glide/releases ,在此页面找到最新的jar包,下载后放到自己项目的libs中,比如glide 3.6.0库的jar包下载地址:https://github.com/bumptech/glide/releases/download/v3.6.0/glide-3.6.0.jar


 接下来是在自己的项目中具体使用,现在给出一个具体的使用例子加以简单说明(通过网络加载图片然后在ImageView中显示出来):

build.gradle增加如下依赖配置:

compile 'com.github.bumptech.glide:glide:3.7.0'


MainActivity.Java

[java]  view plain  copy
  1. import com.bumptech.glide.Glide;  
  2.   
  3. import android.support.v7.app.ActionBarActivity;  
  4. import android.view.LayoutInflater;  
  5. import android.view.View;  
  6. import android.view.ViewGroup;  
  7. import android.widget.ArrayAdapter;  
  8. import android.widget.ImageView;  
  9. import android.widget.ListView;  
  10. import android.app.Activity;  
  11. import android.content.Context;  
  12. import android.os.Bundle;  
  13.   
  14. public class MainActivity extends ActionBarActivity {  
  15.   
  16.     private Activity mActivity;  
  17.     // 将从此URL加载网络图片。  
  18.     private String img_url = "http://avatar.csdn.net/9/7/A/1_zhangphil.jpg";  
  19.   
  20.     @Override  
  21.     protected void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         mActivity = this;  
  24.   
  25.         setContentView(R.layout.activity_main);  
  26.   
  27.         ListView lv = (ListView) findViewById(R.id.listView);  
  28.         lv.setAdapter(new MyAdapter(this, R.layout.item));  
  29.     }  
  30.   
  31.     private class MyAdapter extends ArrayAdapter {  
  32.   
  33.         private int resource;  
  34.   
  35.         public MyAdapter(Context context, int resource) {  
  36.             super(context, resource);  
  37.             this.resource = resource;  
  38.         }  
  39.   
  40.         @Override  
  41.         public View getView(int position, View convertView, ViewGroup parent) {  
  42.             if (convertView == null) {  
  43.                 convertView = LayoutInflater.from(mActivity).inflate(resource,  
  44.                         null);  
  45.             }  
  46.   
  47.             ImageView iv = (ImageView) convertView.findViewById(R.id.imageView);  
  48.   
  49.             Glide.with(mActivity).load(img_url).centerCrop()  
  50.             /* 
  51.              * 缺省的占位图片,一般可以设置成一个加载中的进度GIF图 
  52.              */  
  53.             .placeholder(R.drawable.loading).crossFade().into(iv);  
  54.   
  55.             return convertView;  
  56.         }  
  57.   
  58.         @Override  
  59.         public int getCount() {  
  60.             // 假设加载的数据量很大  
  61.             return 10000;  
  62.         }  
  63.     }  
  64. }  

MainActivity.java需要的两个布局文件:

activity_main.xml

[html]  view plain  copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent"  
  4.     android:orientation="vertical" >  
  5.   
  6.     <ListView  
  7.         android:id="@+id/listView"  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="wrap_content" >  
  10.     </ListView>  
  11.   
  12. </LinearLayout>  


item.xml

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <ImageView  
  8.         android:id="@+id/imageView"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content" />  
  11.   
  12. </LinearLayout>  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值