使用方式
ImageLoader imageLoader = null;
imageLoader = new ImageLoader(this);
imageLoader.DisplayImage(this, cChoice, cButton, "RadioButton");
注意
- ImageView和Radiobutton控件使用ImageLoader时需要创建独立的
ImageLoader
对象
ImageLoader.java
public class ImageLoader {
MemoryCache memoryCache = new MemoryCache();
FileCache fileCache;
private Map<View, String> views = Collections.synchronizedMap(new WeakHashMap<View, String>());
ExecutorService executorService;
String type = null;
Context context = null;
public ImageLoader(Context context) {
fileCache = new FileCache(context);
executorService = Executors.newFixedThreadPool(5);
}
final int stub_id = R.drawable.getimagefailed;
public void DisplayImage(Context context, String url, View view, String type) {
this.type = type;
this.context = context;
views.put(view, url);
Bitmap bitmap = memoryCache.get(url);
if (bitmap != null) {
if (type.equals("ImageView")) {
((ImageView) view).setImageBitmap(bitmap);
} else if (type.equals("RadioButton")) {
BitmapDrawable drawable = new BitmapDrawable(context.getResources(), bitmap);
drawable.setBounds(0, 0, 260, 260);
((RadioButton) view).setCompoundDrawables(null, null, drawable, null);
} else if (type.equals("messageView")) {
ImageView mesView = (ImageView) view;
bitmap = BubbleImageHelper.getInstance(context)
.getBubbleImageBitmap(bitmap,
R.drawable.zf_other_image_default_bk);
mesView.setImageBitmap(bitmap);
}
} else {
queuePhoto(url, view);
if (type.equals("ImageView")) {
((ImageView) view).setImageResource(stub_id);
} else if (type.equals("RadioButton")) {
bitmap = BitmapFactory.decodeResource(context.getResources(), stub_id);
BitmapDrawable drawable = new BitmapDrawable(context.getResources(), bitmap);
drawable.setBounds(0, 0, 260, 260);
((RadioButton) view).setCompoundDrawables(null, null, drawable, null);
} else if (type.equals("messageView")) {
((ImageView) view).setImageBitmap(bitmap);
}
}
}
private void queuePhoto(String url, View view) {
PhotoToLoad p = new PhotoToLoad(url, view);
executorService.submit(new PhotosLoader(p));
}
private Bitmap getBitmap(String url) {
File f = fileCache.getFile(url);
Bitmap b = decodeFile(f);
if (b != null)
return b;
try {
Bitmap bitmap = null;
URL imageUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setInstanceFollowRedirects(true);
InputStream is = conn.getInputStream();
OutputStream os = new FileOutputStream(f);
Utils.CopyStream(is, os);
os.close();
bitmap = decodeFile(f);
return bitmap;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
private Bitmap decodeFile(File f) {
try {
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
final int REQUIRED_SIZE = 70;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {
}
return null;
}
private class PhotoToLoad {
public String url;
public View view;
public PhotoToLoad(String u, View i) {
url = u;
view = i;
}
}
class PhotosLoader implements Runnable {
PhotoToLoad photoToLoad;
PhotosLoader(PhotoToLoad photoToLoad) {
this.photoToLoad = photoToLoad;
}
@Override
public void run() {
if (imageViewReused(photoToLoad))
return;
Bitmap bmp = getBitmap(photoToLoad.url);
memoryCache.put(photoToLoad.url, bmp);
if (imageViewReused(photoToLoad))
return;
BitmapDisplayer bd = new BitmapDisplayer(bmp, photoToLoad);
Activity a = (Activity) photoToLoad.view.getContext();
a.runOnUiThread(bd);
}
}
boolean imageViewReused(PhotoToLoad photoToLoad) {
String tag = views.get(photoToLoad.view);
return tag == null || !tag.equals(photoToLoad.url);
}
class BitmapDisplayer implements Runnable {
Bitmap bitmap;
PhotoToLoad photoToLoad;
public BitmapDisplayer(Bitmap b, PhotoToLoad p) {
bitmap = b;
photoToLoad = p;
}
public void run() {
if (imageViewReused(photoToLoad))
return;
if (bitmap != null) {
if (type.equals("ImageView")) {
((ImageView) photoToLoad.view).setImageBitmap(bitmap);
} else if (type.equals("RadioButton")) {
BitmapDrawable drawable = new BitmapDrawable(context.getResources(), bitmap);
drawable.setBounds(0, 0, 260, 260);
((RadioButton) photoToLoad.view).setCompoundDrawables(null, null, drawable, null);
} else if (type.equals("messageView")) {
ImageView mesView = ((ImageView) photoToLoad.view);
bitmap = BubbleImageHelper.getInstance(context)
.getBubbleImageBitmap(bitmap,
R.drawable.zf_other_image_default_bk);
mesView.setImageBitmap(bitmap);
}
} else {
if (type.equals("ImageView")) {
((ImageView) photoToLoad.view).setImageResource(stub_id);
} else if (type.equals("RadioButton")) {
bitmap = BitmapFactory.decodeResource(context.getResources(), stub_id);
BitmapDrawable drawable = new BitmapDrawable(context.getResources(), bitmap);
drawable.setBounds(0, 0, 260, 260);
((RadioButton) photoToLoad.view).setCompoundDrawables(null, null, drawable, null);
}
}
}
}
public void clearCache() {
memoryCache.clear();
fileCache.clear();
}
}
MemoryCache.java
public class MemoryCache {
private Map<String, SoftReference<Bitmap>> cache = Collections.synchronizedMap(new HashMap<String, SoftReference<Bitmap>>());
public Bitmap get(String id) {
if (!cache.containsKey(id))
return null;
SoftReference<Bitmap> ref = cache.get(id);
return ref.get();
}
public void put(String id, Bitmap bitmap) {
cache.put(id, new SoftReference<Bitmap>(bitmap));
}
public void clear() {
cache.clear();
}
}
FileCache.java
public class FileCache {
private File cacheDir;
private String TAG = FileCache.class.getSimpleName();
public FileCache(Context context) {
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
cacheDir = new File(android.os.Environment.getExternalStorageDirectory() + "/weike", "LazyList");
} else
cacheDir = context.getCacheDir();
if (!cacheDir.exists())
cacheDir.mkdirs();
}
public File getFile(String url) {
String filename = String.valueOf(url.hashCode()+".jpg");
File f = new File(cacheDir, filename);
return f;
}
public void clear() {
File[] files = cacheDir.listFiles();
if (files == null)
return;
for (File f : files)
f.delete();
}
}