您的位置:首页 > 移动开发 > Android开发

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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: