packagecom.example.imagescan;importjava.util.concurrent.ExecutorService;importjava.util.concurrent.Executors;importandroid.graphics.Bitmap;importandroid.graphics.BitmapFactory;importandroid.graphics.Point;importandroid.os.Handler;importandroid.os.Message;importandroid.support.v4.util.LruCache;/***本地图片加载器,采用的是异步解析本地图片,单例模式利用getInstance()获取NativeImageLoader实例*调用loadNativeImage()方法加载本地图片,此类可作为一个加载本地图片的工具类**@bloghttp://blog.csdn.net/xiaanming**@authorxiaanming**/publicclassNativeImageLoader{privateLruCache<String,Bitmap>mMemoryCache;privatestaticNativeImageLoadermInstance=newNativeImageLoader();privateExecutorServicemImageThreadPool=Executors.newFixedThreadPool(1);privateNativeImageLoader(){//获取应用程序的最大内存finalintmaxMemory=(int)(Runtime.getRuntime().maxMemory()/1024);//用最大内存的1/4来存储图片finalintcacheSize=maxMemory/4;mMemoryCache=newLruCache<String,Bitmap>(cacheSize){//获取每张图片的大小@OverrideprotectedintsizeOf(Stringkey,Bitmapbitmap){returnbitmap.getRowBytes()*bitmap.getHeight()/1024;}};}/***通过此方法来获取NativeImageLoader的实例*@return*/publicstaticNativeImageLoadergetInstance(){returnmInstance;}/***加载本地图片,对图片不进行裁剪*@parampath*@parammCallBack*@return*/publicBitmaploadNativeImage(finalStringpath,finalNativeImageCallBackmCallBack){returnthis.loadNativeImage(path,null,mCallBack);}/***此方法来加载本地图片,这里的mPoint是用来封装ImageView的宽和高,我们会根据ImageView控件的大小来裁剪Bitmap*如果你不想裁剪图片,调用loadNativeImage(finalStringpath,finalNativeImageCallBackmCallBack)来加载*@parampath*@parammPoint*@parammCallBack*@return*/publicBitmaploadNativeImage(finalStringpath,finalPointmPoint,finalNativeImageCallBackmCallBack){//先获取内存中的BitmapBitmapbitmap=getBitmapFromMemCache(path);finalHandlermHander=newHandler(){@OverridepublicvoidhandleMessage(Messagemsg){super.handleMessage(msg);mCallBack.onImageLoader((Bitmap)msg.obj,path);}};//若该Bitmap不在内存缓存中,则启用线程去加载本地的图片,并将Bitmap加入到mMemoryCache中if(bitmap==null){mImageThreadPool.execute(newRunnable(){@Overridepublicvoidrun(){//先获取图片的缩略图BitmapmBitmap=decodeThumbBitmapForFile(path,mPoint==null?0:mPoint.x,mPoint==null?0:mPoint.y);Messagemsg=mHander.obtainMessage();msg.obj=mBitmap;mHander.sendMessage(msg);//将图片加入到内存缓存addBitmapToMemoryCache(path,mBitmap);}});}returnbitmap;}/***往内存缓存中添加Bitmap**@paramkey*@parambitmap*/privatevoidaddBitmapToMemoryCache(Stringkey,Bitmapbitmap){if(getBitmapFromMemCache(key)==null&&bitmap!=null){mMemoryCache.put(key,bitmap);}}/***根据key来获取内存中的图片*@paramkey*@return*/privateBitmapgetBitmapFromMemCache(Stringkey){returnmMemoryCache.get(key);}/***根据View(主要是ImageView)的宽和高来获取图片的缩略图*@parampath*@paramviewWidth*@paramviewHeight*@return*/privateBitmapdecodeThumbBitmapForFile(Stringpath,intviewWidth,intviewHeight){BitmapFactory.Optionsoptions=newBitmapFactory.Options();//设置为true,表示解析Bitmap对象,该对象不占内存options.inJustDecodeBounds=true;BitmapFactory.decodeFile(path,options);//设置缩放比例options.inSampleSize=computeScale(options,viewWidth,viewHeight);//设置为false,解析Bitmap对象加入到内存中options.inJustDecodeBounds=false;returnBitmapFactory.decodeFile(path,options);}/***根据View(主要是ImageView)的宽和高来计算Bitmap缩放比例。默认不缩放*@paramoptions*@paramwidth*@paramheight*/privateintcomputeScale(BitmapFactory.Optionsoptions,intviewWidth,intviewHeight){intinSampleSize=1;if(viewWidth==0||viewWidth==0){returninSampleSize;}intbitmapWidth=options.outWidth;intbitmapHeight=options.outHeight;//假如Bitmap的宽度或高度大于我们设定图片的View的宽高,则计算缩放比例if(bitmapWidth>viewWidth||bitmapHeight>viewWidth){intwidthScale=Math.round((float)bitmapWidth/(float)viewWidth);intheightScale=Math.round((float)bitmapHeight/(float)viewWidth);//为了保证图片不缩放变形,我们取宽高比例最小的那个inSampleSize=widthScale<heightScale?widthScale:heightScale;}returninSampleSize;}/***加载本地图片的回调接口**@authorxiaanming**/publicinterfaceNativeImageCallBack{/***当子线程加载完了本地的图片,将Bitmap和图片路径回调在此方法中*@parambitmap*@parampath*/publicvoidonImageLoader(Bitmapbitmap,Stringpath);}}


更多相关文章

  1. Android(安卓)启动线程OOM
  2. android 加载第三方库
  3. android保存预览图片
  4. android 拖拽图片
  5. android 內存等使用情況(转)
  6. android .9 图片的全能适配方法
  7. Android中图片的放大和缩小源码
  8. Android(安卓)3D图片切换
  9. Android(安卓)之内置内存卡与外置存储卡判断

随机推荐

  1. Android开发常用开源库
  2. 重温Android四大组件(一)—Activity的生
  3. 【Android】错误:Installation error: INS
  4. 10大开源Android项目
  5. 【Android】Android(安卓)线程&进程模型
  6. Android ICS关机跟重新启动功能研究
  7. Android Handler那些事儿(一)——是什么,有
  8. Android视频开发基础(三)
  9. Android应用程序结构--比较偏的考试题目
  10. Android 启动模式