android 輔助工具類—— 圖片緩衝池

在現在的很多應用中,單一的文字描述往往達不到圖片直接展示所帶來的效果,這樣的需求對開發人員來說,就要經常和圖片處理工作打交道。本着“莫要重複發明輪子”的精神下面分享一個圖片處理類幫助我們做一些簡單的工作。


 

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();
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章