ViewPager内存不足+ImageView

3
我想制作一个图片轮播器(ViewPager),但是遇到了内存溢出的问题。
我读过一些资料,建议使用位图(Bitmaps)或者像“reorganize()”这样的方法来解决问题,但我并不理解。
以下是我的代码:
适配器:
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
}
}

活动

@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);
}

如何获得更好的性能并避免OutOfMemory错误
非常感谢你!

只有4张图片就出现内存不足的情况了? - Rob
@Rob 是的...它在第三张图片上崩溃了... - Flo
请查看:http://developer.android.com/training/displaying-bitmaps/index.html - Haresh Chhelana
@Rob...每个图像只有105kb... - Flo
@mmlooloo 640 x 930... - Flo
在你的清单文件中的<application>标签中使用android:largeHeap="true" - Piyush
1个回答

3

您必须阅读:

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

然后使用我下面的函数,我已经将示例更改为从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;
    }

好的,我已经这样做了...但是它没有改变任何东西...我滑到第三张图片时出现了“内存不足”的错误。 - Flo
我可以给您建议4个解决方案:1- 将int inSampleSize = 1;更改为2或4...2- 回收位图3- 在您的清单中使用android:largeHeap="true"4- 使用 FragmentStatePagerAdapter 并将您的内容放在片段中。 - mmlooloo
好的,FragmentStatePagerAdapter是同样的问题,我之前尝试过这个... 我不明白如何回收位图..?我为每张图片创建一个新的吗? - Flo
但是我使用了非常大的图像,例如3000*2500,并增加了样本大小,它运行得很好。 - mmlooloo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接