具有相同密度的Android多种屏幕尺寸

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2638202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:42:24  来源:igfitidea点击:

Android multiple screen sizes with same density

android

提问by droidy

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.

我对密度感到困惑。我看到在中等密度下,屏幕分辨率可以是 320x480、480x800 或 480x854。因此,如果我在 mdpi 文件夹中有一个 300px 宽的图像,它如何在所有 3 个不同的屏幕尺寸(主要是 320x480 与其他 2 个)上看起来相同?通过看起来相同的尺寸,我的意思是根据屏幕尺寸放大或缩小。谢谢。

回答by Steve Haley

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

这里需要理解三个不同但相互关联的概念:屏幕密度(每英寸/厘米的像素数,或通常称为打印机中每英寸点数的 DPI)、物理屏幕尺寸(以英寸或厘米为单位)和像素数(也称为分辨率,以像素为单位)。

These terms are notinterchangeable, 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.

这些术语不可互换,您需要了解它们是如何相互关联的,以免与问题混淆。通常,您可以忽略物理屏幕尺寸,因为它已经在密度中考虑在内。例如,3 英寸宽和 300 像素的屏幕的 DPI 为 100。此外,即使像素数量非常不同,手机屏幕也往往具有大致相同的物理尺寸。

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.

因此,让我们考虑分辨率为 480x320 且密度约为 160dpi 的 G1 或 Hero 的屏幕。300 像素宽的图像将是 1.875 英寸宽。这是通过像素大小 (300) / 密度 (160) 计算的。现在,如果将其与 Nexus One、Droid 或类似产品的屏幕进行比较,这些型号的屏幕分辨率更高,约为 800x480,密度约为 240dpi。如果您显示相同的 300 像素宽图像,它现在只会以大约 1 又四分之一英寸的宽度实际显示。换句话说,它会小得多。这可能是一个问题,因为如果图像包含文本,则文本可能不再可读。

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.

可以告诉 Android 自动缩放图像以适应这些不同的屏幕,使其看起来仍然是相同的大小。这是通过在密度独立像素中设置大小来完成的。如果某物的宽度为 100dp,那么它在中等密度屏幕上的宽度为 100px。在高密度屏幕上,它的宽度为 150 像素,但它们在实际屏幕上看起来大小相同。但是,如果您这样做,您的图像可能会有点模糊。这与在图片查看程序中将照片放大得太近是一样的;边缘变得模糊,因为它在缩放时“拉伸”它们。

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.

解决这个问题的方法是使用 mdpi、hdpi 等文件夹。你给 Android 一个已经缩放的图像,这样它就不必自己做。显然,如果你只是在 Photoshop 中自己拉伸图像,那么它看起来不会更好。但通常一种方法是将非常大的图像缩小以使其适合移动设备屏幕。在这种情况下,您只需将它们的大小调整三次,每次调整为不同的分辨率。

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.

因此,最后回答您的具体问题:如果您在 mdpi 文件夹中放置了一个图像,那么无论屏幕分辨率如何,只要它们的密度相同,它的大小都将完全相同。将会改变的是它们周围有多少空间,例如,320x320px 宽的图像将填满 320x480 屏幕的大部分,但仅占 480x800 屏幕的三分之一左右。然而,如上所述,通常更高分辨率的手机也有更密集的屏幕。在这种情况下,Android 不会在您的 mdpi 文件夹中查找图像 - 它会转到 hdpi 文件夹,如果在那里找不到它,它将使用默认的“drawable”文件夹。然后,如果您使用 DP,它会自动缩放它,或者如果您使用 PX,它会保持原样,它只会看起来更小。

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

那里!给你一个很长的答案。我希望这是有道理的。

回答by rndStr

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
用法:

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):

将您的应用限制为特定的屏幕尺寸(通过 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:

对于代码级别的 tweeking:

float scale = getContext().getResources().getDisplayMetrics().density;
并且不要忘记:

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

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

这一切都在这个文档中:developer.android.com: Supporting Multiple Screens

回答by CommonsWare

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)?

因此,如果我在 mdpi 文件夹中有一个 300px 宽的图像,它如何在所有 3 个不同的屏幕尺寸(主要是 320x480 与其他 2 个)上看起来相同?

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

图像的物理外观取决于屏幕密度,而不是屏幕尺寸。您的-mdpi文件夹与屏幕尺寸无关——它与屏幕密度相关。

回答by Ram

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

密度 = SQRT (wp^2 + hp^2)/屏幕尺寸

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

wp -> 屏幕宽度(以 px 为单位) hp -> 屏幕高度(以 px 为单位)屏幕尺寸 -> 物理屏幕尺寸(对角线英寸)

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

屏幕尺寸 (320x480) = SQRT(102400 + 230400) /160 = 3.6 英寸

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

屏幕尺寸 (480x800) = SQRT(640000 + 230400) /160 = 5.8 英寸

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

屏幕尺寸 (480x854) = SQRT(729316 + 230400) /160 = 6.12 英寸

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.

因此,布局(UI 屏幕)由屏幕尺寸(小:<3",正常 <4",大 >5")驱动,可绘制资源(图像)由屏幕密度驱动。

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

而且,如果屏幕的密度(320x480、480x800 或 480x854)相同,则图像的大小(以像素为单位)不会改变。

Could you please confirm?

能否请你确认?

Thanks, Ram

谢谢,拉姆

回答by Jim Blackler

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

这就是设备独立像素 (DIP) 的用途。而不是320px320dip

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

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

回答by Michelle

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);