我有一个列表适配器
adapter = new SimpleCardStackAdapter(this);
// System.gc(); i should call this function here or just before set adapter calling
int i=0;
if(cardModels.size() > VISIBLE_NUMBER_OF_CARDS){
i = cardModels.size()-VISIBLE_NUMBER_OF_CARDS;
}
for ( ;i < cardModels.size(); i++) {
//if(i<VISIBLE_NUMBER_OF_CARDS){
adapter.add(cardModels.get(i));
//}else{
// break;
//}
}
// System.gc(); i should call this function or here
mCardContainer.setAdapter(adapter);
我的问题是适配器消耗内存 在setadapter期间或创建适配器期间
我面临内存不足错误...适配器项目有一些大的位图 我必须在内存消耗操作发生之前手动调用垃圾收集器谢谢。
答案 0 :(得分:0)
您可以使用以下函数来获取与您想要加载它们的imageview大小相对应的位图...这有助于防止为较小的图像视图创建大位图甚至..
public static Bitmap decodeSampledBitmapFromResource(String filepath,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filepath, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(filepath, 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;
}