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

对位图进行解码显示:

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

计算图片压缩比例:

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;
}

网络图片请求:

 public static byte[] sendPost (String path){
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttPost (path);
HttpResponse response = null;
try {
response = httpClient.execute(httpPost);
if (response.getStatusLine().getStatusCode() == 200) {
return EntityUtils.toByteArray(response.getEntity());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
httpClient.getConnectionManager().shutdown();
}
return null;
}

批量加载大位图:

 //在adpter中的getView中处理显示
public View getView(int position, View converView, ViewGroup parent) {
View view = null;
if (converView == null ){
view = LayoutInflater.from(MainActivity.this,).inflate(R.layout.item, null);
} else {
view = converView;
}
ImageView imageView = (ImageView) view.findViewById(R.id.item_image);
//获取图片
loadBitmap(path[position], imageView);
return view;
}
//在滑动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);
}
}

Android 大位图加载的更多相关文章

  1. Android照片墙-多图加载

    http://blog.csdn.net/guolin_blog/article/details/9526203 照片墙这种功能现在应该算是挺常见了,在很多应用中你都可以经常看到照片墙的身影.它的设计 ...

  2. 深入探索Glide图片加载框架:做了哪些优化?如何管理生命周期?怎么做大图加载?

    前言 Glide可以说是最常用的图片加载框架了,Glide链式调用使用方便,性能上也可以满足大多数场景的使用,Glide源码与原理也是面试中的常客. 但是Glide的源码内容比较多,想要学习它的源码往 ...

  3. Android高清巨图加载方案

    1.今天看了鸿洋的<Android高清巨图加载方案>一文,对加载高清巨图时的解决方案有了一定的认识. 思路为: 提供一个设置图片的入口. 重写onTouchEvent,在里面根据用户移动的 ...

  4. Android 高清加载巨图方案 拒绝压缩图片

    Android 高清加载巨图方案 拒绝压缩图片 转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/49300989: 本文出自:[张 ...

  5. Android 高清加载巨图方案, 拒绝压缩图片

    源地址:http://blog.csdn.net/lmj623565791/article/details/49300989 一.概述 距离上一篇博客有段时间没更新了,主要是最近有些私事导致的,那么就 ...

  6. Android 使用Glide加载网络图片等比例缩放

    在做android图片加载的时候,由于手机屏幕受限,很多大图加载过来的时候,我们要求等比例缩放,比如按照固定的宽度,等比例缩放高度,使得图片的尺寸比例得到相应的缩放,但图片没有变形.显然按照andro ...

  7. Android 使用Picasso加载网络图片等比例缩放

    在做android图片加载的时候,由于手机屏幕受限,很多大图加载过来的时候,我们要求等比例缩放,比如按照固定的宽度,等比例缩放高度,使得图片的尺寸比例得到相应的缩放,但图片没有变形.显然按照andro ...

  8. Android之批量加载图片OOM问题解决方案

    一.OOM问题出现的场景和原因 一个好的app总少不了精美的图片,所以Android开发中图片的加载总是避免不了的,而在加载图片过程中,如果处理不当则会出现OOM的问题.那么如何彻底解决这个问题呢?本 ...

  9. Android大神 博客

    https://github.com/yeungeek/awesome-android-person Android大神 受Trinea的开源项目的启发和参考,也准备列一列Android圈里的大神们. ...

随机推荐

  1. DC/DC与LDO的差别

    转自:http://bbs.eetop.cn/thread-459121-1-1.html 在平时的学习中,我们都有接触LDO和DC/DC这一类的电源产品,但作为学生的我们队这些东西可能了解不够深刻, ...

  2. JavaScript中判断对象类型方法大全1

    我们知道,JavaScript中检测对象类型的运算符有:typeof.instanceof,还有对象的constructor属性: 1) typeof 运算符 typeof 是一元运算符,返回结果是一 ...

  3. [LeetCode] Valid Parentheses

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  4. [LeetCode] Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  5. Multiple types were found that match the controller named 'Home'. (weird error)

    found the error, because I changed the namespace and assembly name, then on the bin folder the old d ...

  6. thinkphp 两表、三表联合查询

    //两表联合查询 $Model = M('T1');$Model->join('left join t2 on t1.cid = t2.id')->select();// $list = ...

  7. linux下搭建属于自己的博客(WordPress安装)

    转自:http://www.cnblogs.com/xiaofengkang/archive/2011/11/16/2251608.html WordPress简介 WordPress 是一种使用 P ...

  8. Shell拆分大文件

    需求:由于文件过大,不方便进行相关的操作,需要将其拆分成大小小于500000B,即488.28125k的文件.同时,为了保证文件的可读性,行内不可以分割,同时,由于内容是块状可读,按照日期进行分割的, ...

  9. Vue#Class 与 Style 绑定

    绑定HTMLCLASS 在我没看这之前,我觉得要写绑定class ,应该像绑定数据一样这么写 class ={{class-a}} 看官方教程时,不推荐这么写,推荐这样 v-bind:class=&q ...

  10. javase基础笔记3——this关键字和内存图

    什么是面向对象? 面向过程. 面向过程:解决一个问题的思路和方法以及步骤 面向对象:把一些具有相同特征的问题抽象成一个对象,用""""对象.方法()" ...