android 辅助工具类—— 图片缓冲池
16lz
2021-01-26
在现在的很多应用中,单一的文字描述往往达不到图片直接展示所带来的效果,这样的需求对开发人员来说,就要经常和图片处理工作打交道。本着“莫要重复发明轮子”的精神下面分享一个图片处理类帮助我们做一些简单的工作。
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 exceptionbitmap = 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 exceptionbitmap = 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();}}
更多相关文章
- android 拍照或者图库选择 压缩后 图片 上传
- android 9.png 图片制作
- NDK DEBUG方法
- JNI 引用问题梳理
- Android(安卓)JNI用bitmap来实现Imageview的上下翻转和左右镜像
- Android(安卓)文件夹介绍
- Android(安卓)图片转动效果(一)
- android避免内存泄露
- Android(安卓)中 Base64 转换成 图片