這裏是我的代碼:的Android OOM造成繪製成位圖
int[] image = {R.drawable.image1, R.drawable.image2, R.drawable.image1, R.drawable.image2,
R.drawable.image1, R.drawable.image2, R.drawable.image1, R.drawable.image2,
R.drawable.image1, R.drawable.image2, R.drawable.image1, R.drawable.image2,
R.drawable.image1, R.drawable.image2, R.drawable.image1, R.drawable.image2,
R.drawable.image1, R.drawable.image2, R.drawable.image1, R.drawable.image2,
R.drawable.image1, R.drawable.image2, R.drawable.image1, R.drawable.image2};
而且getView:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView=LayoutInflater.from(getApplicationContext()).inflate(R.layout.item, null);
ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView);
imageView.setBackgroundResource(image[position]);
return convertView;
}
一切都很好。沒有OOM。
然後,我想先品嚐繪製:
public static Bitmap sampleImage(Context context, int resourceId, int sampleSize) {
Bitmap resizeBmp = null;
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = sampleSize;
resizeBmp = BitmapFactory.decodeResource(context.getResources(), resourceId, opts);
return resizeBmp;
}
而且新getView:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView=LayoutInflater.from(getApplicationContext()).inflate(R.layout.item, null);
ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView);
Bitmap item = BitmapUtil.sampleMaskInShelf(getApplicationContext(), image[position], 4);
imageView.setImageBitmap(item);
// imageView.setBackgroundResource(image[position]);
return convertView;
}
的事情發生了。起初,應用程序不會崩潰。但是使用的內存超過了50MB,在我上下滾動之後,會導致OOM。
的問題是: 1)爲什麼樣的圖像將增加所用的存儲器。 2)如何減少使用的內存,有沒有其他方法?
它是gridview。 – jjLin