Android background image memory usage

Is that why it’s making the image so big?

Well, what’s happening is that setBackgroundResource(R.drawable.blockbackgroundbottom1) is going to cause Android to first do the BitmapFactory.decodeResource() thing you that experimented with, but then have the rendering logic scale the image to apply it as a background. So, for example, the 3MB difference between the Galaxy Nexus and the Nexus S probably reflects the size difference, in pixels, between the renditions of the LinearLayout.

There may also be some resampling going on based on screen density, depending upon where you have stored this image in your resource tree.

Is there any way to make it keep the original image size in any way?

Off the cuff, I would first try putting it in res/drawable-nodpi/ (to prevent any automatic density-based resampling), then manually get the Bitmap via the version of BitmapFactory.decodeResource() that takes the BitmapFactory.Options, so you can get it scaled as it is being read in. If that does not seem to help much, you may need to move the PNG out of drawable resources and into a raw resource or assets, as Android might still try holding onto an un-scaled copy of the image. I don’t think that it will if you use BitmapFactory.decodeResource() directly yourself, but I cannot rule it out.

Leave a Comment