Android 大位图加载

时间:2022-06-02 17:24:10

说明:没对图片进行缓存处理,只是使用软引用进行位图资源的释放,从而避免内存泄漏。

对位图进行解码显示:

 1 public Bitmap decodeBitmap(Resources resources, int resId, int reqWith, reqHeight ) {
2 //对位图进行解码的参数设置
3 BitmapFactory.Options options = new BitmapFactory.Options();
4 //在对位图进行解码的过程中,避免申请内存空间
5 options.inJustDecodeBounds = true;
6 BimapFactory.decodeResource(resources, resId, options);
7 //对图片进行一定比例的压缩处理
8 options.inSampleSize = caculateInSimpleSize(options, reqWith, reqHeight);
9 //真正输出位图
10 options.inJustDecodeBounds = false;
11 return BimapFactory.decodeResource(resources, resId, options);
12 }

计算图片压缩比例:

public int caculateInSimpleSize(BitmapFactory.Options options, int reqWidth, int reqHeight){
//
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
int inSimpeSize = 1; // 压缩比例
if (imageHeight > reqHeight || imageWidth > reqWidth) {
final int heightRatio = Math.round((float) imageHeight / (float) reqHeight );
final int widthRatio = Math.round((float) imageWidth / (float) reqWidth);
inSimpleSize
= heightRatio < widthRatio ? heightRatio : widthRatio ;
}
return inSimpleSize;
}

网络图片请求:

 1 public static byte[] sendPost (String path){
2 HttpClient httpClient = new DefaultHttpClient();
3 HttpPost httpPost = new HttPost (path);
4 HttpResponse response = null;
5 try {
6 response = httpClient.execute(httpPost);
7 if (response.getStatusLine().getStatusCode() == 200) {
8 return EntityUtils.toByteArray(response.getEntity());
9 }
10 } catch (Exception e) {
11 e.printStackTrace();
12 } finally {
13 httpClient.getConnectionManager().shutdown();
14 }
15 return null;
16 }

批量加载大位图:

 1 //在adpter中的getView中处理显示
2 public View getView(int position, View converView, ViewGroup parent) {
3 View view = null;
4 if (converView == null ){
5 view = LayoutInflater.from(MainActivity.this,).inflate(R.layout.item, null);
6 } else {
7 view = converView;
8 }
9 ImageView imageView = (ImageView) view.findViewById(R.id.item_image);
10 //获取图片
11 loadBitmap(path[position], imageView);
12 return view;
13 }
//在滑动ListView时,会对旧的布局进行资源回收,如果ListView结合异步任务操作时,不能确保重用的布局被及时回收。
static class AsyncDrawable extends BitmapDrawable{
private final SoftReference<BitmapWorkerTask> softReference;
public AsyncDrawable (Resources resources, Bitmap bitmap, BitmapWorkerTask bitmapWorkerTask) {
super(resources, bitmap);
softReference
= SoftReference<MainActivity.BitmapWorkerTask>(bitmapWorkerTask);
}

public BitmapWorkerTask getBitmapWorkerTask() {
return softReference.get();
}
}

/**异步任务**/
class BitmapWorkerTask extends AsyncTask<String, void, Bitmap> {
private SoftReference<ImageView> imageSoftReference;
private String data = "";

public BitmapWorkerTask (ImageView imageView) {
imageSoftReference
= new SoftReference<ImageView>(imageView);
}

@Override
protected Bitmap doInBackground (String... params) {
data
= params[0];
byte[] result = sendPost(data);
// 对位图解码, 返回100*100
return decodeBitmap(result, 100, 100);
}

@Override
potected
void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
if (isCancelled()) {
bitmap
= null;
}
if (imageSoftReference != null && bitmap != null) {
final ImageView imageView = imageSoftReference.get();
final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
// 加载异步任务是独立的线程,保证引用imageView和异步任务有关联才可以。
if (this == bitmapWorkerTask && imageView != null) {
imageView.setImageBitmap(bitmap);
}
}
}
}


private static BitmapWorkerTask getBitmapWorkerTask (ImageView imageView) {

if (imageView != null) {
final Drawable drawable = imageView.getDrawable();
if (drawable instanceof AsyncDrawable) {
final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable;
return asyncDrawable.getBitmapWorkerTask();
}
}
return null;
}

// 检查是否有另外一个执行的异步任务和imageView来绑定,前一个异步任务进行取消操作
public static boolean cancelPotntialWork(String data, ImageView imageView) {
final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
if (bitmapWorkerTask != null) {
final String bitmapData = bitmapWorkerTask.data;
if (bitmapData != data) {
bitmapWorkerTask.cancel(
true);
}
else {
return false;
}
}
return true;
}

//加载图片
public void loadBitmap(String data, ImageView imageView) {
Bitmap placeBitmap
= BitmapFactory.decodeResource(getResources(), R.drawable.empty_photo);
if (cancelPotntialWork(data, imageView)) {
final BitmapWorkerTask task = new BitmapWorkerTask(imageView);
final AsyncDrawable asyncDrawable = new AsyncDrawable(getResources(), placeBitmap, task);
imageView.setImageDrawable(asyncDrawable);
task.execute(data);
}
}