Advertisement
Guest User

Untitled

a guest
Feb 14th, 2016
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. public class BitmapLruCache extends LruCache<String, Bitmap> implements ImageCache {
  2.  
  3. public static int getDefaultLruCacheSize() {
  4. final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
  5. final int cacheSize = maxMemory / 6;
  6.  
  7. return cacheSize;
  8. }
  9.  
  10. public BitmapLruCache() {
  11. this(getDefaultLruCacheSize());
  12. }
  13.  
  14. public BitmapLruCache(int sizeInKiloBytes) {
  15. super(sizeInKiloBytes);
  16. }
  17.  
  18. @Override
  19. protected int sizeOf(String key, Bitmap value) {
  20. return value.getRowBytes() * value.getHeight() / 1024;
  21. }
  22.  
  23. @Override
  24. public Bitmap getBitmap(String url) {
  25. return get(url);
  26. }
  27.  
  28. @Override
  29. public void putBitmap(String url, Bitmap bitmap) {
  30. put(url, bitmap);
  31. }
  32. }
  33.  
  34. public class VolleyHelper {
  35.  
  36. private static final int MAX_IMAGE_CACHE_ENTIRES = 100;
  37. private static RequestQueue mRequestQueue;
  38. private static ImageLoader mImageLoader;
  39.  
  40. private VolleyHelper() {
  41. }
  42.  
  43. public static void init(Context context) {
  44. mRequestQueue = Volley.newRequestQueue(context);
  45. mImageLoader = new ImageLoader(mRequestQueue, new BitmapLruCache(MAX_IMAGE_CACHE_ENTIRES));
  46. }
  47.  
  48. public static RequestQueue getRequestQueue() {
  49. if (mRequestQueue != null) {
  50. return mRequestQueue;
  51. } else {
  52. throw new IllegalStateException("RequestQueue not initialized");
  53. }
  54. }
  55.  
  56. public static ImageLoader getImageLoader() {
  57. if (mImageLoader != null) {
  58. return mImageLoader;
  59. } else {
  60. throw new IllegalStateException("ImageLoader not initialized");
  61. }
  62. }
  63. }
  64.  
  65. ImageLoader mImageLoader = VolleyHelper.getImageLoader();
  66.  
  67. if (mImageLoader.isCached(url, maxWidth, maxheight)) {
  68. //always false
  69. }
  70.  
  71. /**
  72. * Checks if the item is available in the cache.
  73. * @param requestUrl The url of the remote image
  74. * @param maxWidth The maximum width of the returned image.
  75. * @param maxHeight The maximum height of the returned image.
  76. * @return True if the item exists in cache, false otherwise.
  77. */
  78. public boolean isCached(String requestUrl, int maxWidth, int maxHeight) {
  79. throwIfNotOnMainThread();
  80.  
  81. String cacheKey = getCacheKey(requestUrl, maxWidth, maxHeight);
  82. return mCache.getBitmap(cacheKey) != null;
  83. }
  84.  
  85. if (mImageLoader.isCached(imageUrl, 0, 0)) {
  86. // code here...
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement