AsyncImageLoader.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package com.easemob.easeui.oss;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.graphics.Bitmap;
  5. import android.widget.ImageView;
  6. import java.io.File;
  7. import java.util.ArrayList;
  8. import java.util.Collections;
  9. import java.util.List;
  10. import java.util.Map;
  11. import java.util.WeakHashMap;
  12. import java.util.concurrent.ExecutorService;
  13. import java.util.concurrent.Executors;
  14. public class AsyncImageLoader {
  15. private MemoryCache mMemoryCache;
  16. private FileCache mFileCache;
  17. private ExecutorService mExecutorService;
  18. private Context context;
  19. private Map<ImageView, String> mImageViews = Collections
  20. .synchronizedMap(new WeakHashMap<ImageView, String>());
  21. private List<LoadPhotoTask> mTaskQueue = new ArrayList<LoadPhotoTask>();
  22. public AsyncImageLoader(Context context, MemoryCache memoryCache, FileCache fileCache) {
  23. this.context = context;
  24. mMemoryCache = memoryCache;
  25. mFileCache = fileCache;
  26. mExecutorService = Executors.newFixedThreadPool(5);
  27. }
  28. public Bitmap loadBitmap(ImageView imageView, String url) {
  29. mImageViews.put(imageView, url);
  30. Bitmap bitmap = mMemoryCache.get(url);
  31. if(bitmap == null) {
  32. enquequeLoadPhoto(url, imageView);
  33. }
  34. return bitmap;
  35. }
  36. private void enquequeLoadPhoto(String url, ImageView imageView) {
  37. if(isTaskExisted(url))
  38. return;
  39. LoadPhotoTask task = new LoadPhotoTask(url, imageView);
  40. synchronized (mTaskQueue) {
  41. mTaskQueue.add(task);
  42. }
  43. mExecutorService.execute(task);
  44. }
  45. private boolean isTaskExisted(String url) {
  46. if(url == null)
  47. return false;
  48. synchronized (mTaskQueue) {
  49. int size = mTaskQueue.size();
  50. for(int i=0; i<size; i++) {
  51. LoadPhotoTask task = mTaskQueue.get(i);
  52. if(task != null && task.getUrl().equals(url))
  53. return true;
  54. }
  55. }
  56. return false;
  57. }
  58. private Bitmap getBitmapByUrl(String url) {
  59. File f = mFileCache.getFile(url);
  60. Bitmap b = ImageUtil.decodeFile(f);
  61. if (b != null)
  62. return b;
  63. return ImageUtil.loadBitmapFromWeb(context,url, f);
  64. }
  65. private boolean imageViewReused(ImageView imageView, String url) {
  66. String tag = mImageViews.get(imageView);
  67. if (tag == null || !tag.equals(url))
  68. return true;
  69. return false;
  70. }
  71. private void removeTask(LoadPhotoTask task) {
  72. synchronized (mTaskQueue) {
  73. mTaskQueue.remove(task);
  74. }
  75. }
  76. class LoadPhotoTask implements Runnable {
  77. private String url;
  78. private ImageView imageView;
  79. LoadPhotoTask(String url, ImageView imageView) {
  80. this.url = url;
  81. this.imageView = imageView;
  82. }
  83. @Override
  84. public void run() {
  85. if (imageViewReused(imageView, url)) {
  86. removeTask(this);
  87. return;
  88. }
  89. Bitmap bmp = getBitmapByUrl(url);
  90. mMemoryCache.put(url, bmp);
  91. if (!imageViewReused(imageView, url)) {
  92. BitmapDisplayer bd = new BitmapDisplayer(bmp, imageView, url);
  93. Activity a = (Activity) imageView.getContext();
  94. a.runOnUiThread(bd);
  95. }
  96. removeTask(this);
  97. }
  98. public String getUrl() {
  99. return url;
  100. }
  101. }
  102. class BitmapDisplayer implements Runnable {
  103. private Bitmap bitmap;
  104. private ImageView imageView;
  105. private String url;
  106. public BitmapDisplayer(Bitmap b, ImageView imageView, String url) {
  107. bitmap = b;
  108. this.imageView = imageView;
  109. this.url = url;
  110. }
  111. public void run() {
  112. if (imageViewReused(imageView, url))
  113. return;
  114. if (bitmap != null)
  115. imageView.setImageBitmap(bitmap);
  116. }
  117. }
  118. public void destroy() {
  119. mMemoryCache.clear();
  120. mMemoryCache = null;
  121. mImageViews.clear();
  122. mImageViews = null;
  123. mTaskQueue.clear();
  124. mTaskQueue = null;
  125. mExecutorService.shutdown();
  126. mExecutorService = null;
  127. }
  128. }