在现在的很多应用中,单一的文字描述往往达不到图片直接展示所带来的效果,这样的需求对开发人员来说,就要经常和图片处理工作打交道。本着“莫要重复发明*”的精神下面分享一个图片处理类帮助我们做一些简单的工作。
import java.io.FileInputStream; import java.io.InputStream; import java.lang.ref.ReferenceQueue; import java.lang.ref.SoftReference; import java.util.Hashtable; import java.util.Queue; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.BitmapFactory.Options; /** 图片高速缓存池 * */ public class BitmapMgr { private Hashtable<String, BitmapRef> mBitmaps; //图片软引用表 private Hashtable<String, Options> mOptions; //图片属性哈西表 private ReferenceQueue<Bitmap> mRefQueue; //图片引用队列 private static BitmapMgr mStaticBitmap; private BitmapMgr(){ mBitmaps = new Hashtable<String, BitmapRef>(); mRefQueue = new ReferenceQueue<Bitmap>(); mOptions = new Hashtable<String, Options>(); } public static BitmapMgr getInstance(){ if(mStaticBitmap == null){ mStaticBitmap = new BitmapMgr(); } return mStaticBitmap; } public Bitmap getBitmap(String path,Options options){ Bitmap bitmap = null; try{ if(path == null || path.equals("")) return bitmap; BitmapRef ref = mBitmaps.get(path); if (ref != null) bitmap = ref.get(); if ((bitmap == null || bitmap.isRecycled())) { mBitmaps.remove(path); // Log.e("LOG", "Cantians:" + mBitmaps.containsKey(path) + ",没有缓存:" + path); if (path.startsWith("/res/raw/")) { InputStream in = getClass().getResourceAsStream(path); bitmap = BitmapFactory.decodeStream(in, null, options); ref = new BitmapRef(bitmap, mRefQueue); mBitmaps.put(path, ref); } else { bitmap = BitmapFactory.decodeFile(path, options); ref = new BitmapRef(bitmap, mRefQueue); mBitmaps.put(path, ref); } } else { // Log.e("LOG", "Cantians:" + mBitmaps.containsKey(path) + ",有缓存:"+ path); } }catch (Exception e) { // TODO: handle exception bitmap = null; } return bitmap; } public Bitmap getBitmap(String path, Options options ,boolean isTag){ Bitmap bitmap = null; try{ if(path == null || path.equals("")) return bitmap; if(!isTag) return getBitmap(path, options); BitmapRef ref = mBitmaps.get(path); if (ref != null) bitmap = ref.get(); if ((bitmap == null || bitmap.isRecycled())) { mBitmaps.remove(path); if (path.startsWith("/res/raw/")) { InputStream in = getClass().getResourceAsStream(path); bitmap = BitmapFactory.decodeStream(in, null, options); ref = new BitmapRef(bitmap, mRefQueue); mBitmaps.put(path, ref); } else { FileInputStream in = new FileInputStream(path); byte[] buf = new byte[16]; if (in.read(buf, 0, 13) < 13) return null; String tag = new String(buf, 0, 13, "utf-8"); if (tag.equalsIgnoreCase("Book_Cover")) { bitmap = BitmapFactory.decodeStream(in); } else { bitmap = BitmapFactory.decodeFile(path, options); } in.close(); ref = new BitmapRef(bitmap, mRefQueue); mBitmaps.put(path, ref); } } else { // Log.e("LOG", "Cantians:" + mBitmaps.containsKey(path) + ",有缓存:"+ path); } }catch (Exception e) { // TODO: handle exception bitmap = null; } return bitmap; } public Bitmap getBitmap(int resId,Options options){ String key = Integer.toString(resId); Bitmap bitmap = null; BitmapRef ref = mBitmaps.get(key); if (ref != null) bitmap = ref.get(); if(bitmap == null || bitmap.isRecycled()){ mBitmaps.remove(key); Resources res = APP.getAppContext().getResources(); bitmap = BitmapFactory.decodeResource(res, resId,options); ref = new BitmapRef(bitmap, mRefQueue); mBitmaps.put(key, ref); } return bitmap; } public Bitmap getBitmap(InputStream in,String key,Options options){ Bitmap bitmap = null; BitmapRef ref = mBitmaps.get(key); if (ref != null) bitmap = ref.get(); if(bitmap == null || bitmap.isRecycled()){ mBitmaps.remove(key); bitmap = BitmapFactory.decodeStream(in, null, options); ref = new BitmapRef(bitmap, mRefQueue); mBitmaps.put(key, ref); } return bitmap; } public Bitmap getBitmap(BitmapCreate creater,String key){ Bitmap bitmap = null; BitmapRef ref = mBitmaps.get(key); if (ref != null) bitmap = ref.get(); if(bitmap == null || bitmap.isRecycled()){ mBitmaps.remove(key); bitmap = creater.createBitmap(); ref = new BitmapRef(bitmap, mRefQueue); mBitmaps.put(key, ref); } return bitmap; } public Options getBitmapOptions(InputStream in, String key,Options options){ Options op = mOptions.get(key); if(op == null){ mOptions.remove(key); BitmapFactory.decodeStream(in, null, options); op = options; mOptions.put(key, op); } return op; } public void clearOptionsMap(){ mOptions.clear(); } private class BitmapRef extends SoftReference<Bitmap>{ public BitmapRef(Bitmap r, ReferenceQueue<? super Bitmap> q) { super(r, q); } } public interface BitmapCreate{ Bitmap createBitmap(); } }