可绘制VS单可重复使用的位图的内存比较好? [英] Drawable vs Single reusable Bitmap better with memory?

查看:184
本文介绍了可绘制VS单可重复使用的位图的内存比较好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,(不,我是正确的),可绘制一般正确地从内存中删除当应用程序完成它们。然而,位图需要手动回收,有时甚至有一类特殊的写入妥善处理。 我的问题是,在关于内存泄漏,是否更有利于简单地可绘制坚持像这样的:

As I understand it (not that I'm correct) Drawables are generally correctly removed from memory when the application is finished with them. Bitmaps however need to be manually recycled, and sometimes even have a special class written to handle them properly. My question is, in regards to memory and leaks, is it more beneficial to simply stick with Drawables like such:

myView.setBackgroundDrawable(getResources().getDrawable(R.drawable.my_image));
myView1.setBackgroundDrawable(getResources().getDrawable(R.drawable.my_image1));
myView2.setBackgroundDrawable(getResources().getDrawable(R.drawable.my_image2));

而不是像这样使用位图:​​

rather than something like so with Bitmaps:

Bitmap tmpBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.my_image);
myView.setImageBitmap(tmpBitmap);

tmpBitmap.recycle();
tmpBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.my_image1);
myView1.setImageBitmap(tmpBitmap);

tmpBitmap.recycle();
tmpBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.my_image2);
myView2.setImageBitmap(tmpBitmap);
tmpBitmap.recycle();

我也看到当然,你必须要小心,位图的循环()方法,因为它们可以在仍在使用被删除了?好像这些问题保持雨后春笋般冒出来以不同的形式,但我真的不能得到一个明确的答案,从任何人的事。一个人说,重复使用位图,每次使用后回收,和其他人说,使用可绘制和unbindDrawables()方法(这是我一直使用的是什么):

I've also read of course that you have to be careful about recycle() method on Bitmaps because they can be removed while still in use? It seems like these issues keep popping up in different forms, but I can't really get a straight answer from anyone on the matter. One person says to reuse a Bitmap and recycle after every use, and others say use Drawables and an unbindDrawables() method (this is what I've been using):

private void unbindDrawables(View view) {
    if (view.getBackground() != null) {
        view.getBackground().setCallback(null);
    }
    if (view instanceof ViewGroup) {
        for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
            unbindDrawables(((ViewGroup) view).getChildAt(i));
        }
        ((ViewGroup) view).removeAllViews();
    }
}

任何适用的见解将是非常美联社preciated虽然。谢谢

Any applicable insight would be much appreciated though. Thanks

推荐答案

我回罗曼的建议,但我不知道你的问题是解决实际问题。我不知道你是怎么处理的参考大家的意见。也许你只是在你的应用程序的内存泄漏?大量的内存泄漏在Android中都涉及到上下文。当绘制对象附属于查看查看被设置为在绘制对象

I back Romain's proposal, but I'm not sure your question is addressing your actual problem. I don't know how you handle the references to your Views. Maybe you simply have memory leaks in your application? A lot of memory leaks in Android are related to Context. When a Drawable is attached to a View, the View is set as a callback on the Drawable.

TextView myView = new TextView(this);
myView.setBackgroundDrawable(getDrawable(R.drawable.my_bitmap));

在上面的code段,这意味着绘制对象有参考的TextView 这本身有一个参照活动(即上下文),这反过来引用了pretty的任何东西视您的code。

In the code snippet above, this means the Drawable has a reference to the TextView which itself has a reference to the Activity (the Context) which in turns has references to pretty much anything depending on your code.

不看你更多的code我想你是在正确的轨道由存储可绘回调设置为活动被销毁。

Without looking at more of your code I guess you're on the right track by setting the stored drawables' callbacks to null when an Activity is destroyed.

这篇关于可绘制VS单可重复使用的位图的内存比较好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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