Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Widget Bitmap Sizes

Hallo,

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

According to the 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?

TIA,

-Frink

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?

like image 522
FrinkTheBrave Avatar asked Jan 08 '11 23:01

FrinkTheBrave


People also ask

What is 4x pixel density?

In 4x screen (~640 DPI) it takes four times as many pixels (352 x 144 PX) to fill the area of 88 x 36 density-independent-pixels.

What is regular bitmap in Android?

Config. ARGB_8888); A bitmap is simply a rectangle of pixels. Each pixel can be set to a given color but exactly what color depends on the type of the pixel.

Why is it problematic to define size using pixels on Android?

Defining dimensions with pixels is a problem because different screens have different pixel densities, so the same number of pixels may correspond to different physical sizes on different devices.

How do you handle bitmaps in Android?

For most cases, we recommend that you use the Glide library to fetch, decode, and display bitmaps in your app. Glide abstracts out most of the complexity in handling these and other tasks related to working with bitmaps and other images on Android.


1 Answers

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

http://developer.android.com/guide/practices/screens_support.html

The formula is 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.

http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch

http://developer.android.com/guide/developing/tools/draw9patch.html

like image 51
Computerish Avatar answered Oct 21 '22 20:10

Computerish