Android mdpi、hdpi、xhdpi 文件夹如何工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11752997/
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
How mdpi, hdpi, xhdpi folder works?
提问by Muhammad Irfan
Extract from Android Developer Guide link above:
摘自上面的 Android 开发者指南链接:
320dp: a typical phone screen (240x320 ldpi, 320x480 mdpi, 480x800 hdpi, etc).
480dp: a tweener tablet like the Streak (480x800 mdpi).
600dp: a 7” tablet (600x1024 mdpi).
720dp: a 10” tablet (720x1280 mdpi, 800x1280 mdpi, etc)
320dp:典型的手机屏幕(240x320 ldpi、320x480 mdpi、480x800 hdpi 等)。
480dp:像 Streak (480x800 mdpi) 这样的 tweener 平板电脑。
600dp:7 英寸平板电脑 (600x1024 mdpi)。
720dp:10 英寸平板电脑(720x1280 mdpi、800x1280 mdpi 等)
So i got graphics(images) at resolution 320 pixels per inch from designer in these dimension only
所以我从设计师那里得到了分辨率为每英寸 320 像素的图形(图像),仅在这些维度上
480x800 hdpi
480x800 分辨率
720x1280 mdpi
720x1280 分辨率
800x1280 mdpi
800x1280 分辨率
I am confused which size of images should be placed in mdpi folder, hdpi folder and xhdpi folder. I want to make one application which can work on most android phones and tablets ?
我很困惑应该将哪种大小的图像放在 mdpi 文件夹、hdpi 文件夹和 xhdpi 文件夹中。我想制作一个可以在大多数安卓手机和平板电脑上运行的应用程序?
回答by Ted Hopp
You can create different graphic objects for use at different pixel densities. Android treats mdpi (160 pixels/inch) as the base density. So for mdpi devices, 1 dp = 1 pixel. At higher densities, there are more pixels per inch (240 for hdpi, 320 for xhdpi). Android attempts to make graphic images occupy the same physical dimensions on the screen regardless of the device pixel density. So if all it finds is an mdpi resource, and the device is hdpi, it will scale the graphic by 240/160 = 150%, and it will double the size of the graphic for xhdpi.
您可以创建不同的图形对象以用于不同的像素密度。Android 将 mdpi(160 像素/英寸)视为基本密度。所以对于 mdpi 设备,1 dp = 1 像素。在更高的密度下,每英寸有更多的像素(hdpi 为 240,xhdpi 为 320)。无论设备像素密度如何,Android 都会尝试使图形图像在屏幕上占据相同的物理尺寸。因此,如果它找到的只是一个 mdpi 资源,并且设备是 hdpi,它会将图形缩放 240/160 = 150%,并且它会将图形的大小加倍用于 xhdpi。
If you don't want this automatic scaling (which can make graphics look poor), you can simply supply your own version of graphic resources for use at higher densities. These graphics should be of the same size that Android would scale an mdpi resource.
如果您不想要这种自动缩放(这会使图形看起来很差),您可以简单地提供您自己的图形资源版本以用于更高的密度。这些图形的大小应与 Android 缩放 mdpi 资源的大小相同。
Note that the pixels/inch that was stored in the image file has nothing to do with this. It's all based on where you put the graphics files in the resources directory for your project. Any graphics placed in res/drawable
are assumed to be properly sized for mdpi displays, as are graphics placed in res/drawable-mdpi
. Image files that it finds in res/drawable-hdpi
are assumed to be properly sized for hdpi displays, etc. When your program runs on a particular device, Android will first look for a graphic that matches the display density of that device. If it does not find one but instead finds one for a different density, it will use that and automatically scale the image based on the above rules.
请注意,存储在图像文件中的像素/英寸与此无关。这完全取决于您将图形文件放置在项目资源目录中的位置。任何res/drawable
放置在res/drawable-mdpi
. 它在其中找到的图像文件res/drawable-hdpi
被假定为适合 hdpi 显示器等的大小。当您的程序在特定设备上运行时,Android 将首先查找与该设备的显示密度匹配的图形。如果它没有找到一个,而是找到了一个不同密度的,它将使用它并根据上述规则自动缩放图像。
回答by Pankaj Kumar
When you request a resource for which you provide alternatives, Android selects which alternative resource to use at runtime, depending on the current device configuration. To demonstrate how Android selects an alternative resource, assume the following drawable directories each contain different versions of the same images:
当您请求提供替代资源的资源时,Android 会根据当前设备配置选择在运行时使用哪个替代资源。为了演示 Android 如何选择替代资源,假设以下可绘制目录每个都包含相同图像的不同版本:
drawable/
drawable-en/
drawable-fr-rCA/
drawable-en-port/
drawable-en-notouch-12key/
drawable-port-ldpi/
drawable-port-notouch-12key/
And assume the following is the device configuration:
并假设以下是设备配置:
Locale = en-GB
Screen orientation = port
Screen pixel density = hdpi
Touchscreen type = notouch
Primary text input method = 12key
By comparing the device configuration to the available alternative resources, Android selects drawables from drawable-en-port.
通过将设备配置与可用的替代资源进行比较,Android 从drawable-en-port 中选择可绘制对象。
The system arrives at its decision for which resources to use with the following logic:
系统通过以下逻辑决定使用哪些资源:
Ref : How Android Finds the Best-matching Resource
Other References : Density independence, Providing Alternative Resourcesand Best Practices
And I will say that you should read complete pageSupporting Multiple Screens, I don't think nothing will be better documentation than it...
我会说你应该阅读完整的页面Supporting Multiple Screens,我认为没有比它更好的文档......
回答by karlstackoverflow
I'm confused myself with all the screen size fragmentation but the basics are: 1. You need to create various folders under layouts to work with your images 2. Images will exist in the drawables folders also under various folders. 3. You should have a basic /layout and /drawable folder to accompany non-specific folders 4. Work from xhdpi then scale images down!
我对所有屏幕尺寸碎片感到困惑,但基础知识是: 1. 您需要在布局下创建各种文件夹来处理您的图像 2. 图像将存在于各种文件夹下的 drawables 文件夹中。3. 你应该有一个基本的 /layout 和 /drawable 文件夹来伴随非特定文件夹 4. 从 xhdpi 工作然后缩小图像!
Examples for specific screen folders: /layout-hdpi /layout-xhdpi /drawable-hdpi /drawable-xhdpi
特定屏幕文件夹的示例:/layout-hdpi /layout-xhdpi /drawable-hdpi /drawable-xhdpi
From what I know: 480 x 800 is hdpi (older phones eg S2, HTC Desire etc) 720 x 1280 is xhdpi (new phones eg S3, Galaxy Nexus etc)
据我所知:480 x 800 是 hdpi(旧手机,例如 S2、HTC Desire 等) 720 x 1280 是 xhdpi(新手机,例如 S3、Galaxy Nexus 等)
Basically, Depending on the phone, android will grab resources from the necessary folder and if there is none then it will grab from the main '\layout' or '\drawable' folder. For example, the app running on a Galaxy Nexus will grab resources from '\layout-xhdpi' if the folder exists.
基本上,根据手机,android 将从必要的文件夹中获取资源,如果没有资源,则它将从主 '\layout' 或 '\drawable' 文件夹中获取。例如,如果文件夹存在,在 Galaxy Nexus 上运行的应用程序将从“\layout-xhdpi”中获取资源。
回答by mbelsky
yes, you can make one app, but was need creating folders: /res/drawable, /res/drawable-mdpi, /res/drawable-hdpi and add content for all screen sizes
是的,您可以制作一个应用程序,但需要创建文件夹:/res/drawable、/res/drawable-mdpi、/res/drawable-hdpi 并为所有屏幕尺寸添加内容