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

对位图进行解码显示:

 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. 与你相遇好幸运,async解决循环回调问题

    由于使用的sailsjs框架,用的是sailsjs自身带的ORm(就是waterline),ORM的默认数据库的返回值在回调里面. > arg是一个数组 count用来计数用 tmpArr临时存 ...

  2. wifi基础知识整理

    转自 :http://blog.chinaunix.net/uid-9525959-id-3326047.html WIFI基本知识整理 这里对wifi的802.11协议中比较常见的知识做一个基本的总 ...

  3. .net学习之进程外Session的配置

    转载地址:http://www.cnblogs.com/rohelm/archive/2012/05/13/2498465.html 人人都知道怎么去使用session,但是初学者,尤其是自学的学生可 ...

  4. 【PHP Cookie&&Session】

    大部分的人都知道Cookie,但是可能不了解Session,现在对这两者进行解释. 问题的提出: 有些网站会提示用户在一定的时间之内免登陆,这是用的什么技术?答案是Cookie技术. 有些购物网站会提 ...

  5. 【openGL】画五角星

    #include "stdafx.h" #include <GL/glut.h> #include <stdlib.h> #include <math ...

  6. Swing布局基础

    虽然很简单,但还是记录一下,以备复查. 1.BorderLayout ,这是JFrame的默认布局方式,基于此的新组件,例如BUTTON,可以放在东西南北中的某一个位置,如果不指定,则默认是中央.中央 ...

  7. C# 文件读取方法,自己写的例子,保存一下,备用

    /// <summary> /// 将output.config内容传到app.config /// </summary> string ReadString; //两个地址 ...

  8. android 入门-android Studio 快捷输入

    1.输入 log的时候按一下Tab.就会打出 private static final String TAG="Settings"; 2. shift +alt+x 运行 shif ...

  9. [Linux][Hadoop] 运行WordCount例子

    紧接上篇,完成Hadoop的安装并跑起来之后,是该运行相关例子的时候了,而最简单最直接的例子就是HelloWorld式的WordCount例子.   参照博客进行运行:http://xiejiangl ...

  10. 从官方ROM中提取原生APK

    背景:由于自己手机总出现android.process.acore问题,最后发现是被自己精简掉了日历相关应用,故寻找提取原生apk. 注:解决方案主要是在机锋论坛上看到的. 环境要求:需要电脑安卓ja ...