Chinaunix首页 | 论坛 | 博客
  • 博客访问: 375685
  • 博文数量: 214
  • 博客积分: 770
  • 博客等级: 军士长
  • 技术积分: 1969
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-08 01:22
文章分类

全部博文(214)

文章存档

2013年(110)

2012年(104)

我的朋友

分类: Android平台

2013-07-30 11:39:54

这里,我使用Gallery来举例,在模拟器中,不会出现OOM错误,但是,一旦把程序运行到真机里,图片文件一多,必然会出现OOM,我们通过做一些额外的处理来避免。

1.创建一个图片缓存对象HashMap dataCache,integer对应Adapter中的位置position,我们只用缓存处在显示中的图片,对于之外的位置,如果dataCache中有对应的图片,我们需要进行回收内存。在这个例子中,Adapter对象的getView方法首先判断该位置是否有缓存的bitmap,如果没有,则解码图片(bitmapDecoder.getPhotoItem,BitmapDecoder类见后面)并返回bitmap对象,设置dataCache在该位置上的bitmap缓存以便之后使用;若是该位置存在缓存,则直接取出来使用,避免了再一次调用底层的解码图像需要的内存开销。有时为了提高Gallery的更新速度,我们还可以预存储一些位置上的bitmap,比如存储显示区域位置外向上3个向下3个位置的bitmap,这样上或下滚动Gallery时可以加快getView的获取。

点击(此处)折叠或打开

  1. public View getView(int position, View convertView, ViewGroup parent) {
  2.       
  3.     if(convertView==null){
  4.         LayoutInflater inflater = LayoutInflater.from(context);
  5.         convertView = inflater.inflate(R.layout.photo_item, null);
  6.   
  7.            holder = new ViewHolder();
  8.            holder.photo = (ImageView) convertView.findViewById(R.id.photo_item_image);
  9.            holder.photoTitle = (TextView) convertView.findViewById(R.id.photo_item_title);
  10.            holder.photoDate = (TextView) convertView.findViewById(R.id.photo_item_date);
  11.            convertView.setTag(holder);
  12.     }else {
  13.        holder = (ViewHolder) convertView.getTag();
  14.     }
  15.     cursor.moveToPosition(position);
  16.       
  17.     Bitmap current = dateCache.get(position);
  18.     if(current != null){//如果缓存中已解码该图片,则直接返回缓存中的图片
  19.         holder.photo.setImageBitmap(current);
  20.     }else {
  21.         current = bitmapDecoder.getPhotoItem(cursor.getString(1), 2) ;
  22.         holder.photo.setImageBitmap(current);
  23.         dateCache.put(position, current);
  24.     }
  25.     holder.photoTitle.setText(cursor.getString(2));
  26.     holder.photoDate.setText(cursor.getString(4));
  27.     return convertView;
  28. }

点击(此处)折叠或打开

  1. BitmapDecoder.class

  2. package com.wuyi.bestjoy;
  3.   
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6.   
  7. import Android.content.Context;
  8. import android.graphics.Bitmap;
  9. import android.graphics.BitmapFactory;
  10. import android.graphics.Matrix;
  11.   
  12. public class BitmapDecoder {
  13.     private static final String TAG = "BitmapDecoder";
  14.     private Context context;
  15.     public BitmapDecoder(Context context) {
  16.         this.context = context;
  17.     }
  18.       
  19.     public Bitmap getPhotoItem(String filepath,int size) {
  20.           BitmapFactory.Options options = new BitmapFactory.Options();
  21.           options.inSampleSize=size;
  22.           Bitmap bitmap = BitmapFactory.decodeFile(filepath,options);
  23.           bitmap=Bitmap.createScaledBitmap(bitmap, 180, 251, true);//预先缩放,避免实时缩放,可以提高更新率
  24.           return bitmap;
  25.           
  26.     }
  27. }
2.由于Gallery控件的特点,总有一个item处于当前选择状态,我们利用此时进行dataCache中额外不用的bitmap的清理,来释放内存。

点击(此处)折叠或打开

  1. @Override
  2.     public void onItemSelected(AdapterView<?> parent, View view, int position,long id) {
  3.           
  4.         releaseBitmap();
  5.         Log.v(TAG, "select id:"+ id);
  6.     }
  7.   
  8. private void releaseBitmap(){
  9.     //在这,我们分别预存储了第一个和最后一个可见位置之外的3个位置的bitmap
  10.     //即dataCache中始终只缓存了(M=6+Gallery当前可见view的个数)M个bitmap
  11.         int start = mGallery.getFirstVisiblePosition()-3;
  12.         int end = mGallery.getLastVisiblePosition()+3;
  13.         Log.v(TAG, "start:"+ start);
  14.         Log.v(TAG, "end:"+ end);
  15.         //释放position<start之外的bitmap资源
  16.         Bitmap delBitmap;
  17.         for(int del=0;del<start;del++){
  18.             delBitmap = dateCache.get(del);
  19.             if(delBitmap != null){
  20.                                 //如果非空则表示有缓存的bitmap,需要清理
  21.                 Log.v(TAG, "release position:"+ del);
  22.                 //从缓存中移除该del->bitmap的映射
  23.                                 dateCache.remove(del);
  24.                 delBitmap.recycle();
  25.             }
  26.         }
  27.   
  28.         freeBitmapFromIndex(end);
  29.           
  30.     }
  31.       
  32.     /**
  33.      * 从某一位置开始释放bitmap资源
  34.      * @param index
  35.      */
  36.     private void freeBitmapFromIndex(int end) {
  37.         //释放之外的bitmap资源
  38.         Bitmap delBitmap;
  39.         for(int del =end+1;del<dateCache.size();del++){
  40.             delBitmap = dateCache.get(del);
  41.             if(delBitmap != null){
  42.                 dateCache.remove(del);
  43.                 delBitmap.recycle();
  44.                 Log.v(TAG, "release position:"+ del);
  45.             }
  46.               
  47.         }
  48.     }
经过这些额外的操作,有效的避免了OOM的问题。

阅读(773) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~