Search code examples
androidandroid-2.1-eclair

Is there a recommended pixel size for android backgrounds?


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


Solution

  • 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