Android开发之超强图片工具类BitmapUtil完整实例

  

本文实例讲述了Android开发之超强图片工具类BitmapUtil。分享给大家供大家参考,具体如下:

  

说明:为了方便大家使用,本人把大家常用的图片处理代码集中到这个类里

  

使用了LruCache与SoftReference

     /* *   *图片加载及转化工具- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -延伸:一个位图到底占用多大内存?系统给每个应用程序分配多大内存?位图占用的内存为:像素总数   * *每个像素占用的内存。在Android中,位图有四种像素类型:ARGB_8888, ARGB_4444, ARGB_565, ALPHA_8,他们每个像素占用的字节数分别为4、2、2、1,因此,一个2000 * 1000的ARGB_8888   *类型的位图占用的内存为2000 * 1000 * 4=8000000 b=8 mb。   *   * @author chen.lin   *   */公开课BitmapUtil {/* *   * 1)软引用,已经不适合缓存图片信息,加载图片时会出现重叠的现象   * 2)Android 3.0 (API级别11)中,图片的数据会存储在本地的内存当中   *因而无法用一种可预见的方式将其释放,这就有潜在的风险造成应用程序的内存溢出并崩溃,   * 3)因为从Android 2.3 (API级别9)开始,垃圾回收器会更倾向于回收持有软引用或弱引用的对象,   这让软引用和弱引用变得不再可靠。   *   */私有静态Map<字符串,SoftReference比;imageCache=new HashMap<字符串,SoftReference在();/* *   *初始化lrucache,最少使用最先移除,lrucache来缓存图片,   *当存储形象的大小大于LruCache设定的值,系统自动释放内存,   */私有静态LruCache<字符串,Bitmap>mMemoryCache;   静态{   最后一个int内存=(int) (Runtime.getRuntime () .maxMemory ()/1024);   最后一个int cacheSize=内存/8;   mMemoryCache=new LruCache<字符串,Bitmap> (cacheSize) {   保护int sizeOf(字符串键,位图值){//返回value.getByteCount ()/1024;   返回value.getHeight () * value.getRowBytes ();   }   };   }//?lrucache - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -/* *   *添加图片到lrucache   *   * @param关键   * @param位图   */公共同步空白addBitmapToMemCache(字符串键,位图的位图){   如果(getBitmapFromMemCache(关键)==null) {   如果(钥匙!=零,位图!=null) {   mMemoryCache。把(关键,位图);   }   }   }/* *   *清除缓存   */公共空间clearMemCache () {   如果(mMemoryCache !=null) {   如果(mMemoryCache.size()在0){   mMemoryCache.evictAll ();   }   mMemoryCache=零;   }   }/* *   *移除缓存   */公共同步空白removeMemCache (String键){   如果(关键!=null) {   如果(mMemoryCache !=null) {   位图bm=mMemoryCache.remove(关键);   如果(bm !=null)   bm.recycle ();   }   }   }/* *   *从lrucache里读取图片   *   * @param关键   * @return   */公共位图getBitmapFromMemCache (String键){   如果(关键!=null) {   返回mMemoryCache.get(关键);   }   返回null;   }/* *   *加载图片   *   * @param上下文   * @param渣油   * @param imageView   */公共空间loadBitmap(上下文语境、int渣油ImageView ImageView) {   最终字符串imageKey=String.valueOf(渣油);   最后一位图的位图=getBitmapFromMemCache (imageKey);   如果(位图!=null) {   imageView.setImageBitmap(位图);   其他}{   imageView.setImageResource(渣油);   BitmapWorkerTask任务=new BitmapWorkerTask(上下文);   task.execute(渣油);   }   }/* *   *任务类   *   * @Project App_View   * @Package com.android.view.tool   * @author chenlin   * @version 1.0   * @Date 2014年5月10日   */类BitmapWorkerTask AsyncTask<延伸;整数,空虚,Bitmap>{   私人上下文mContext;   公共BitmapWorkerTask(上下文语境){   mContext=上下文;   }//在后台加载图片。   @Override   保护位图doInBackground(整数…params) {   最后一位图的位图=decodeSampledBitmapFromResource (mContext.getResources (), params[0], 100年,100年);   addBitmapToMemCache (String.valueOf (params[0]),位图);   返回位图;   }   }//硪? - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -   公共静态孔隙addBitmapToCache (String路径){//强引用的位图对象   位图的位图=BitmapFactory.decodeFile(路径);//软引用的位图对象   SoftReferencesoftBitmap=new SoftReference

Android开发之超强图片工具类BitmapUtil完整实例