问题
I am making a small game for android. It is working smoothly when its working on a white background but when i want to put a background image, its becoming slower and slower. First i tried to add it as a layout background image but it was very slow, the animations were happening frame by frame. And then i decided to draw the background image as a Bitmap on canvas, the result was a little faster than the previous one but still its nothing comparing to the white background.
I tried to scale the image many times, i did it with higher resolution, lower resolution, but the result didnt change, its still slower.
So i wanted to ask, is there a way to add a background image to an android app without losing the smoothness and speed of the application?
Thanks
P.S: I am trying my application on Sony Tablet S
回答1:
its pretty late but if someone else have the same problem ; on the draw it may try to resize again, you can use no-dpi (drawable-nodpi / drawable-sw600dp-nodpi) folder to skip resizing which requires so much cpu and time.
also something else may cause this , which i assume you forget to recycle old your bitmaps.
回答2:
Recently i had the same issue and no answer helped me.But after playing with codes so many hours i found an amazing solution.What you have to do is copy your background image to all the drawable folders (drawable-hdpi,drawable-xhdpi...etc.) and thats it. And also my background image is png with 590kb file size. Be careful with image size because higher the size is slower the app. Hope this help to someone. :-)
回答3:
The main reason you're noticing the lag is because the image needs to because you need to copy your background image to all the drawable folders (drawable-hdpi,drawable-xhdpi...etc.) . I use the Android Studio plugin named Android Drawable Importer for automating. Works like a charm.
回答4:
For excellent performance use tiled background you can find nice explanation for it here
回答5:
I was in the same position as you. I have a very simple app and it has lags when i add some background. Try this app 9-patch-resizer - Transform the image dimensions for all screens
And then comes this tool, that takes a xhdpi PNG file, or 9.png file, and generates ldpi, mdpi and hdpi png files automatically.
When it transoforms the image , move the each file to its own project folder (resources/mipmap-hdpi ,mipmap-xhdpi ...etc)
来源:https://stackoverflow.com/questions/17335874/slow-app-with-background-image