Android 异步加载图片(扩展后)
2013-07-12 09:37
246 查看
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() { @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub if (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() { @Override public void run() { // TODO Auto-generated method stub Bitmap 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() { @Override public void run() { // TODO Auto-generated method stub Bitmap bitmap = null; int i = 0; boolean downOk = false; handler.post(new Runnable() { @Override public 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() { @Override public void messageControl(String str) { } @Override public void stepControl(final double j) { // TODO Auto-generated method stub Message 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; } }
相关文章推荐
- Android:ViewPager扩展的具体解释——导航ViewPagerIndicator(有图片缓存,异步加载图片)
- Android 异步加载图片,使用LruCache和SD卡或手机缓存,效果非常的流畅
- 又优化了一下 Android ListView 异步加载图片
- Android实现异步从网络加载图片列表
- android异步加载图片并缓存到本地实现方法
- Android实现异步加载图片
- volley学习笔记(四):Android利用Volley异步加载数据(JSON和图片)完整示例
- Android listview异步加载图片
- Android异步加载网络图片
- Android-自定义TextView和异步加载图片的实现
- Android-Universal-Image-Loader 图片异步加载类库的使用
- android listview异步加载图片
- android 仿微信选择相册中的图片跟图片异步加载
- Android实现ListView异步加载图片
- Android-Universal-Image-Loader 图片异步加载类库的使用(超详细配置)
- 使用Android-Universal-Image-Loader异步加载图片遇到的问题
- Android Handler 异步消息处理机制的妙用 创建强大的图片加载类
- 【Android】异步加载图片-------不错的思路
- Android ListView异步加载网络图片