关于 Android 图像和资产大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11581649/
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
About Android image and asset sizes
提问by manuelBetancurt
I need to clarify some doubt about the image assets for my app,
我需要澄清一些关于我的应用程序图像资产的疑问,
if I specify in an xml file that the height of something [image view] is 50 dip height
如果我在 xml 文件中指定某物的高度 [图像视图] 是 50 倾角高度
which type of screen should i choose from the resources folder?
我应该从资源文件夹中选择哪种类型的屏幕?
drawable, hdpi, ldpi, mdpi, xhdpi,
to have the 50 px height image,
拥有 50 像素高度的图像,
and what is the percentage for bigger, smaller size images compared to the base image,
与基本图像相比,更大、更小尺寸的图像的百分比是多少,
like in iOS, @2x, is literally 2 times the size of the image, and you say programatically the normal size,
就像在 iOS 中一样,@2x 实际上是图像大小的 2 倍,并且您以编程方式说正常大小,
thanks!
谢谢!
回答by Kevin Coppock
mdpi
is the reference density -- that is, 1 px on an mdpi
display is equal to 1 dip. The ratio for asset scaling is:
mdpi
是参考密度——也就是说,mdpi
显示器上的 1 像素等于 1 倾角。资产扩容比例为:
ldpi | mdpi | tvdpi | hdpi | xhdpi | xxhdpi | xxxhdpi
0.75 | 1 | 1.33 | 1.5 | 2 | 3 | 4
Although you don't really need to worry about tvdpi
unless you're developing specifically for Google TV or the original Nexus 7 -- but even Google recommends simply using hdpi
assets.
尽管tvdpi
除非您是专门为 Google TV 或最初的 Nexus 7 开发,否则您真的不需要担心——但即使是 Google 也建议简单地使用hdpi
资产。
What this means is if you're doing a 48dip image and plan to support up to xxhdpi resolution
, you should start with a 144px image (192px if you want native assets for xxxhdpi) and make the following images for the densities:
这意味着如果您正在制作 48dip 图像并计划最多支持xxhdpi resolution
,则应从 144px 图像(如果您想要 xxxhdpi 的本机资源为 192px)开始,并为密度制作以下图像:
ldpi | mdpi | tvdpi | hdpi | xhdpi | xxhdpi | xxxhdpi
36 x 36 | 48 x 48 | 64 x 64 | 72 x 72 | 96 x 96 | 144 x 144 | 192 x 192
And these should display at roughly the same size on any device, provided you've placed these in density-specific folders (e.g. drawable-xhdpi
, drawable-hdpi
, etc.)
而这些应该显示在大致任何设备上相同的大小,只要您放置在这些密度的特定文件夹(例如drawable-xhdpi
,drawable-hdpi
等)
For reference, the pixel densities for these are:
作为参考,这些的像素密度是:
ldpi | mdpi | tvdpi | hdpi | xhdpi | xxhdpi | xxxhdpi
120 | 160 | 213 | 240 | 320 | 480 | 640
回答by electronix384128
Based on kcoppock's answer I have created the following shell script to automatically resize all images to the correct size and copy them in the respective Android drawable-* - folders!
基于kcoppock的回答,我创建了以下 shell 脚本来自动将所有图像调整为正确的大小并将它们复制到相应的 Android drawable-* - 文件夹中!
Create a shell script and paste the following code:
创建一个 shell 脚本并粘贴以下代码:
createAndroidImages.sh
创建AndroidImages.sh
#!/bin/bash
read -p "Please enter the subfolder of the original images? " folder
read -p "How many DP (width) should the image have? " dp
for i in $(find $folder/. -type f -name "*[A-Z]*"); do mv "$i" "$(echo $i | tr A-Z a-z)"; done
mkdir drawable-ldpi
mkdir drawable-mdpi
mkdir drawable-tvdpi
mkdir drawable-hdpi
mkdir drawable-xhdpi
mkdir drawable-xxhdpi
mkdir drawable-xxxhdpi
cp $folder/* drawable-ldpi/
cp $folder/* drawable-mdpi/
cp $folder/* drawable-tvdpi/
cp $folder/* drawable-hdpi/
cp $folder/* drawable-xhdpi/
cp $folder/* drawable-xxhdpi/
cp $folder/* drawable-xxxhdpi/
sips -Z $(echo $dp*3/4 | bc) drawable-ldpi/*
sips -Z $(echo $dp | bc) drawable-mdpi/*
sips -Z $(echo $dp*4/3 | bc) drawable-tvdpi/*
sips -Z $(echo $dp*3/2 | bc) drawable-hdpi/*
sips -Z $(echo $dp*2 | bc) drawable-xhdpi/*
sips -Z $(echo $dp*3 | bc) drawable-xxhdpi/*
sips -Z $(echo $dp*4 | bc) drawable-xxxhdpi/*
Put your script in a folder and your original images in a subfolder e.g.:
将您的脚本放在一个文件夹中,将您的原始图像放在一个子文件夹中,例如:
/
.. createAndroidImages.sh
.. originalImages/
....a123.png
....b456.png
Run the shell script in terminal: sh createAndroidImages.sh
在终端中运行 shell 脚本: sh createAndroidImages.sh
To copy the created images directly to your Android Studio Project:
要将创建的图像直接复制到您的 Android Studio 项目:
cp -R drawable-* ~/AndroidStudioProjects/ESCRating/app/src/main/res/
You're done! Hope this helps someone!
你完成了!希望这可以帮助某人!
P.S. Please note that the original images should have at least four times the width in pixels, than the desired width in dpi (e.g. 4 (factor xxxhdpi) * 30dpi => 120px) for optimal results.
PS 请注意,原始图像的宽度应至少是像素宽度的四倍,比所需的 dpi 宽度(例如 4(因子 xxxhdpi)* 30dpi => 120px)以获得最佳效果。
回答by geng
kcoppock did a great job explaining Andorid screen densities. I just would like to add one more point regarding the original question.
kcoppock 在解释 Andorid 屏幕密度方面做得很好。我只想就原始问题再补充一点。
Android Tablet launcher icon uses one density bucket up.
Android 平板电脑启动器图标使用一个密度桶。
According to Google's developer Nick Butcher's Google+ post
根据 Google 的开发者 Nick Butcher 的 Google+ 帖子
The gorgeous screen on the Nexus 10 falls into the XHDPI density bucket. On tablets, Launcher uses icons from one density bucket up [0] to render them slightly larger. To ensure that your launcher icon (arguably your apps most important asset) is crisp you need to add a 144*144px icon in the drawable-xxhdpi or drawable-480dpi folder.
Nexus 10 上的华丽屏幕属于 XHDPI 密度范围。在平板电脑上,Launcher 使用从一个密度桶 [0] 中的图标将它们渲染得稍大一些。为了确保您的启动器图标(可以说是您的应用程序最重要的资产)清晰,您需要在 drawable-xxhdpi 或 drawable-480dpi 文件夹中添加一个 144*144px 的图标。
Find source here
在此处查找来源
回答by Vishal Kumar Sahu
Here is my calculations for upscaling and scaling down of images for android-
这是我对 android 图像放大和缩小的计算 -
ldpi (120 dpi, Low density screen) - 36px x 36px (0.19) (1)
ldpi(120 dpi,低密度屏幕)- 36px x 36px (0.19) (1)
mdpi (160 dpi, Medium density screen) - 48px x 48px (0.25) (1.33)
mdpi(160 dpi,中密度屏幕)- 48px x 48px (0.25) (1.33)
hdpi (240 dpi, High density screen) - 72px x 72px (0.38) (2)
hdpi(240 dpi,高密度屏幕)- 72px x 72px (0.38) (2)
xhdpi (320 dpi, Extra-high density screen) - 96px x 96px (0.5) (2.67)
xhdpi(320 dpi,超高密度屏幕)- 96px x 96px (0.5) (2.67)
xxhdpi (480 dpi, Extra-extra-high density screen) - 144px x 144px (0.75) (4)
xxhdpi(480 dpi,超高密度屏幕) - 144px x 144px (0.75) (4)
xxxhdpi (640 dpi, Extra-extra-extra-high density screen) - 192px x 192px (1.0) (5.33)
xxxhdpi(640 dpi,超高密度屏幕) - 192px x 192px (1.0) (5.33)
My short articleis helpful to create image resources using imagemagick, when there are multiple images.
当有多个图像时,我的短文有助于使用 imagemagick 创建图像资源。