博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android 大位图加载
阅读量:7121 次
发布时间:2019-06-28

本文共 5242 字,大约阅读时间需要 17 分钟。

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

对位图进行解码显示:

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
softReference; public AsyncDrawable (Resources resources, Bitmap bitmap, BitmapWorkerTask bitmapWorkerTask) { super(resources, bitmap); softReference = SoftReference
(bitmapWorkerTask); } public BitmapWorkerTask getBitmapWorkerTask() { return softReference.get(); }}/**异步任务**/class BitmapWorkerTask extends AsyncTask
{ private SoftReference
imageSoftReference; private String data = ""; public BitmapWorkerTask (ImageView imageView) { imageSoftReference = new SoftReference
(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); }}

 

转载地址:http://slxel.baihongyu.com/

你可能感兴趣的文章
我的友情链接
查看>>
用python实现选择截图区域
查看>>
我的友情链接
查看>>
上班族之初体验+flexigrid关于起始加载页的笔记
查看>>
MAC下rabbitmq搭建
查看>>
ubuntu下安装MyEclipse10和MySql,为MyEclipse配置android ADT(全程截图)
查看>>
我的友情链接
查看>>
设置接口跨域调用方法
查看>>
python selenium系列(八)元素定位进阶之分层定位
查看>>
MySQL多表连接优化一例
查看>>
PHP动态扩展模块安装
查看>>
AgileEAS.NET平台开发实例-药店系统-UI层重构技巧及其他
查看>>
我的友情链接
查看>>
Shell开发的一些技巧和经验
查看>>
5-2 array 数组的赋值及遍历
查看>>
Go编程基础 - 类型与变量
查看>>
外链优化的发展
查看>>
集合类操作优化经验总结(三)
查看>>
内存数据库 eXtreme db 插入测试
查看>>
我的友情链接
查看>>