Android multiple screen sizes with same density

后端 未结 6 729
你的背包
你的背包 2020-11-29 16:49

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

相关标签:
6条回答
  • 2020-11-29 17:20

    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.

    0 讨论(0)
  • 2020-11-29 17:24

    For completeness, also check these option for controlling layout:

    Directory qualifiers:

    Size:         small, normal, large
    Density:      ldpi, mdpi, hdpi, nodpi(no auto-scale)
    Aspect ratio: long, notlong
    Orientation:  land
    
    Usage:

    res/layout/my_layout.xml            
    res/layout-small/my_layout.xml      
    res/layout-large/my_layout.xml      
    res/layout-large-long/my_layout.xml      
    res/layout-large-land/my_layout.xml     
    res/drawable-ldpi/my_icon.png  
    res/drawable-mdpi/dpi/my_icon.png  
    res/drawable-hdpi/my_icon.png      
    res/drawable-nodpi/composite.xml   
    

    Restricting your app to specific screen sizes(via the AndroidManifest):

    <manifest xmlns:android="http://schemas.android.com/apk/res/android">
    ...
    <supports-screens
              android:largeScreens="true"
              android:normalScreens="true"
              android:smallScreens="true"
              android:anyDensity="true" />
    ...
    </manifest>

    And for code level tweeking:

    float scale = getContext().getResources().getDisplayMetrics().density;
    
    And don't forget:

    dpi    = 160; //At 160dpi
    pixels = dips * (density / dpi)
    

    It's all in this doc: developer.android.com:Supporting Multiple Screens

    0 讨论(0)
  • 2020-11-29 17:30

    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

    0 讨论(0)
  • 2020-11-29 17:34

    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 (160). 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.

    0 讨论(0)
  • 2020-11-29 17:34

    Actually the code to calculate physical screen size for devices is the following one:

    DisplayMetrics dm = new DisplayMetrics();
    
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    
    double x = Math.pow(dm.widthPixels/dm.xdpi,2);
    
    double y = Math.pow(dm.heightPixels/dm.ydpi,2);
    
    double screenInches = Math.sqrt(x+y);
    
    0 讨论(0)
  • 2020-11-29 17:35

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

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

    0 讨论(0)
提交回复
热议问题