31

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

koraxis
  • 801
  • 2
  • 12
  • 22
  • 1
    Yes- you just add the background via the xml. But you're mentioning animations- is this an animated image? And what code are you using to display the image, especially if its animated – Gabe Sechan Jun 27 '13 at 06:38
  • What is a size of this image? – Piotr Chojnacki Jun 27 '13 at 06:39
  • @PiotrChojnacki i have different size of the image, its 1920x1080 and 1280x720 – koraxis Jun 28 '13 at 08:40
  • @GabeSechan the image is not animated, its just a normal png background – koraxis Jun 28 '13 at 08:40
  • @GabeSechan I first put it with android:background to the layout. But it was too slow , and then i put it inside of a custom view, and created a bitmap from it and then onDraw i used canvas.drawBitmap() – koraxis Jun 28 '13 at 08:55
  • I almost guarantee that its not the background causing problems. The framework will be doing the exact same thing you're doing- it will create a Bitmap when it creates the view, and then in onDraw it will draw the background bitmap using drawBitmap before drawing the rest of the view. – Gabe Sechan Jun 28 '13 at 09:04

7 Answers7

47

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.

Alp
  • 1,863
  • 1
  • 20
  • 38
14

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. :-)

SajithK
  • 832
  • 10
  • 17
  • I had a problem with animations when transitioning views with Xamarin Forms targeting Android. Firstly, all my images were only on the resources/drawable. Once I copied them to the different drawable folders, the view transition worked smoothly. Hint: use correctly sized images for each dpi. – Joao de Araujo Sep 05 '17 at 01:18
4

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.

1

For excellent performance use tiled background you can find nice explanation for it here

mmoghrabi
  • 1,233
  • 1
  • 14
  • 23
1

enter image description here

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)

BeatingBytes
  • 353
  • 1
  • 4
  • 8
0

Background image Resolution to maintain smoothness

DPI Size in Pixels
xxxhdpi 1280x1920
xxhdpi 960x1600
xhdpi 640x960
hdpi 480x800
mdpi 320x480
ldpi 240x320

You need to use background pictures matching the resolution as above to avoid lagging or being slow down.!

If you use high resolution images which exceeds the above restrictions would actually cause this issue and it's advisable to use only png

myeongkil kim
  • 2,465
  • 4
  • 16
  • 22
-1

Iam also facing the same problem, I have a very simple app when Iam running it on a white background it is working completely fine. But when Iam adding a background Image it is Lagging, but after some tweaks I have figured out how to smoothly run the app with a background Image. You just need to resize your Background Image, and also lower the quality of Image, this is because the size of Image is directly proportional to your apps performance. So the only solution is to resize it and decrease the quality of Image. I hope my answer will be helpful to you.