androidbackground-drawable

Do SetBackgroundDrawable load image in memory?


i'm making an Android application, and i had some problem in report with "out of memory exception", after many search on the net, I found that android:background was loading background image in memory.

so i'll remove all my "android:background" declarations in my XML files, and replace it by :

    background = new BitmapDrawable(getResources(),ImageFactoring.decodeSampledBitmapFromResource(getResources(), R.drawable.catalogue_button_poeles_bois, mMaxButton_Cheminees.getLayoutParams().width, mMaxButton_Cheminees.getLayoutParams().height));
    mMaxButton_Cheminees.setBackgroundDrawable(background);

and before to do that, i'd want to know if "setBackgroundDrawable" is loading picture in memory or not ?


Solution

  • Of course, it does. All you see on display is stored im memory. After you set the background it is immediately loaded. Either while inflating from xml or when setting explicitly. Don't use images larger than needed as a resource. And try to avoid large HQ images where possible.