import java.io.File;import java.lang.ref.SoftReference;import java.util.HashMap;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Handler;import android.os.Message;import android.widget.ImageView;import android.widget.ProgressBar;public class AsyncBitmapLoader {/** * 内存图片软引用缓冲 */private HashMap<String, SoftReference<Bitmap>> imageCache = null;private String localImagePath = "";public AsyncBitmapLoader(String localImagePath) {imageCache = new HashMap<String, SoftReference<Bitmap>>();this.localImagePath = localImagePath;}/** * @param imageView * @param progressBar * @param imageURL * @param imageCallBack * @param position *            list中第几项 * @param size *            设定图片导出大小 * @param fromAct *            "main"来自主界面, "detail"来自详情 * @return */public Bitmap loadBitmap(final ImageView imageView, final ProgressBar progressBar, final String imageURL, final ImageCallBack imageCallBack,final int position, final int size, final String fromAct) {final String picName = imageURL.substring(imageURL.lastIndexOf("/") + 1);// 在内存缓存中,则返回Bitmap对象Bitmap bitmap = null;if (imageCache.containsKey(imageURL)) {SoftReference<Bitmap> reference = imageCache.get(imageURL);bitmap = reference.get();if (bitmap != null) {return bitmap;}}/** * 加上一个对本地缓存的查找 */String bitmapName = picName;File cacheDir = new File(localImagePath);File[] cacheFiles = cacheDir.listFiles();int i = 0;for (; i < cacheFiles.length; i++) {if (bitmapName.equals(cacheFiles[i].getName())) {break;}}final Handler handler = new Handler() {@Overridepublic void handleMessage(Message msg) {// TODO Auto-generated method stubif (msg.what == 0)imageCallBack.imageLoad(imageView, progressBar, (Bitmap) msg.obj, msg.arg1);else if (msg.what == 1) {imageCallBack.imageLoad(imageView, progressBar, msg.arg1, msg.arg2);// System.out.println(msg.arg1);}}};if (i < cacheFiles.length) {new Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubBitmap bitmap = null;BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;bitmap = BitmapFactory.decodeFile(localImagePath + picName, options); // 此时返回bm为空options.inJustDecodeBounds = false;int be = (int) (options.outHeight / (float) size);if (be <= 0)be = 1;if (fromAct.equals("detail")) {options.inSampleSize = be;} else {options.inSampleSize = be;options.inPurgeable = true;options.inInputShareable = true;options.inPreferredConfig = Bitmap.Config.ARGB_4444;}bitmap = BitmapFactory.decodeFile(localImagePath + picName, options);imageCache.put(imageURL, new SoftReference<Bitmap>(bitmap));imageCache.put(imageURL, new SoftReference<Bitmap>(bitmap));Message msg2 = handler.obtainMessage(0, position, position, bitmap);// 把自身位置传进去handler.sendMessage(msg2);}}).start();return null;}// 如果不在内存缓存中,也不在本地(被jvm回收掉),则开启线程下载图片new Thread() {@Overridepublic void run() {// TODO Auto-generated method stubBitmap bitmap = null;int i = 0;boolean downOk = false;handler.post(new Runnable() {@Overridepublic void run() {Message msg = handler.obtainMessage(1, 0, position);handler.sendMessage(msg);}});// 下载while (i < 3) {if (MyHttpURLConnection_Util.downloadFile(imageURL, localImagePath + picName, null, new ProgressControl() {@Overridepublic void messageControl(String str) {}@Overridepublic void stepControl(final double j) {// TODO Auto-generated method stubMessage msg = handler.obtainMessage(1, (int) j, position);handler.sendMessage(msg);}})) {downOk = true;break;}i++;}if (!downOk) {System.out.println("downFailed--" + imageURL);// Message msg1 = handler.obtainMessage(0, bitmap);// handler.sendMessage(msg1);return;}// 解码缩放BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;bitmap = BitmapFactory.decodeFile(localImagePath + picName, options); // 此时返回bm为空options.inJustDecodeBounds = false;int be = (int) (options.outHeight / (float) size);if (be <= 0)be = 1;if (fromAct.equals("detail")) {options.inSampleSize = be;} else {options.inSampleSize = be;options.inPurgeable = true;options.inInputShareable = true;options.inPreferredConfig = Bitmap.Config.ARGB_4444;}bitmap = BitmapFactory.decodeFile(localImagePath + picName, options);// 放入缓存imageCache.put(imageURL, new SoftReference<Bitmap>(bitmap));Message msg2 = handler.obtainMessage(0, position, position, bitmap);// 把自身位置传进去handler.sendMessage(msg2);}}.start();return null;}}

更多相关文章

  1. 缓存工具类封装
  2. Android内存缓存图片的标准方法
  3. Android 缓存的工具类
  4. Android中如何判断内存卡是否存在
  5. Android:管理应用内存
  6. Android在屏幕任意位置显示对话框
  7. Android临时数据缓存方法
  8. Android内存分析和调优(下)

随机推荐

  1. android判断当前手机是否是小米
  2. android使用opengl es2.0播放视频
  3. Android(安卓)左边滑动菜单栏
  4. Android(安卓)屏幕宽度、高度
  5. Android(安卓)版本以及设备比重
  6. org.gradle.api.tasks.compile.CompileOp
  7. TextView双击事件
  8. android 项目 gradle 命令
  9. Retrofit response 回调封装
  10. 安卓自定义简单loading