tags:

views:

1216

answers:

4

I'm confused regarding the densities. I see that with medium density, the screen resolution could be either 320x480, 480x800, or 480x854. So if I have an image thats 300px wide in the mdpi folder, how is it going to look the same size on all 3 different screen sizes (mainly 320x480 vs the other 2)? And by look the same size, I mean scale to be bigger or smaller depending upon the screen size. Thanks.

+1  A: 

This is what Device Independent Pixels (DIPs) are for. Instead of 320px write 320dip.

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

Jim Blackler
+1  A: 

So if I have an image thats 300px wide in the mdpi folder, how is it going to look the same size on all 3 different screen sizes (mainly 320x480 vs the other 2)?

How the image looks, physically, is driven by screen density, not screen size. Your -mdpi folder is not tied to screen size -- it is tied to screen density.

CommonsWare
+12  A: 

There are three distinct but linked concepts to understand here: screen density (pixels per inch/centimeter, or commonly known as DPI from dots per inch in printers), physical screen size (in inches or centimeters) and number of pixels (also known as resolution, in pixels).

These terms are not interchangeable, and you need to understand how they interlink to not be confused with the issue. Generally, you can ignore physical screen size since that's already accounted for in the density. For example a screen 3 inches wide and 300 pixels across will have a DPI of 100. Furthermore phones screens tend to have about the same physical size, even if the number of pixels is very different.

So, let's consider the screen of a G1 or Hero which has a resolution 480x320 and a density of approx 160dpi. An image 300 pixels wide will be 1.875 inches across. This is calculated by pixel size (300) / density (240). Now if you compare this to the screen of the Nexus One, Droid or similar, these models have a higher resolution screen of approx 800x480 with a high density of approx 240dpi. If you display the same 300px wide image, it will now only physically be displayed at about one and a quarter inches across. In other words, it will be much smaller. This can be a problem because if the image contains text, then the text might not be readable anymore.

Android can be told to automatically scale images to fit these different screens so that it still looks to be the same size. This is done by setting sizes in Density Independent pixels. If something is 100dp wide, it will be 100px wide on a medium density screen. On a high density screen, it will be 150px wide, but they will both look about the same size on the actual screen. However, if you do this, your image can go a bit blurry. It's the same as when you zoom into a photo too closely in a picture viewing program; the edges go blurry since it 'stretches' them while you zoom.

The way to solve this is to use the mdpi, hdpi and so forth folders. You're giving Android an image that has already been scaled, so that it doesn't have to do it itself. Obviously if you just stretch the image yourself in Photoshop, then it won't look any better. But normally one is resizing very large images down to make them fit the mobile screen. In that case, you just resize them three different times, each into a different resolution.

So to finally answer your specific question: if you have an image placed in your mdpi folder, it will be exactly the same size regardless of the screen resolution, as long as they are all the same density. What will change is how much space around them, e.g. a 320x320px wide image would fill most of a 320x480 screen, but only about a third of a 480x800 screen. However, as noted above, generally the higher resolution phones also have a more dense screen. In that case, Android won't look in your mdpi folder for the image - it will go to the hdpi folder, and if it can't find it there, it will take the default "drawable" folder. Then if you've used DP it will automatically scale it, or if you've used PX, it will leave it as is, and it will just look smaller.

There! A very long answer for you. I hope it makes sense.

Steve H
how to differenciate the 480X800 and 480X854 screens. we have an option to put the one image at the hdpi folder.in my case 480X800 image is does not support for the 480X854 image.Any Idea?
Praveen Chandrasekaran
@Praveen Chandrasekaran: As far as I know, you can't distinguish between the two relying on the hdpi folder, but that's normal. Both screens will probably have the same density. However, you can get the screen size programmatically. See the Display Metrics class (http://developer.android.com/reference/android/util/DisplayMetrics.html) This is a much more reliable way to do it - a screen's density is independent from its number of pixels. For example, there's no absolute reason why a screen couldn't have high density and have only 480x320 pixels.
Steve H
"An image 300 pixels wide will be 1.875 inches across. This is calculated by pixel size (300) / density (240)" the 240 should be 160 as per the sentence before
Jack BeNimble
A: 

Could you please confirm the formula for calculating the screen density?

As I have read, the following is the formula:

Density = SQRT (wp^2 + hp^2)/screen size

wp -> width of the screen (in px) hp -> height of the screen (in px) screen size -> Physical screen size (diagonal inches)

screen size (320x480) = SQRT(102400 + 230400) /160 = 3.6 inches

screen size (480x800) = SQRT(640000 + 230400) /160 = 5.8 inches

screen size (480x854) = SQRT(729316 + 230400) /160 = 6.12 inches

So, the layouts (UI screens) are driven by screen sizes (small: <3", normal <4", large >5") and drawable resources (images) are driven by screen densities.

And, the size of the image (in pixels) does not change if the density of the screens (320x480, 480x800, or 480x854) are the same.

Could you please confirm?

Thanks, Ram

Ram