ViewPager Out of Memory + ImageView

I am trying to make a ViewPager for images. But I am getting OutOfMemory error.

I've read that I'm using bitmaps or doing something like "refactor ()" ... I don't understand it ...

My code:

Adapter:

public class app_info_Adapter extends PagerAdapter {
 private Activity act; 
 int[] pictures = { 
               R.drawable.1,
               R.drawable.2,
               R.drawable.3,
               R.drawable.4
             };

public app_info_Adapter(Activity act) {
    this.act = act;
}

@Override
public void destroyItem(View collection, int position, Object o) {
    View view = (View)o;
    ((ViewPager) collection).removeView(view);
    view = null;
}

@Override
public void finishUpdate(View arg0) {
    // TODO Auto-generated method stub

}
@Override
public int getCount() {
    return pictures.length;
}

@Override
public Object instantiateItem(View context, int position) {
    ImageView imageView = new ImageView(act);
    imageView.setImageResource(pictures[position]);
    LayoutParams imageParams = new LayoutParams();
    imageParams.height = LayoutParams.WRAP_CONTENT;
    imageParams.width = LayoutParams.WRAP_CONTENT;
    imageView.setLayoutParams(imageParams);

    ((ViewPager) context).addView(imageView);

    return imageView;
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return view==((ImageView)object);
}

@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
    // TODO Auto-generated method stub
}
@Override
public Parcelable saveState() {
    // TODO Auto-generated method stub
    return null;
}
@Override
public void startUpdate(View arg0) {
    // TODO Auto-generated method stub
}
}

      

Activities

@Override
protected void onStart() {
    super.onStart();
    setContentView(R.layout.app_info);


    ViewPager pager = (ViewPager)findViewById(R.id.viewpager);
    app_info_Adapter myPagerAdapter = new app_info_Adapter(this);
    pager.setAdapter(myPagerAdapter);
}

      

How to get better performance and no errors OurOfMemory-error

Thanks a lot!

+3


source to share


1 answer


you should read:

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html



then use my function below which I changed sample to read from drawable:

  public static Bitmap decodeSampledBitmapFromDrawable(Resources res,int resId,int reqWidth, int reqHeight) {

            // First decode with inJustDecodeBounds=true to check dimensions
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeResource(res, resId, options);

            // Calculate inSampleSize
            options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

            // Decode bitmap with inSampleSize set
            options.inJustDecodeBounds = false;
            return BitmapFactory.decodeResource(res, resId, options);
        }


public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) > reqHeight
                    && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }

        return inSampleSize;
    }

      

+3


source







All Articles