这是我的代码:
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};
Run Code Online (Sandbox Code Playgroud)
和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;
}
Run Code Online (Sandbox Code Playgroud)
一切都好.没有OOM.
然后,我想首先对drawable进行采样:
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;
}
Run Code Online (Sandbox Code Playgroud)
而新的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;
}
Run Code Online (Sandbox Code Playgroud)
事情发生了.首先,应用程序不会崩溃.但是我使用的内存超过50MB,在我向上和向下滚动后,它会导致OOM.
问题是:1)为什么采样图像会增加使用的内存.2)如何减少使用的内存,还有其他方法吗?
| 归档时间: |
|
| 查看次数: |
894 次 |
| 最近记录: |