错误移除位图[Android版] [英] Error removing Bitmaps[Android]

查看:108
本文介绍了错误移除位图[Android版]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作在一个简单的应用,吸引了小位图到屏幕上,并有一个小麻烦清除屏幕。我的位图存储在一个名为_graphics一个ArrayList,我需要清除屏幕,所以我清楚我的ArrayList。这部作品在清除画面精美,但过了一段时间我的应用程序强制关闭。

I am working on a simple application that draws small bitmaps onto the screen, and have a little trouble clearing the screen. My bitmaps are stored in an ArrayList called _graphics, and I need to clear the screen, so I clear my ArrayList. This works fine in clearing the screen, however after a while my app force closes.

如果我在屏幕上绘制约50位图,它会强制关闭我第一次清除它,但如果我只画5,我可以得到约10清除它崩溃了。我认为这是值得做的GC没有正确清除位图。有谁对这个问题有一个想法?

If I draw around 50 bitmaps on the screen, it will force close the first time I clear it, however if I only draw 5, I can get about 10 Clears before it crashes. I assume this is something to do with the GC not clearing the bitmaps correctly. Does anyone have an ideas on the subject?

推荐答案

确定 - 现在的堆栈跟踪附:ConcurrentModificationException的
修改来自不同线程的ArrayList中或遍历它。要么你必须将同步访问或使用其他集合像ConcurrentHashSet。

OK - now the stack trace is attached: ConcurrentModificationException You modify the ArrayList from different threads or iterating over it. Either you have to synchronize the access or use another collection like ConcurrentHashSet.

不过,也许下面的(我的出身答案)将是有趣太:P

But maybe the following (my origin answer) will be interesting too :p

听起来,你是与整个像素数据缓存真正的Bitmap对象 - >我猜,你有OutOfMemoryException异常

Sounds like, that you are caching the real Bitmap objects with the whole pixel data -> I guess, you got OutOfMemoryException

垃圾收集器是一个异步操作,如果你清楚你的位图阵,位图不从内存中卸载瞬间。如果插入新的,它可以发生,旧仍在内存和新的被载入了。

The garbage collector is an asynchronous operation and if you clear your bitmap array, the bitmaps are not unloaded from memory instantly. If you are inserting new ones, it can happen, that the old are still in memory and the new ones gets loaded too.

解决方法:不要缓存位图的方式,你正在做的。

Solution: Do not cache the bitmaps in that way, you are doing.

他们是不同的东西,你可以尝试(取决于具体YOUT scenrario):

They are different things, you can try (depends on yout concrete scenrario):

1),如果它们被存储SD卡或别​​的东西,只缓存路径上的位图您的收藏并加载它们,如果需要,他们绘制。

1) If they are stored on SD card or something else, cache only the path to the bitmaps in your collection and load them, if they are needed for drawing.

2)使用拖拉的对象,而不是位图 - 雷斯有优化的访问位图数据的一些有趣的方法和算法

2) Use Drawable objects instead of bitmaps - theys have some interesting methods and algorithm for optimized access to the bitmap data.

3)为了优化性能使用类似的 SoftReference的或类似的 - 也许的 WeakRefernce 。 SoftReferences可以通过GC点播(当内存变低)被卸载。在这种情况下,你必须检查,软refence是否为空或依然存在。
例如:

3) To optimize the performance use something like SoftReference or similar - maybe WeakRefernce. SoftReferences can be unloaded by the GC on demand (when memory gets low). In that case you have to check, whether the soft refence is null or still exists. E.g:

ArrayList<SoftReference<Bitmap>> _graphics = new ArrayList<SoftReference<Bitmap>>();
...

for (int i = 0; i < _graphics.size(); i++)
{
    Bitmap b = _graphics.get(i).get();
    if (b == null)
    {
        b = loadFromSomewhere(i);
        _graphics.add(new SoftReference<Bitmap>(b), i);
    }
    ... do something wwith your bitmap
}

这code snipet不测试或使用Java编辑器(请原谅打字错误或错误的方法签名)。

This code snipet is not tested or written with an Java Editor (please excuse typing errors or wrong method signatures).

这篇关于错误移除位图[Android版]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆