android 辅助工具类—— 图片缓冲池
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了android 辅助工具类—— 图片缓冲池,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4741字,纯文字阅读大概需要7分钟。
内容图文
在现在的很多应用中,单一的文字描述往往达不到图片直接展示所带来的效果,这样的需求对开发人员来说,就要经常和图片处理工作打交道。本着“莫要重复发明轮子”的精神下面分享一个图片处理类帮助我们做一些简单的工作。
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(); } }
原文:http://blog.csdn.net/xuduzhoud/article/details/22588229
内容总结
以上是互联网集市为您收集整理的android 辅助工具类—— 图片缓冲池全部内容,希望文章能够帮你解决android 辅助工具类—— 图片缓冲池所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。