001 |
package cn.jd3g.utils; |
003 |
import java.lang.ref.SoftReference; |
004 |
import java.util.HashMap; |
005 |
import java.util.LinkedHashMap; |
006 |
import java.util.Map.Entry; |
008 |
import android.graphics.Bitmap; |
009 |
import android.os.Handler; |
010 |
import android.util.Log; |
011 |
import android.widget.ImageView; |
019 |
public final class AsynImageLoader { |
021 |
private LoaderThread thread; |
022 |
private HashMap<String, SoftReference<Bitmap>> imageCache; |
023 |
private Handler handler; |
025 |
public AsynImageLoader(Handler handler) { |
026 |
imageCache = new HashMap<String, SoftReference<Bitmap>>(); |
027 |
this .handler = handler; |
031 |
* 加载图片前显示到指定的ImageView中,图片的url保存在视图对象的Tag中 |
035 |
* @param defaultBitmap |
036 |
* 加载需要显示的提示正在加载的默认图片对象 |
038 |
public void loadBitmap(ImageView imageView, Bitmap defaultBitmap) { |
040 |
String url = (String) imageView.getTag(); |
041 |
if (imageCache.containsKey(url)) { |
042 |
SoftReference<Bitmap> softReference = imageCache.get(url); |
043 |
Bitmap bitmap = softReference.get(); |
044 |
if (bitmap != null ) { |
045 |
imageView.setImageBitmap(bitmap); |
048 |
Log.e( "TAG" , "cache bitmap is null" ); |
049 |
imageCache.remove(url); |
052 |
imageView.setImageBitmap(defaultBitmap); |
053 |
if (thread == null ) { |
054 |
thread = new LoaderThread(imageView, url); |
057 |
thread.load(imageView, url); |
063 |
* 释放缓存中所有的Bitmap对象,并将缓存清空 |
065 |
public void releaseBitmapCache() { |
066 |
if (imageCache != null ) { |
067 |
for (Entry<String, SoftReference<Bitmap>> entry : imageCache.entrySet()) { |
068 |
Bitmap bitmap = entry.getValue().get(); |
069 |
if (bitmap != null ) { |
080 |
private class LoaderThread extends Thread { |
082 |
LinkedHashMap<String, ImageView> mTaskMap; |
083 |
private boolean mIsWait; |
085 |
public LoaderThread(ImageView imageView, String url) { |
086 |
mTaskMap = new LinkedHashMap<String, ImageView>(); |
087 |
mTaskMap.put(url, imageView); |
095 |
public void load(ImageView imageView, String url) { |
096 |
mTaskMap.remove(imageView); |
097 |
mTaskMap.put(url, imageView); |
099 |
synchronized ( this ) { |
107 |
while (mTaskMap.size() > 0 ) { |
109 |
final String url = mTaskMap.keySet().iterator().next(); |
110 |
final ImageView imageView = mTaskMap.remove(url); |
111 |
if (imageView.getTag() == url) { |
112 |
final Bitmap bitmap = MyConnection.getBitmapByUrl(url); |
115 |
} catch (InterruptedException e1) { |
116 |
e1.printStackTrace(); |
119 |
imageCache.put(url, new SoftReference<Bitmap>(bitmap)); |
120 |
if (url == imageView.getTag()) { |
121 |
handler.post( new Runnable() { |
124 |
imageView.setImageBitmap(bitmap); |
129 |
if (mTaskMap.isEmpty()) { |
132 |
synchronized ( this ) { |
135 |
} catch (InterruptedException e) { |
01 |
private class ProductListAdapter extends BaseAdapter { |
03 |
private AsynImageLoader mImageAsynLoader; |
05 |
public ProductListAdapter() { |
06 |
mImageAsynLoader = new AsynImageLoader(mHandler); |
10 |
public int getCount() { |
11 |
int size = Math.min(mLastItemViewIndex + 1 , mDataList.size()); |
12 |
mLastItemViewIndex = size - 1 ; |
17 |
public Object getItem( int position) { |
18 |
return mDataList.get(position); |
22 |
public long getItemId( int position) { |
27 |
public View getView( int position, View convertView, ViewGroup parent) { |
28 |
if (convertView == null ) { |
29 |
convertView = getLayoutInflater().inflate(R.layout.product_list_item, |
32 |
ImageView imageView = (ImageView) convertView |
33 |
.findViewById(R.id.iv_item_product_image); |
34 |
Map<String, String> map = mDataList.get(position); |
36 |
imageView.setTag(map.get( "product_pic_address" )); |
37 |
mImageAsynLoader.loadBitmap(imageView, mDefautBitmap); |
|