Hi,
I'm facing a serious Memory Leaking problem. The code below is a ImageAdapter 
for android.widget.Gallery.
After I rotate the screen for several times, the Application crashes and report 
a "out of memory error: bitmap size exceeds VM budget."
I wrote a finish() but it doesn't work either.
Really grateful to any suggestions.

Code:

public class ImageAdapter extends BaseAdapter {
 
 private Context mContext;
 private ImageView[] mImages;

 public ImageAdapter(Context context, Bitmap[] bitmaps) {
  if (mContext == null) {
   mContext = context;
  }
  if (mImages == null) {
   mImages = new ImageView[bitmaps.length];
  }
  createReflectedImages(bitmaps);
 }

 public ImageAdapter(Context context, int[] imageIds) {
  if (mContext == null) {
   mContext = context;
  }
  Bitmap[] bitmaps = new Bitmap[imageIds.length];
  if (mImages == null) {
   mImages = new ImageView[imageIds.length];
  }
  int i = 0;
  for (int imageId : imageIds) {
   bitmaps[i++] = BitmapFactory.decodeResource(
     mContext.getResources(), imageId);
  }
  createReflectedImages(bitmaps);
 }


 private boolean createReflectedImages(Bitmap[] mBitmaps) {
  // The gap we want between the reflection and the original image
  final int reflectionGap = 0;
  int index = 0;
  Bitmap reflectionImage = null;
  Bitmap bitmapWithReflection = null;
  BitmapDrawable bd;
  Paint paint = new Paint();
  for (Bitmap originalImage : mBitmaps) {
       int width = originalImage.getWidth();
       int height = originalImage.getHeight();
       Matrix matrix = new Matrix();
       matrix.preScale(1, -1);
   
       reflectionImage = Bitmap.createBitmap(originalImage, 0, height / 
2,width, height / 2, matrix, false);
       bitmapWithReflection = Bitmap.createBitmap(width,(height + height / 2), 
Config.ARGB_8888);
      
       Canvas canvas = new Canvas(bitmapWithReflection);
       canvas.drawBitmap(originalImage, 0, 0, null);
       
       paint.reset();
       canvas.drawRect(0, height, width, height + reflectionGap, paint);
       canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
      
       final LinearGradient shader = new LinearGradient(0, 
originalImage.getHeight(), 0,bitmapWithReflection.getHeight()+ reflectionGap, 
0x70ffffff, 0x00ffffff, TileMode.CLAMP);
     
       paint.setShader(shader);
       paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
       canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + 
reflectionGap, paint);
  
       bd = new BitmapDrawable(bitmapWithReflection);
       bd.setAntiAlias(true);
       
       final ImageView imageView = new ImageView(mContext);
       imageView.setImageDrawable(bd);
       imageView.setLayoutParams(new GalleryFlow.LayoutParams(120, 120));
     
       mImages[index++] = imageView;
   
       originalImage.recycle();
  }
  
  reflectionImage.recycle();
 
  return true;
 }

 public int getCount() {
  return mImages.length;
 }
 public Object getItem(int position) {
  return position;
 }
 public long getItemId(int position) {
  return position;
 }

 public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
   convertView = mImages[position];
  }
  return convertView;
  }

 public void finish() {
 for (ImageView v : mImages) {
    v.setImageDrawable(null);
    v = null;
  }
  
 }
 }

2010-10-22



kearnel_android

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to