public class ImageLoadTask extends AsyncTask<Void, Void, Void> {
private MyListViewAdapter adapter;
private List<ArchRequest> archRequestList;
private LoadingDialog loadingDialog;
private Context context;
public ImageLoadTask(Context context,MyListViewAdapter adapter, List<ArchRequest> archRequestList,LoadingDialog loadingDialog) {
this.context = context;
this.adapter = adapter;
this.archRequestList = archRequestList;
this.loadingDialog = loadingDialog;
}
@Override
protected Void doInBackground(Void... params) {
for (int i = 0; i < adapter.getCount(); i++) {
ImageBean bean = (ImageBean) adapter.getItem(i);
if(archRequestList.get(i) != null){
ArchResponse archResponse= SyncApi.getArch(archRequestList.get(i));
if(archResponse != null){
byte[] b = archResponse.getArchData();
if (b!= null && b.length != 0) {
bean.setBitmap(createBitmap(b,80,80));
}
}
}
bean.setNeedBar(false);
publishProgress(); // 通知去更新UI
}
return null;
}
public void onProgressUpdate(Void... voids) {
if (isCancelled()) {
return;
}
// 更新UI
loadingDialog.dismiss();
adapter.notifyDataSetChanged();
}
/**
* @param data 数据或路径
* @param width 目标宽
* @param height 目标高
* @return 图片
*/
private Bitmap createBitmap(Object data, int width, int height) {
Options options = new Options();
int scale = 1;
if (width > 0 && height > 0) {//创建目标大小的图片
options.inJustDecodeBounds = true;
if (data instanceof String) {
BitmapFactory.decodeFile((String) data, options);
} else {
BitmapFactory.decodeByteArray((byte[]) data, 0, ((byte[]) data).length, options);
}
int dw = options.outWidth / width;
int dh = options.outHeight / height;
scale = Math.max(dw, dh);
options = new Options();
}
options.inDensity = DeviceInfo.getInstance().getDencity();
options.inScaled = true;
options.inPurgeable = true;
options.inSampleSize = scale;
Bitmap bitmap = null;
if (data instanceof String) {
bitmap = BitmapFactory.decodeFile((String) data, options);
} else {
bitmap = BitmapFactory.decodeByteArray((byte[]) data, 0, ((byte[]) data).length,
options);
}
return bitmap;
}
}
private MyListViewAdapter adapter;
private List<ArchRequest> archRequestList;
private LoadingDialog loadingDialog;
private Context context;
public ImageLoadTask(Context context,MyListViewAdapter adapter, List<ArchRequest> archRequestList,LoadingDialog loadingDialog) {
this.context = context;
this.adapter = adapter;
this.archRequestList = archRequestList;
this.loadingDialog = loadingDialog;
}
@Override
protected Void doInBackground(Void... params) {
for (int i = 0; i < adapter.getCount(); i++) {
ImageBean bean = (ImageBean) adapter.getItem(i);
if(archRequestList.get(i) != null){
ArchResponse archResponse= SyncApi.getArch(archRequestList.get(i));
if(archResponse != null){
byte[] b = archResponse.getArchData();
if (b!= null && b.length != 0) {
bean.setBitmap(createBitmap(b,80,80));
}
}
}
bean.setNeedBar(false);
publishProgress(); // 通知去更新UI
}
return null;
}
public void onProgressUpdate(Void... voids) {
if (isCancelled()) {
return;
}
// 更新UI
loadingDialog.dismiss();
adapter.notifyDataSetChanged();
}
/**
* @param data 数据或路径
* @param width 目标宽
* @param height 目标高
* @return 图片
*/
private Bitmap createBitmap(Object data, int width, int height) {
Options options = new Options();
int scale = 1;
if (width > 0 && height > 0) {//创建目标大小的图片
options.inJustDecodeBounds = true;
if (data instanceof String) {
BitmapFactory.decodeFile((String) data, options);
} else {
BitmapFactory.decodeByteArray((byte[]) data, 0, ((byte[]) data).length, options);
}
int dw = options.outWidth / width;
int dh = options.outHeight / height;
scale = Math.max(dw, dh);
options = new Options();
}
options.inDensity = DeviceInfo.getInstance().getDencity();
options.inScaled = true;
options.inPurgeable = true;
options.inSampleSize = scale;
Bitmap bitmap = null;
if (data instanceof String) {
bitmap = BitmapFactory.decodeFile((String) data, options);
} else {
bitmap = BitmapFactory.decodeByteArray((byte[]) data, 0, ((byte[]) data).length,
options);
}
return bitmap;
}
}