Back to project page SwitchTabs.
The source code is released under:
Apache License
If you think the Android project SwitchTabs listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.normal.testdemo.utils; //from ww w . j a v a 2 s. c o m import android.graphics.Bitmap; import android.os.Handler; import android.util.Log; import android.widget.ImageView; import java.lang.ref.SoftReference; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; /** * Created by ex_chenjinghao on 2014/4/8. * ???????????????? */ public class AsynImageLoader { private LoaderThread thread;// ??????????????????????? private HashMap<String, SoftReference<Bitmap>> imageCache;// ???????key:???url private Handler handler;// ????Activity?Handler?? private Bitmap defaultBitmap, errorBitmap; // ????????? public AsynImageLoader(Handler handler, Bitmap defaultBitmap, Bitmap errorBitmap) { imageCache = new HashMap<String, SoftReference<Bitmap>>(); this.handler = handler; this.defaultBitmap = defaultBitmap; this.errorBitmap = errorBitmap; } /** * ????????????ImageView?????url?????????Tag? * * @param imageView * ????????? * @param defaultBitmap * ??????????????????????? */ public void loadBitmap(ImageView imageView) { // ??????url,?????????????????????? String url = (String) imageView.getTag(); if (imageCache.containsKey(url)) {// ?????????? SoftReference<Bitmap> softReference = imageCache.get(url); Bitmap bitmap = softReference.get(); if (bitmap != null) {// ????????????????????????? imageView.setImageBitmap(bitmap); return; } else {// ??????????????????bitmap?????????????????? Log.e("TAG", "cache bitmap is null"); imageCache.remove(url); } } imageView.setImageBitmap(defaultBitmap);// ???????????????? if (thread == null && url != null) {// ?????????????????????????????? thread = new LoaderThread(imageView, url); thread.start(); } else if (url != null) {// ??????????????? thread.load(imageView, url); } else {// ?????????url??????????? imageView.setImageBitmap(errorBitmap); } } /** * ????????Bitmap????????? */ public void releaseBitmapCache() { if (imageCache != null) { for (Map.Entry<String, SoftReference<Bitmap>> entry : imageCache.entrySet()) { Bitmap bitmap = entry.getValue().get(); if (bitmap != null) { bitmap.recycle();// ??bitmap?? } } imageCache.clear(); } } /** * ?????????? */ private class LoaderThread extends Thread { LinkedHashMap<String, ImageView> mTaskMap;// ???????????????????? private boolean mIsWait;// ???????????????? public LoaderThread(ImageView imageView, String url) { mTaskMap = new LinkedHashMap<String, ImageView>(); mTaskMap.put(url, imageView); } /** * ?????????????? * @param imageView */ public void load(ImageView imageView, String url) { mTaskMap.remove(imageView);// ?????????????? mTaskMap.put(url, imageView);// ???????? if (mIsWait) {// ????????????????????????????????? synchronized (this) {// ?????notify()??????? this.notify(); } } } @Override public void run() { while (mTaskMap.size() > 0) {// ????????????????????,??????????????????? mIsWait = false; final String url = mTaskMap.keySet().iterator().next(); final ImageView imageView = mTaskMap.remove(url); if (url != null && imageView.getTag() == url) {// ?????????????ImageView??????tag????????? final Bitmap bitmap = Utils.getBitmapByUrl(url);// ??????????sd?????? // ??????????map? imageCache.put(url, new SoftReference<Bitmap>(bitmap)); if (url.equals(imageView.getTag())) {// ????????????? handler.post(new Runnable() {// ???????????????UI @Override public void run() { if (bitmap != null) { imageView.setImageBitmap(bitmap); } else { imageView.setImageBitmap(errorBitmap); } } }); } } if (mTaskMap.isEmpty()) {// ??????????????????????????? try { mIsWait = true;// ????????????wait()????? synchronized (this) { this.wait();// ???????????????????????????? } } catch (InterruptedException e) { e.printStackTrace(); } } } } } }