您的位置:首页 > 其它

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;

    }

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