Drawable vs Single reusable Bitmap better with memory?

余生长醉 提交于 2019-11-28 05:25:53

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));

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.

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.

Bitmaps do not need to be manually recycled. They are garbage collected just like Drawables and other objects. Similarly you don't need to unbind drawables, except in very specific situations. It seems that you read a lot of misleading information.

Recycling bitmaps and unbinding drawable can be useful in some situations (for instance if your app manipulate large amounts of bitmap data or if you store drawable in a static manner.)

The two examples you show at the beginning of your question are equivalent. If you load a drawable directly, bitmaps will be loaded on your behalf. If you load bitmaps manually and set them on an ImageView, they will be enclosed in drawables on your behalf.

Use the first solution since it's simpler and don't worry about unbinding and other memory management techniques until you actually need them.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!