I’m trying to make an Android app which has about 20-30 resource images. These images are used to make continuous animation showing on canvas,so I’m wondering what container should I use to store these images for best performance?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It sounds like you already understand that these images should go into the res/drawable directory (if not, that’s where they go).
assuming you name your images something like “image0”, “image1”, “image2” etc you can actually loop through those strings and load them like this:
This will load your images into that ArrayList.
You can then simply
myCanvas.drawBitmap(myList.get(i));to draw them into your canvas one at a time.Keep in mind however (and this is VERY important) that this is EXTREMELY expensive in terms of memory consumption. It doesn’t matter if your images are really nicely compressed JPGs, when you load them into Bitmaps, they explode with each pixel taking up as much space as necessary as per whatever configuration you’re using to load the image. So, if you’re doing this with some small images to run them as an animation, that will work ok, but if you’re hoping to have something that is the size of the device (like a wallpaper) you will definitely get an OutOfMemory Exception, unless you load and release the bitmaps as you go (calling
recycle()on them).However, if you do call recycle() on them as you animate, then you will see a lot of pausing in your animation as the images load.