在android开发中我们要进行网络交互请求数据时,避免不了对图片的加载与缓存。下面讲下我用到的方法:
一.运用DisplayImageOption加载。运用架包再进行配置就行(架包已上传)。
//加载图片 public static void getview(String icon_path,ImageView imageview){ ImageLoader loader=ImageLoader.getInstance(); //图片请求路径给ImageView设置图片 loader.displayImage("http://192.168.0.107:8080/LaQuan/"+icon_path, imageview);
在application中创建DisplayImageOption加载
public void onCreate() { super.onCreate(); //加载并缓存图片配置 // 使用DisplayImageOptions.Builder()创建DisplayImageOptions DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder() .cacheInMemory() //1.8.6包使用时候,括号里面传入参数true 设置下载的图片是否缓存在内存中 .cacheOnDisc() //1.8.6包使用时候,括号里面传入参数true 设置下载的图片是否缓存在SD卡中 .build(); ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext()) .defaultDisplayImageOptions(defaultOptions) .threadPriority(Thread.NORM_PRIORITY - 2) .denyCacheImageMultipleSizesInMemory() .discCacheFileNameGenerator(new Md5FileNameGenerator()) .tasksProcessingOrder(QueueProcessingType.LIFO) .build(); ImageLoader.getInstance().init(config); map=new HashMap<String, Object>(); }
二.通过异步任务加载图片保存到缓存
; private LruCache<String,Bitmap> mLruCache; final int maxSize=(int) Runtime.getRuntime().maxMemory(); //运行时最大内存 final int cacheSize=maxSize/5; //设置将最大内存的五分之一用于缓存 public TuanAdapter(ArrayList<Goods> list, Context context) { super(); mLruCache=new LruCache<String,Bitmap>(cacheSize){ protected int sizeOf(String key, Bitmap value) { return value.getByteCount()/1024; } }; }
//通过路劲异步加载图片并给ImageView设置图片 public void lodadBitmap(ImageView imageview,String url){ Bitmap bitmap=null; GetImageAsyncTask asyncTask=new GetImageAsyncTask(imageview,mLruCache); bitmap=asyncTask.getBitmapFromMemoryCache(url); if(bitmap!=null){ //如果缓存中有图片就直接给ImageView设置 imageview.setImageBitmap(bitmap); }else{ //如果缓存中没有图片就进行异步加载 imageview.setImageResource(R.drawable.ic_launcher); asyncTask.execute(url); //通过路劲启动异步加载 } }
异步加载类
public class GetImageAsyncTask extends AsyncTask<String,Integer,Bitmap>{ private ImageView mImageView; private LruCache<String,Bitmap> mLruCache; public GetImageAsyncTask(ImageView mImageView,LruCache<String,Bitmap> mLruCache) { super(); this.mImageView = mImageView; this.mLruCache=mLruCache; } //异步加载图片��̨ ���� @Override protected Bitmap doInBackground(String... url) { Bitmap mBitmap=null; try { URL imgUrl=new URL(url[0]); InputStream is=imgUrl.openConnection().getInputStream(); mBitmap=BitmapFactory.decodeStream(is); addBitmapToMemoryCache(url[0],mBitmap); } catch (IOException e) { e.printStackTrace(); } return mBitmap; } //从内存获取图片 public Bitmap getBitmapFromMemoryCache(String key){ return mLruCache.get(key); } //将图片保存至内存 public void addBitmapToMemoryCache(String key,Bitmap bitmap){ if(mLruCache.get(key)==null){ mLruCache.put(key, bitmap); } } //异步加载后调用方法�ִ�� @Override protected void onPostExecute(Bitmap result) { super.onPostExecute(result); mImageView.setImageBitmap(result); } }
三.运用Google提供Volley架包进行图片的获取与缓存
public class BitmapLruCache implements ImageCache { private LruCache<String, Bitmap> mCache; public BitmapLruCache(){ //设置最大缓存为10M int maxCache=10*1024*1024; mCache = new LruCache<String, Bitmap>(maxCache) { @Override protected int sizeOf(String key, Bitmap value) { return value.getRowBytes() * value.getHeight(); } }; } //从缓存获取图片 @Override public Bitmap getBitmap(String url) { return mCache.get(url); } //将图片存入缓存 @Override public void putBitmap(String url, Bitmap bitmap) { mCache.put(url, bitmap); }
public class App extends Application { /** * 整个App的图片内存缓存类 */ public static BitmapLruCache bitmapLruCache; /** * 整个App内AVOS相关类要用到的RequestQueue */ public static RequestQueue requestQueue; @Override public void onCreate() { super.onCreate(); requestQueue=Volley.newRequestQueue(getApplicationContext()); bitmapLruCache=new BitmapLruCache(); } }
ImageLoader imageLoader=new ImageLoader(App.requestQueue, App.bitmapLruCache); //给ImageView设置图片(imgurl为图片路径) holder.img.setImageUrl(imgurl,imageLoader);