Android Widget Bitmap Sizes

為{幸葍}努か 提交于 2020-01-11 19:53:02

问题


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?


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/4636767/android-widget-bitmap-sizes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!