Tamaños de mapas de bits de widgets de Android


I am writing a widget, but am unsure what size bitmap to use as a background for each screen resolution/density.

Según el Android Developer App Widgets tutorial:

To find your minimum width and height in density-independent pixels (dp), use this formula:
(number of cells * 74) - 2
Following this formula, you should use 72 dp for a height of one cell, 294 dp and for a width of four cells

So if my widget is 72dp x 294dp, what size bitmaps do I need for my ldpi, mdpi and hdpi drawables?

Also, will I need any particular supports-screens settings in my manifest?



Update: I've made some checkerboard patterns in various sizes and colours for each drawable- folder and tried them out. Is this density thing a bit of a red herring?

I've just done a screen-grab of my emulator running as HVGA, medium density, 320x480. The size taken up by my widget is 320x100, so I create a 320x100 bitmap in the mdpi folder as a background for my widget it looks perfect both on my emulator and LG GT540.

And for an emulator running as WVGA854, high density, 480x854. The size of my widget is 480x150. Creating a background this size and placing it in the hdpi folder displays correctly on this emulator. I have no hardware to test this on tho :-(

Update2: I think I can explain my problem better now :-)

If I have three devices:

Device1, resolution 320x480, density ldpi
Device2, resolution 320x480, density mdpi
Device3, resolution 400x854, density mdpi

The physical size of the screens would probably all be different, but I don't think the actual sizes matter

I've worked out that Device1 and Device2 will need a background of 320x100, whereas Device3 will need 400x150 So what size background goes into the mdpi folder to display properly on Device2 and Device3?

preguntado el 08 de enero de 11 a las 23:01

I'm also interested in the answer. My widget is a single cell, and image sizes I used are squares of side 72px, 100px and 150px. These were just guesses though, so I would be very interested to know the "official" numbers. -

In response to your update, it sounds like it's working, right? Or is there a problem? Also, when you launch your emulator, there is a setting (or it might be in emulator settings) to scale the emulator to "real" size, which might help you test. -

Computerish - please see Update2! I am looking at my emulators in 'real' size, ta -

2 Respuestas

You just need to convert 72dp x 294dp into actual pixel sizes for ldpi, mdpi, hdpi. That process is explained here:


La formula es pixels = dps * (density / 160), so in your case, 72 dp x 294 dp would equal:

  • ldpi (120): 54 x 221 pixels
  • mdpi (160): 72 x 294 pixels
  • hdpi (240): 108 x 441 pixels
  • xhdpi (320): 144 x 588 pixels

Update in response to your second update: Density-based resources are aimed at making sure that graphics are (roughly) the same actual size across devices. In your case, though, you don't care about the actual size, you care about the background filling up whatever space it is supposed to fill for the widget. You should be able to handle this issue for most backgrounds by created an Nine Patch image. The Nine Patch will scale according to the size required.



Respondido el 21 de enero de 11 a las 19:01

Cheers, I'll give that a go when I next get a chance - FrinkTheBrave

@FrinkTheBrave please let us know how you get on - dave.c

Computerish, it seems to me that the drawable folders should be screen size related, rather than density? - FrinkTheBrave

The drawable folders are based on density, not screen size. A set of four generalized densities: ldpi (low), mdpi (medium), hdpi (high), and xhdpi (extra high) From the same link above. - Computarizado

The idea of the density-based folders is to make the graphics appear the same size no matter what the density of the screen. The XML layouts can be defined per screen size if you want, though. - Computarizado

There is good advice from Google on icon sizes here: http://developer.android.com/guide/practices/ui_guidelines/icon_design.html

I guess they should apply to custom widgets too.

Respondido el 09 de enero de 11 a las 03:01

The icon and drawable design guidelines seem to apply to everything excepto widget images. Hence the confusion. - dave.c

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.