Skip to content

Commit

Permalink
1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
赌徒 authored and 赌徒 committed Feb 21, 2020
1 parent 805248a commit 4fb2e42
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.junl.utils.ImageloaderUtils;

import android.graphics.Bitmap;
import android.util.LruCache;

public class ImageCache {
LruCache<String, Bitmap> mImageCache;

public ImageCache() {
initImageCache();
}

private void initImageCache() {
//计算可使用的最大内存
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
//取四分之一的可用内存作为缓存
final int cacheSize = maxMemory / 4;

mImageCache = new LruCache<String, Bitmap>(cacheSize) {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
return bitmap.getRowBytes() * bitmap.getHeight() / 1024;
}
};
}

public void put(String url, Bitmap bitmap) {
mImageCache.put(url, bitmap);
}

public Bitmap get(String url) {
return mImageCache.get(url);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,27 @@

public class ImageLoader {
//图片缓存
LruCache<String, Bitmap> mImageCache;
ImageCache mImageCache = new ImageCache();
//线程池,线程池数量为 CPU 的数量
ExecutorService mExecutorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
//UI Handler
Handler mUIHandler = new Handler(Looper.getMainLooper());

public ImageLoader() {
initImageCache();
}

private void initImageCache() {
//计算可使用的最大内存
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
//取四分之一的可用内存作为缓存
final int cacheSize = maxMemory / 4;

mImageCache = new LruCache<String, Bitmap>(cacheSize) {
private void updateImageView(final ImageView imageView, final Bitmap bitmap) {
mUIHandler.post(new Runnable() {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
return bitmap.getRowBytes() * bitmap.getHeight() / 1024;
public void run() {
imageView.setImageBitmap(bitmap);
}
};
});
}

public void displayImage(final String url, final ImageView imageView) {
Bitmap bitmap = mImageCache.get(url);
if(bitmap != null){
imageView.setImageBitmap(bitmap);
}

imageView.setTag(url);
mExecutorService.submit(new Runnable() {
@Override
Expand All @@ -55,15 +51,6 @@ public void run() {
});
}

private void updateImageView(final ImageView imageView, final Bitmap bitmap) {
mUIHandler.post(new Runnable() {
@Override
public void run() {
imageView.setImageBitmap(bitmap);
}
});
}

public Bitmap downloadImage(String imageUrl) {
Bitmap bitmap = null;
try {
Expand Down

0 comments on commit 4fb2e42

Please sign in to comment.