我已经实现了一个ViewPager,它将容纳5个BitMap。我的图像每张不超过50kb。初始化ViewPager时,会先实例化前2个图像,但它们消耗的内存为100 MB!在第二张图像后,我的应用程序消耗大约256MB的内存,比我正常的内存使用量多190MB。这仅在3个图像创建后发生。 (总计150KB)
我尝试解码BitMaps但没有用。我不知道为什么这么小的图像消耗了那么多的内存。图像幻灯片动画通常都是缓慢的。你知道我在哪里做错了吗?
日志
Initialize start
﹕ Position 0 is created
﹕ Position 1 is created
Initialize end
Swipe Next image (1)
﹕ Position 2 is created
Swipe Next image (2)
﹕ Position 0 is destroyed
﹕ Position 3 is created
Swipe Next image (3)
﹕ Position 1 is destroyed
﹕ Position 4 is created
Swipe Next image (4)(last image)
﹕ Position 2 is destroyed
Swipe Previous image (3)
﹕ Position 2 is created
Swipe Previous image (2)
CRASHED
这是我的适配器
class CustomPagerAdapter extends PagerAdapter {
Context mContext;
LayoutInflater mLayoutInflater;
public CustomPagerAdapter(Context context) {
mContext = context;
mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return NUM_PAGES;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((LinearLayout) object);
}
@Override
public Object instantiateItem(ViewGroup collection, int position) {
DebugLog.debug(TAG,"Position " + position + " is created");
View itemView = mLayoutInflater.inflate(R.layout.pager_item, collection, false);
ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), mResources[position]);
imageView.setImageBitmap(bitmap);
collection.addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
View view = (View)object;
ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
BitmapDrawable bmpDrawable = (BitmapDrawable) imageView.getDrawable();
if (bmpDrawable != null && bmpDrawable.getBitmap() != null) {
bmpDrawable.getBitmap().recycle();
DebugLog.debug(TAG, "Position " + position + " is destroyed");
}
container.removeView((LinearLayout) object);
view = null;
}
}
答案 0 :(得分:0)
添加到您的适配器 private HashMap usedImages;
...
public CustomPagerAdapter(Context context) {
...
usedImages=new HashMap<>();
}
@Override
public Object instantiateItem(ViewGroup collection, int position) {
DebugLog.debug(TAG,"Position " + position + " is created");
View itemView = mLayoutInflater.inflate(R.layout.pager_item, collection, false);
ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), mResources[position]);
imageView.setImageBitmap(bitmap);
usedImages.put(position, bitmap);
collection.addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
View view = (View)object;
ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
imageView.setImageBitmap(null);
Bitmap bitmap = usedImages.get(position);
if (bitmap != null) {
bitmap.recycle();
bitmap = null;
}
usedImages.remove(position);
container.removeView(view);
}
将方法 clear()添加到您的适配器,并在活动或片段的 onDestroy 中调用它:
public void clear(){
if(usedImages!=null) {
for (Map.Entry<Integer, Bitmap> item : usedImages.entrySet()) {
Bitmap bitmap = item.getValue();
if (bitmap != null) {
bitmap.recycle();
bitmap = null;
}
}
usedImages.clear();
usedImages = null;
}
mContext = null;
mLayoutInflater=null;
}