Util:ImageLoad图片缩放处理
2015-08-21 22:01
281 查看
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;
}
}
相关文章推荐
- Slim Span
- 架构师不是演员,是管家
- 【LeetCode】(66)Plus One (Easy)
- iOS开发——网络篇——HTTP/NSURLConnection(请求、响应)、http响应状态码大全
- CodeForces 题目191C. Fools and Roads(Link Cut Tree,边权加求边权值)
- 【Android开发经验】超好用的json解析工具——Gson项目使用详解
- BZOJ 1096 [ZJOI2007]仓库建设 斜率优化dp
- bootstrap注意事项(八)一些辅助信息
- 数组和指针内存分配示例代码
- 经典任务调度算法的模拟程序
- 华为OJ(MP3光标移动)
- UVALIve 5987 素数
- 基于CANopen DSP402的运动控制笔记
- 奥斯特发现电生磁
- 网页源码保存为文件
- JAVA泛型
- 使用Reader模拟实现BufferedReader效果
- 最短路径之弗洛伊德算法(Floyd)
- 记使用Kali linux 2.0的一些坑
- android---粒子雨效果的实现