在现在的很多应用中,单一的文字描述往往达不到图片直接展示所带来的效果,这样的需求对开发人员来说,就要经常和图片处理工作打交道。本着“莫要重复发明轮子”的精神下面分享一个图片处理类帮助我们做一些简单的工作。


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


更多相关文章

  1. android 拍照或者图库选择 压缩后 图片 上传
  2. android 9.png 图片制作
  3. NDK DEBUG方法
  4. JNI 引用问题梳理
  5. Android(安卓)JNI用bitmap来实现Imageview的上下翻转和左右镜像
  6. Android(安卓)文件夹介绍
  7. Android(安卓)图片转动效果(一)
  8. android避免内存泄露
  9. Android(安卓)中 Base64 转换成 图片

随机推荐

  1. Android中知识点整理
  2. android解锁触发广播
  3. Android应用程序(activity)启动过程(一)
  4. Android版本28使用http请求报错not permi
  5. android 源码下载、编译和测试
  6. Android PKMS深入分析安装应用
  7. Android初级到高级指南(精华版)
  8. Android中使用ListView绘制自定义表格(2)
  9. android mapview 学习总结
  10. Android(安卓)studio 3.4中gradle版本5.1