bitmap缓存
Posted halo-yang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了bitmap缓存相关的知识,希望对你有一定的参考价值。
1.获取LruCache对象
/** * 获取内存的1/8用作缓存空间 */ private fun getCache(): LruCache<String, Bitmap> { val maxMemory = Runtime.getRuntime().maxMemory()/1024 val cacheSize = maxMemory / 8 val lruCache = LruCache<String, Bitmap>(cacheSize.toInt()) return lruCache }
2.根据key来获取Bitmap
/** * 获取缓存 * @param key */ private fun getBitmapFromMemeory(key:String):Bitmap{ return mLruCache?.get(key)!! }
3.增加bitmap到缓存
/** * 增加bitmap到缓存 * @param key * @param bitmap */ private fun addBitmapToMemeory(key:String,bitmap:Bitmap){ if (getBitmapFromMemeory(key) == null){ mLruCache?.put(key,bitmap) } }
4.通过给定资源来源,和显示的imageView来加载图片
/** * imageView 加载图片 * @param resId 资源id * @param imageView */ fun loadBitmap(resId:Int,imageView: ImageView){ val key = resId.toString() val bitmap = mLruCache?.get(key) if (bitmap != null){ imageView.setImageBitmap(bitmap) }else{ val bitmapWorkerTask = BitmapWorkerTask(this, imageView) //无缓存 加载任务异步执行 bitmapWorkerTask.execute(resId) } }
5.BitmapWorkerTask继承AsyncTask,实现异步加载图片
1 class BitmapWorkerTask extends AsyncTask{ 2 3 private Context mContext; 4 private final WeakReference mImageViewReference; 5 private int data = 0; 6 7 public BitmapWorkerTask(Context context,ImageView imageView) { 8 mImageViewReference = new WeakReference(imageView); 9 this.mContext = context; 10 } 11 12 13 /** 14 * 异步执行代码 15 * @param objects 16 * @return 17 */ 18 @Override 19 protected Object doInBackground(Object[] objects) { 20 data = (int) objects[0]; 21 return decodeSampledBitmapFromResource(mContext.getResources(),data,100,100); 22 } 23 24 /** 25 * 执行完成UI线程执行代码 26 * @param o 27 */ 28 @Override 29 protected void onPostExecute(Object o) { 30 if (isCancelled()){ 31 o = null; 32 } 33 34 if (mImageViewReference != null && o != null){ 35 final ImageView imageView = (ImageView) mImageViewReference.get(); 36 final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView); 37 if (this == bitmapWorkerTask && imageView !=null) { 38 imageView.setImageBitmap((Bitmap) o); 39 } 40 } 41 } 42 43 /** 44 * 获取合适的Bitmap 45 * @param res 46 * @param resId 47 * @param reqWidth 48 * @param reqHeight 49 * @return 50 */ 51 public Bitmap decodeSampledBitmapFromResource(Resources res, int resId, 52 int reqWidth, int reqHeight) { 53 54 // First decode with inJustDecodeBounds=true to check dimensions 55 final BitmapFactory.Options options = new BitmapFactory.Options(); 56 options.inJustDecodeBounds = true; 57 BitmapFactory.decodeResource(res, resId, options); 58 59 // Calculate inSampleSize 60 options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 61 62 // Decode bitmap with inSampleSize set 63 options.inJustDecodeBounds = false; 64 return BitmapFactory.decodeResource(res,resId, options); 65 } 66 67 /** 68 * 根据给定宽高计算出缩放值 69 * @param options 70 * @param reqWidth 71 * @param reqHeight 72 * @return 73 */ 74 private int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { 75 int outHeight = options.outHeight; 76 int outWidth = options.outWidth; 77 int inSampleSie = 1; 78 if (outHeight>reqHeight || outWidth>reqWidth){ 79 int halfHeight = outHeight / 2; 80 int halfWidth = outWidth / 2; 81 while (halfHeight/inSampleSie>reqHeight && halfWidth/inSampleSie > reqWidth){ 82 inSampleSie*=2; 83 } 84 } 85 return inSampleSie; 86 } 87 }
以上是关于bitmap缓存的主要内容,如果未能解决你的问题,请参考以下文章