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

对位图进行解码显示:

 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. 有关Oracle数据库

    创建数据库(DCA):http://jingyan.baidu.com/article/cbcede07cf42ef02f40b4dc2.html 创建表(连接数据库,sql创建数据表):http:/ ...

  2. PHP魔术方法在框架中的应用

    class usermodel{ protected $email='user@163.com'; protected $data=array(); public function __set($k, ...

  3. Android Programming: Pushing the Limits -- Chapter 5: Android User Interface Operations

    多屏幕 自定义View 多屏幕 @.Android 4.2 开始支持多屏幕. @.举例: public class SecondDisplayDemo extends Activity { priva ...

  4. AXURE在原型设计中的应用

    转: http://uedc.163.com/2248.html 前言 什么是原型呢? 产品原型简单的说就是产品设计成形之前的一个简单框架,对网站来讲,就是将页面模块.元素进行粗放式的排版和布局,深入 ...

  5. Extjs ComboBox 动态选中第一项

    有时候我们希望通过Store加载过来的数据,ComboBoxItem能够选择第一条数据作为默认数据,我们可以这么操作: var storeinfo = Ext.create('Ext.data.Sto ...

  6. 【JAVA 其它流对象】

    一.PrintStream类. 该流是字节流. public class PrintStream extends FilterOutputStream implements Appendable, C ...

  7. javascript实用技巧,js小知识

    一.js整数的操作 使用|0和~~可以将浮点转成整型且效率方面要比同类的parseInt,Math.round 要快,在处理像素及动画位移等效果的时候会很有用.性能比较见此. var foo = (1 ...

  8. sqlplus 中spool命令的简单用法

    spool基本格式: spool 路径+文件名 select col1||','||col2||','||col3||','||col4||'..' from tablename; spool off ...

  9. WPF QuickStart系列之样式和模板(Style and Template)

    在WPF桌面程序中,当我们想构建一个统一的UI表现时(在不同操作系统下,显示效果一致),此时我们就需要使用到WPF中的样式和模板技术.简单来说,如果我们需要简单的给一个Button设置宽,高,Marg ...

  10. Effective C++ 之 0 导读(Introduction)

    Effective C++ 导读 (Introduction) 术语(terminology) 声明式 (declaration) 是告诉编译器某个东西的名称和类型(type),但略去细节.以下都是声 ...