LruCache写完之后没效果呀?
没效果的意思是图片没有加载进去,一片空白。
代码:
SimpleImageLoader.java:
package top.omooo.cachedemo.utils;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.util.LruCache;
import android.widget.ImageView;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
/**
* Created by SSC on 2017/12/6.
* 加载网络图片,并缓存图片到本地和内存
*/
public class SimpleImageLoader {
private static SimpleImageLoader mLoader;
private LruCache<String, Bitmap> mLruCache;
private static final String TAG = "SimpleImageLoader";
public static SimpleImageLoader getInstance() {
if (mLoader == null) {
synchronized (SimpleImageLoader.class) {
if (mLoader == null) {
mLoader = new SimpleImageLoader();
}
}
}
return mLoader;
}
/**
* 用来初始化缓存对象
*/
private SimpleImageLoader() {
int maxSize = (int) (Runtime.getRuntime().maxMemory() / 8);
mLruCache = new LruCache<String, Bitmap>(maxSize){
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getByteCount();
}
};
}
/**
* 用来加载网络图片
* @param view
* @param url
*/
public void displayImage(ImageView view, String url) {
Bitmap bitmap = getBitmapFromCache(url);
if (bitmap != null) {
Log.i(TAG, "displayImage bitmap!=null");
view.setImageBitmap(bitmap);
} else {
Log.i(TAG, "displayImage bitmap==null");
downloadImage(view,url);
view.setImageBitmap(getBitmapFromCache(url));
}
}
/**
* 从缓存中读取图片
* @param url
* @return
*/
private Bitmap getBitmapFromCache(String url) {
Log.i(TAG, "getBitmapFromCache");
return mLruCache.get(url);
}
/**
* 将下载下来的图片保存到缓存中
* @param bitmap
* @param url
*/
private void putBitmapToCache(Bitmap bitmap, String url) {
if (bitmap != null) {
Log.i(TAG, "putBitmapToCache");
mLruCache.put(url, bitmap);
}
}
/**
* 下载图片,并添加到缓存中
* @param imageView
* @param url
*/
private void downloadImage(final ImageView imageView, final String url) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(url).build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.i(TAG, "onFailure");
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.i(TAG, "onResponse");
Bitmap bitmap = BitmapFactory.decodeStream(response.body().byteStream());
if (bitmap != null) {
putBitmapToCache(bitmap, url);
Log.i(TAG, "bitmap不为空");
} else {
Log.i(TAG, "bitmap为空");
}
}
});
}
}
然后在MainActivity里面调用:
SimpleImageLoader.getInstance().displayImage(mImageView, url);
运行没效果,呜呜呜