码迷,mamicode.com
首页 > 其他好文 > 详细

listview加载图片显示

时间:2015-12-21 23:44:00      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:

 

Adapter:   ----

//adapter的构造方法:   参数1 为url数组: 

 public static String[] mList;// 讲url保村在静态的String[] 中 在其他类可以直接调用

public MyListAdapter(String[] list, Context context, ListView listView) {
this.mList = list;
this.mContext = context;
bitmapUtils = new HttpUtils(context, listView);
listView.setOnScrollListener(this);
}

在getView()方法中为url设置tag

holder.mImageView.setTag(mList[position]);

bitmapUtils.display(item, mImageView);//加载图片  不过不加载网络图片 只加载内存和本地图片   没有不加载

//为Adapter添加 listView的滑动监听

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
// 加载可见项
bitmapUtils.loadImage(mStart, mEnd);
} else {
bitmapUtils.stopLoad();
}

}

private boolean isFrist = true;

@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
mStart = firstVisibleItem;
mEnd = firstVisibleItem + visibleItemCount;
if (isFrist && visibleItemCount > 0) {
bitmapUtils.loadImage(mStart, mEnd);
isFrist = false;
}

}

 

图片加载类 :主要用到LruCache 类和异步加载:  然后关于图片的错乱处理:(还可以加上本地缓存来进一步优化)

ImageView image =(ImageView)listiew.findViewWithTag(url); 来找到控件:

 

//-------------------------犹豫的分割线----------------======================

package com.example.listoptimization.activity;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashSet;
import java.util.Set;

import com.example.listoptimization.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.support.v4.util.LruCache;
import android.widget.ImageView;
import android.widget.ListView;

/**
* 还差本地缓存 实现了网络和内存加载
*
* @author chen
*
*/
public class HttpUtils {
private ImageView mImageView;
private String murl;

private Context mContext;
private LruCache<String, Bitmap> mCache;

public HttpUtils(Context context, ListView list) {
this.mContext = context;
this.mListView = list;
int maxSize = (int) Runtime.getRuntime().maxMemory();
int cacheSize = maxSize / 5;
mCache = new LruCache<String, Bitmap>(cacheSize) {
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getByteCount();
}
};
mSet = new HashSet<HttpUtils.MyAsyncTask>();
}

public void display(String url, ImageView image) {
this.mImageView = image;
this.murl = url;
Bitmap bitmapFromCache = getBitmapFromCache(url);
if (bitmapFromCache != null) {
System.out.println("内存中加载");
image.setImageBitmap(bitmapFromCache);

} else {
image.setImageResource(R.drawable.ic_launcher);

}

}

// 加载万络图片
public void loadImage(String url, ImageView imageView) {
Bitmap bitmapFromCache = getBitmapFromCache(url);
if (bitmapFromCache == null) {
new MyAsyncTask(url).execute(url);

} else {

imageView.setImageBitmap(bitmapFromCache);

}
}

// 从LruCachhe中获取
public Bitmap getBitmapFromCache(String path) {
if (path != null) {
Bitmap mBitmap = mCache.get(path);
return mBitmap;
}
return null;

}

// 保村图片到缓存中去
public void saveBitmapToCache(String path, Bitmap bitmap) {
if (path != null) {

if (getBitmapFromCache(path) == null) {
mCache.put(path, bitmap);

}
}

}

// 根据url获取Bitmap
public Bitmap getBitmapFromService(String url) {
Bitmap bitmap = null;
try {
URL murl = new URL(url);
HttpURLConnection mHttpConnection = (HttpURLConnection) murl.openConnection();
mHttpConnection.setRequestMethod("GET");
mHttpConnection.setConnectTimeout(3000);
if (mHttpConnection.getResponseCode() == 200) {
InputStream mInputStream = mHttpConnection.getInputStream();
bitmap = BitmapFactory.decodeStream(mInputStream);

}

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}

public class MyAsyncTask extends AsyncTask<String, Void, Bitmap> {
String url;

public MyAsyncTask(String url) {

this.url = url;
}

@Override
protected Bitmap doInBackground(String... params) {
Bitmap bitmapFromService = getBitmapFromService(params[0]);
saveBitmapToCache(url, bitmapFromService);
return bitmapFromService;
}

@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
ImageView mImageView = (ImageView) mListView.findViewWithTag(url);
if (mImageView != null && result != null) {
mImageView.setImageBitmap(result);
}

mSet.remove(url);

}

}

private Set<MyAsyncTask> mSet;// 用于管理task任务
private ListView mListView;

public void loadImage(int start, int end) {
for (int i = start; i < end; i++) {
String url = MyListAdapter.mList[i];
Bitmap bitmapFromCache = getBitmapFromCache(url);
if (bitmapFromCache != null) {
ImageView mImageView = (ImageView) mListView.findViewWithTag(url);
mImageView.setImageBitmap(bitmapFromCache);

} else {
MyAsyncTask task = new MyAsyncTask(url);
task.execute(url);
mSet.add(task);

}

}

}

public void stopLoad() {
if (mSet != null) {

for (MyAsyncTask myAsyncTask : mSet) {
myAsyncTask.cancel(false);
}

}
}
}

 

listview加载图片显示

标签:

原文地址:http://www.cnblogs.com/chenli123/p/5065160.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!