为什么我的药滑动之后才加载图片啊
就是点开程序他不加载图片,显示的是系统图片,要往下滑才能加载,应该不是网络原因,因为没加缓存之前好像没这个问题、
就是点开程序他不加载图片,显示的是系统图片,要往下滑才能加载,应该不是网络原因,因为没加缓存之前好像没这个问题、
2016-09-23
解决办法:将mImageView和mUrl声明在AsyncTask中,删除在ImageLorder中的声明。老师在ImageLorder中声明是为了Handler使用,我们这里将Handler的内容全部删除其实是没有影响的。
原理:如果我们在adapter构造方法中初始化ImageView,全局只有一个ImageLorder,而在ImageLoder中声明mImageView和mUrl,那么adapter没调用一次ShowImageByAsynctask,mImageViedw和mUrl就会被初始化一次,导致上一个bitmap形成后需要赋值的ImageVIew却发生了改变,无法加载成功。
解决办法:
1:getview()方法每次都new ImageLorder 不推荐
2:adapter构造方法中初始化ImageLorder,但是mImageView和mUrl在Asynctask中声明,避免被覆盖
但是我的监听器里面还没写函数额 package com.example.newapplication; import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.os.Handler; import android.os.Message; import android.support.v4.util.LruCache; import android.widget.ImageView; public class ImageLoader { private ImageView mimageView; private String murl; private LruCache<String , Bitmap> mCache; public ImageLoader(){ int MaxMemery = (int) Runtime.getRuntime().maxMemory(); //先获得最大内存 int cacheSize = MaxMemery/4; mCache = new LruCache<String, Bitmap>(cacheSize){ protected int sizeOf(String key, Bitmap value) { return value.getByteCount(); //每次把图片加入缓存时会调用 用来返回每张图片的大小 } }; } public void AddBitmapToCache(String url,Bitmap bitmap){ if (mCache.get(url)==null) { mCache.put(url, bitmap); } } public Bitmap getBitmapFromCache(String url){ return mCache.get(url); } //通过handler更新ui private Handler handler = new Handler(){ public void handleMessage(android.os.Message msg) { super.handleMessage(msg); if (mimageView.getTag().equals(murl)) { //只有图片和url匹配时才设置图片 使他一一对应 mimageView.setImageBitmap((Bitmap) msg.obj); } }; }; public void showImageByAsyncTask(ImageView imageView, String url) { Bitmap bitmap = getBitmapFromCache(url); // 从缓存中取出对应的图片 if (bitmap == null) { // 如果缓存中没有,那么必须下载 // imageView.setImageResource(R.drawable.ic_launcher); // 为空的时候先设置为自带图标 new NewsAsyncTask(imageView,murl).execute(url); } else { imageView.setImageBitmap(bitmap); } // new NewsAsyncTask(imageView,murl).execute(url); } // public void ShowImageByThread(ImageView imageView,final String url){ // mimageView = imageView; // murl = url; // new Thread(){ // public void run() { // super.run(); // Bitmap bitmap = GetBitmapFromUrl(url); //通过下面的方法获取Bitmap对象 然后交给handler传给主线程更新UI // Message message = new Message().obtain();//获得massage对象 // message.obj = bitmap; // handler.sendMessage(message); // } // }.start(); // } public Bitmap GetBitmapFromUrl(String urlString){ InputStream is = null; try { URL url = new URL(urlString); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); //获得URL之后获得连接 is = new BufferedInputStream(httpURLConnection.getInputStream()); Bitmap bitmap = BitmapFactory.decodeStream(is); //将输入流转换成Bitmap对象 httpURLConnection.disconnect(); return bitmap; } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ try { is.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } private class NewsAsyncTask extends AsyncTask<String, Void, Bitmap> { public NewsAsyncTask(ImageView imageView,String url) { mimageView = imageView; murl = url; } @Override protected Bitmap doInBackground(String... params) { String url = params[0]; Bitmap bitmap = GetBitmapFromUrl(url);// 从网络获取图片 前面bitmap一直没内容 现在才从网上获取资源 if (bitmap != null) { AddBitmapToCache(url, bitmap); // 将图片加入缓存 } return bitmap; } @Override protected void onPostExecute(Bitmap bitmap) { super.onPostExecute(bitmap); // ImageView imageView = (ImageView) mListView.findViewWithTag(mUrl); // if (imageView != null && bitmap != null) { // imageView.setImageBitmap(bitmap); // } // mTask.remove(this); // if (mimageView.getTag().equals(murl)) { //前面在Myadapter里面已经将图片对应url,如果对应才设置图片 // mimageView.setImageBitmap(bitmap); // } if(mimageView.getTag()==null){//初始化时并未设置Tag mimageView.setTag(murl); }else if(mimageView.getTag().equals(murl)){//已经设置过Tag mimageView.setImageBitmap(bitmap); } } } } package com.example.newapplication; import java.util.List; import java.util.zip.Inflater; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.AbsListView.OnScrollListener; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; public class MyAdapter extends BaseAdapter{ private List<NewsBean> mList; private LayoutInflater inflater; private ImageLoader mimageLoader; private int mStart,mEnd; public static String[] URLS; public MyAdapter(List<NewsBean> data,Context context){ mList = data; inflater = LayoutInflater.from(context); mimageLoader = new ImageLoader(); //初始化ImageLoader,避免后面重复创建 URLS = new String[data.size()]; for (int i = 0; i < data.size(); i++) { URLS[i] = data.get(i).imageViewURL; } } public int getCount() { return mList.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return mList.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView==null) { //如果该view对象为空 则创建 否则 直接通过Gettag()方法获得 holder = new ViewHolder(); convertView = inflater.inflate(R.layout.item_layout, null); //将显示样式转换成View对象 holder.imageView = (ImageView) convertView.findViewById(R.id.imageView1); //初始化holder的组件 find样式中的id holder.Title = (TextView) convertView.findViewById(R.id.Item_tv1); holder.conten = (TextView) convertView.findViewById(R.id.Item_tv2); convertView.setTag(holder); }else { holder = (ViewHolder) convertView.getTag(); } //设置控件的资源 holder.imageView.setImageResource(R.drawable.ic_launcher); String url = mList.get(position).imageViewURL; holder.imageView.setTag(url); //将图片和url绑定 mimageLoader.showImageByAsyncTask(holder.imageView, url); //设置图片 holder.Title.setText(mList.get(position).Title); //设置标题 holder.conten.setText(mList.get(position).content); return convertView; } //通过该对象 避免重复创建View对象 class ViewHolder{ ImageView imageView; TextView Title,conten; } // public void onScrollStateChanged(AbsListView view, int scrollState) { // if (scrollState==SCROLL_STATE_IDLE) { //表示此时滑动在停止状态 // // } // } // @Override // public void onScroll(AbsListView view, int firstVisibleItem, // int visibleItemCount, int totalItemCount) { // mStart = firstVisibleItem; // mEnd = firstVisibleItem+visibleItemCount; // } }
举报