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到緩存ide
/** * 增長bitmap到緩存 * @param key * @param bitmap */ private fun addBitmapToMemeory(key:String,bitmap:Bitmap){ if (getBitmapFromMemeory(key) == null){ mLruCache?.put(key,bitmap) } }
4.經過給定資源來源,和顯示的imageView來加載圖片this
/** * 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,實現異步加載圖片spa
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 }