Most memory-efficient way to scale bitmap for live wallpaper?
I don't believe this is a question lim开发者_JAVA百科ited to live wallpapers, but it is the use case I am trying to work with.
I have a 256 x 256 bitmap created as a "tile" for my live wallpaper. I am looking for a memory efficient way to scale this to fit an entire device screen. I continue to run into "exceeding VM memory" issues whenever the device rotates back and forth, which suggests I am not handling the problem correctly.
The image is being drawn using Canvas' drawBitmap()
method. I have tried the following options:
- Create the bitmap, then call
createScaledBitmap()
- Create the bitmap, then call
scale()
on my canvas - Create the bitmap, then create a matrix to "stretch" the width and height, and apply it with a
preScale()
If I call recycle()
the live wallpaper runs for one frame, then crashes. If I don't rotate the device at all, I get no out of memory crashes.
Thanks for any suggestions!
When you rotate, are you preventing the application from 'restarting' like most android apps do? If it 'restarts' (not sure of the terminology off hand) it will trigger you to create the image again. It is likely the case that the previous image has not yet been collected, so you are running out of memory. What you should be doing is making sure the application does not 'reset' (I will remember the correct wording) when you rotate, which is an application property I believe. Then you trap the rotate event and handle changing the drawing orientation there without allocating any new objects.
Hope that makes sense.
精彩评论