1

At the moment, I just have a relative layout with a few buttons, and have set android:background="#FFFFFF"

I would like to create a background image to use instead, however I'm unsure of what size image I should create.

I understand that for menu icons/launcher icons I needed to create 3 sets of them for ldpi, mdpi and hdpi.

However, for a background, what should the pixel sizes be for these be?

I will create a background similar to the following :

enter image description here

Jimmy
  • 16,123
  • 39
  • 133
  • 213

3 Answers3

2

not only are there different screen densities to worry about but the different screen sizes as well. it would be impractical to have a background image for each configuration. instead, I would recommend using java code to take an image and scale it to the screen size, no matter what the screen size or resolution.

in this example I pulled from a game I have, I have a bitmap bg and the screen resolution saved to maxwidth and maxheight. I compare the bitmap to the background, then set some scale values. oldx/oldy determine if the screen resolution has changed since the last time it checked. i create a matrix and resize the bitmap, save it to bgresized, then set my oldx/oldy so my next update is ready to rescale if need be. then, i draw the new background

this code I used a canvas, which you can use in your view's onDraw(Canvas canvas) method, or you dont like canvases, you could change it around to using it in the onCreate and orientation changes, or something similar

checkbackgroundsize();
bgwidth = bg.getWidth();
    bgheight = bg.getHeight();
    scalex = (float)maxwidth / (float)bgwidth;
    scaley = (float)maxheight / (float)bgheight;
    if ((oldx != scalex) || (oldy != scaley)){//if the screen changed sizes
        Matrix matrix = new Matrix();
        matrix.postScale(scalex, scaley);
        bgresized = Bitmap.createBitmap(bg, 0, 0, bgwidth, bgheight, matrix, true);
        oldx = scalex;
        oldy = scaley;
    }

    canvas.drawBitmap(bgresized, 0, 0, null);//draw resized background
corey
  • 498
  • 3
  • 10
  • Just a question: since your preferred approach is scaling images at runtime, what is the base resolution for your drawables? I can imagine you have drawables based on xhdpi and you only scale them down for other devices if needed since it seems like a better idea than stretching them out to enlarge, but I'm not sure. – Matthew Quiros Mar 01 '13 at 09:00
1

You should read Supporting Multiple Screens in best practices on Android developer guide.

Timuçin
  • 4,653
  • 3
  • 25
  • 34
0

I would also recommend looking into Android's 9 patch drawables (although your graphic may be too complicated for that)

Andrew Flynn
  • 1,501
  • 12
  • 17