我已经实现了一个 ViewPager,它给了我一个 Out Of Memory 错误,我不知道为什么会这样。我有大约 11 张图片要展示,每张图片大约有 500 kb(磁盘大小),它们是 .jpg 格式。
这是代码:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bmw_gallery);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);
}
private class ImagePagerAdapter extends PagerAdapter {
private int[] mImages = new int[] {
R.drawable.bmw_1series_3door_wallpaper_01,
R.drawable.bmw_1series_3door_wallpaper_02,
R.drawable.bmw_1series_3door_wallpaper_03,
R.drawable.bmw_1series_3door_wallpaper_04,
R.drawable.bmw_1series_3door_wallpaper_05,
R.drawable.bmw_1series_3door_wallpaper_06,
R.drawable.bmw_1series_3door_wallpaper_07,
R.drawable.bmw_1series_3door_wallpaper_08,
R.drawable.bmw_1series_3door_wallpaper_09,
R.drawable.bmw_1series_3door_wallpaper_11,
R.drawable.bmw_1series_3door_wallpaper_12
};
@Override
public int getCount() {
return mImages.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Context context = MainActivity.this;
ImageView imageView = new ImageView(context);
imageView.setAdjustViewBounds(true);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageResource(mImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
@Override
public void destroyItem(View collection, int position, Object o) {
View view = (View)o;
((ViewPager) collection).removeView(view);
view = null;
}
}
}
我做错了什么或我的代码中缺少什么?