java.lang.IllegalArgumentException:无法绘制回收的位图-Android
我正在尝试实现Android wheel,一个我可以用来模拟吃角子老虎机的库 演示项目运行良好,它使用android自己的drawables=>android.R.drawable* 但是,当我将默认图像更改为所需图像时,会出现以下错误:java.lang.IllegalArgumentException:无法绘制回收的位图-Android,java,android,image,bitmap,recycle,Java,Android,Image,Bitmap,Recycle,我正在尝试实现Android wheel,一个我可以用来模拟吃角子老虎机的库 演示项目运行良好,它使用android自己的drawables=>android.R.drawable* 但是,当我将默认图像更改为所需图像时,会出现以下错误: java.lang.IllegalArgumentException: Cannot draw recycled bitmaps 我所更改的只是id的数组,其中包含对图像的引用 来自 private final int items[] = new int[
java.lang.IllegalArgumentException: Cannot draw recycled bitmaps
我所更改的只是id的数组,其中包含对图像的引用
来自
private final int items[] = new int[] {
android.R.drawable.star_big_on,
android.R.drawable.stat_sys_warning,
android.R.drawable.radiobutton_on_background,
android.R.drawable.ic_delete
};
至
private final int items[] = new int[] {
R.drawable.sunglasses, R.drawable.ball, R.drawable.box};
下面是代码的其余部分
private class SlotMachineAdapter extends AbstractWheelAdapter {
// Image size
final int IMAGE_WIDTH = 267;
final int IMAGE_HEIGHT = 200;
// Slot machine symbols
private final int items[] = new int[] {
R.drawable.sunglasses, R.drawable.ball, R.drawable.box};
// Cached images
private List<SoftReference<Bitmap>> images;
// Layout inflater
private Context context;
/**
* Constructor
*/
public SlotMachineAdapter(Context context) {
this.context = context;
images = new ArrayList<SoftReference<Bitmap>>(items.length);
for (int id : items) {
images.add(new SoftReference<Bitmap>(loadImage(id)));
}
}
/**
* Loads image from resources
*/
private Bitmap loadImage(int id) {
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), id);
Bitmap scaled = Bitmap.createScaledBitmap(bitmap, IMAGE_WIDTH, IMAGE_HEIGHT, true);
bitmap.recycle();
return scaled;
}
@Override
public int getItemsCount() {
return items.length;
}
// Layout params for image view
final LayoutParams params = new LayoutParams(IMAGE_WIDTH, IMAGE_HEIGHT);
@Override
public View getItem(int index, View cachedView, ViewGroup parent) {
ImageView img;
if (cachedView != null) {
img = (ImageView) cachedView;
} else {
img = new ImageView(context);
}
img.setLayoutParams(params);
SoftReference<Bitmap> bitmapRef = images.get(index);
Bitmap bitmap = bitmapRef.get();
if (bitmap == null) {
bitmap = loadImage(items[index]);
images.set(index, new SoftReference<Bitmap>(bitmap));
}
img.setImageBitmap(bitmap);
return img;
}
}
试试这个-
if(bitmap!=null)
{
bitmap.recycle();
bitmap=null;
}
我认为这是因为您太频繁地执行bitmap.recycle(),而system.gc()没有及时调用,所以变量bitmap是recycle(),但系统没有真正清除,我们无法使用它加载新图像。因此,只需注释bitmap.recycle(),让系统自行回收石笼。在哪一行显示异常?如果注释bitmap.recycle(),我已经添加了堆栈跟踪;在loadImage方法中,它可以工作,但是当轮子旋转时它会滞后,而在android drawables中,它是平滑的。我用这个替换了loadImage方法中的bitmap.recycle()行,但是我仍然得到了错误
if(bitmap!=null)
{
bitmap.recycle();
bitmap=null;
}