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);
運行沒效果,嗚嗚嗚
2017-12-21
?@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為空");
? ? ? ? ? ? ? ?}
? ? ? ? ? ?}
? ? ? ?});
?上面這個方法的執行時異步的
然而在首次加載圖片的時候bitmap == null? 會調用系統的OKHTTP去下載圖片(這里需要你斷點一下是不是真的下載好了),假設下載成功了。但是在下載成功之前代碼已經執行到了?view.setImageBitmap(getBitmapFromCache(url)); 這時候圖片還沒有下載好
?/**
? ? * 用來加載網絡圖片
? ? * @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));
? ? ? ?}
? ?}
正確的解決是當圖片下載完成了,直接加載給view
?@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);
? ????????????????displayImage(ImageView view, String url);? ? ? ??
????? ? ? ? ? ? ? Log.i(TAG, "bitmap不為空");
? ? ? ? ? ? ? ?} else {
? ? ? ? ? ? ? ? ? ?Log.i(TAG, "bitmap為空");
? ? ? ? ? ? ? ?}
? ? ? ? ? ?}
? ? ? ?});
這樣就可以了
2018-08-23
視頻里面有遺漏,下載完成還需要在set一下,下載過程是異步的,所以在download后面直接set是不行的。還有一點,okhttp的onResponse是在非UI線程回調,不要直接操作ui