求解 使用 LruCache缓存 后出现的问题
使用 LruCache缓存 后 第一屏图片加载 不出来 ,来回滑动后 图片才能正常显示 请问这是什么原因呢
是
new ImageLoder().showImageByAsyncTask(viewHolder.ivIcon,url);
替换成成
loder.showImageByAsyncTask(viewHolder.ivIcon,url);
出现的问题么
使用 LruCache缓存 后 第一屏图片加载 不出来 ,来回滑动后 图片才能正常显示 请问这是什么原因呢
是
new ImageLoder().showImageByAsyncTask(viewHolder.ivIcon,url);
替换成成
loder.showImageByAsyncTask(viewHolder.ivIcon,url);
出现的问题么
2015-12-03
这个是加载图片的代码 package com.example.asynctask2; import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import android.annotation.SuppressLint; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.os.Handler; import android.os.Message; import android.util.Log; import android.util.LruCache; import android.widget.ImageView; @SuppressLint("NewApi") public class ImageLoder { private ImageView mImageView; //图片缓存 private LruCache<String,Bitmap> mcache; private String mUrl; public ImageLoder(){ //获取最大可用内存 int maxMemory=(int) Runtime.getRuntime().maxMemory(); int cacheSize=maxMemory/4; //Log.i("maxMemory",""+cacheSize); mcache = new LruCache<String, Bitmap>(cacheSize){ //必须重写此方法,来测量Bitmap的大小 @Override protected int sizeOf(String key, Bitmap value) { return value.getRowBytes(); } }; } /** * 添加Bitmap到内存缓存 * @param key * @param bitmap */ public void addBitmapToMemoryCache(String key, Bitmap bitmap) { if (getBitmapFromMemCache(key) == null && bitmap != null) { mcache.put(key, bitmap); } } /** * 从内存缓存中获取一个Bitmap * @param key * @return */ public Bitmap getBitmapFromMemCache(String key) { return mcache.get(key); } Handler handler=new Handler(){ public void handleMessage(android.os.Message msg) { super.handleMessage(msg); if(mImageView.getTag().equals(mUrl)){ mImageView.setImageBitmap((Bitmap) msg.obj); } }; }; public void showImageByThread(ImageView imageview,final String url){ mImageView=imageview; mUrl=url; new Thread(){ public void run() { super.run(); Bitmap bitmap=getBitmapFromURL(url); Message msg=Message.obtain(); msg.obj=bitmap; handler.sendMessage(msg); } }.start(); } /** * 使用异步任务获取网络图片 * @param imageview * @param url */ public void showImageByAsyncTask(ImageView imageview,final String url){ Bitmap bitmap = getBitmapFromMemCache(url); if(bitmap==null){ new ImgNetByAsyncTask(imageview,url).execute(url); }else{ imageview.setImageBitmap(bitmap); } } private class ImgNetByAsyncTask extends AsyncTask<String, Void, Bitmap>{ private ImageView tImageView; public ImgNetByAsyncTask(ImageView imageview,String url){ tImageView=imageview; mUrl=url; } protected Bitmap doInBackground(String... params) { String url=params[0]; Bitmap bitmap = getBitmapFromURL(url); if(bitmap!=null){ addBitmapToMemoryCache(url,bitmap); } return bitmap; } protected void onPostExecute(Bitmap bitmap) { super.onPostExecute(bitmap); if(tImageView.getTag().equals(mUrl)){ tImageView.setImageBitmap(bitmap); } } } public Bitmap getBitmapFromURL(String urlString){ Bitmap bitmap; InputStream is=null; try { URL url=new URL(urlString); HttpURLConnection connection=(HttpURLConnection) url.openConnection(); is=new BufferedInputStream(connection.getInputStream()); bitmap=BitmapFactory.decodeStream(is); connection.disconnect(); return bitmap; } catch (java.io.IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { is.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return null; } }
举报