关于Android映像和资产大小

时间:2022-11-21 16:14:00

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!

谢谢!

4 个解决方案

#1


355  

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显示上的1px等于1dip。资产规模比例为:

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,除非你是专门为谷歌电视或最初的Nexus 7开发的——但是即使谷歌也建议使用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分辨率的图像,那么您应该从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、draw- hdpi等),那么它们在任何设备上的显示大小都应该大致相同。

For reference, the pixel densities for these are:

作为参考,这些像素的密度为:

ldpi  | mdpi  | tvdpi  | hdpi  | xhdpi  | xxhdpi  | xxxhdpi
120   | 160   | 213    | 240   | 320    | 480     | 640

#2


22  

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可绘制的-* -文件夹中!

Create a shell script and paste the following code:

创建shell脚本并粘贴以下代码:

createAndroidImages.sh

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

请注意,原始图像的宽度应该至少是dpi中所要求的宽度的四倍(例如,4 (factor xxxhdpi) * 30dpi => 120px),以获得最佳结果。

#3


4  

kcoppock did a great job explaining Andorid screen densities. I just would like to add one more point regarding the original question.

科波克在解释Andorid屏幕密度方面做得很好。我只是想再补充一点关于最初的问题。

Android Tablet launcher icon uses one density bucket up.

Android平板启动图标使用一个密度桶。

According to Google's developer Nick Butcher's Google+ post

谷歌的开发者Nick Butcher的谷歌+ post说

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密度桶。在平板电脑上,启动器使用一个密度桶上的图标来使它们稍微大一点。要确保你的启动图标(可以说是你的应用程序最重要的资产)是清晰的,你需要在draw- xxhdpi或draw- 480dpi文件夹中添加一个144*144px图标。

Find source here

在这里找到来源

#4


1  

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 article is helpful to create image resources using imagemagick, when there are multiple images.

当有多个图像时,我的短文有助于使用imagemagick创建图像资源。

#1


355  

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显示上的1px等于1dip。资产规模比例为:

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,除非你是专门为谷歌电视或最初的Nexus 7开发的——但是即使谷歌也建议使用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分辨率的图像,那么您应该从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、draw- hdpi等),那么它们在任何设备上的显示大小都应该大致相同。

For reference, the pixel densities for these are:

作为参考,这些像素的密度为:

ldpi  | mdpi  | tvdpi  | hdpi  | xhdpi  | xxhdpi  | xxxhdpi
120   | 160   | 213    | 240   | 320    | 480     | 640

#2


22  

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可绘制的-* -文件夹中!

Create a shell script and paste the following code:

创建shell脚本并粘贴以下代码:

createAndroidImages.sh

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

请注意,原始图像的宽度应该至少是dpi中所要求的宽度的四倍(例如,4 (factor xxxhdpi) * 30dpi => 120px),以获得最佳结果。

#3


4  

kcoppock did a great job explaining Andorid screen densities. I just would like to add one more point regarding the original question.

科波克在解释Andorid屏幕密度方面做得很好。我只是想再补充一点关于最初的问题。

Android Tablet launcher icon uses one density bucket up.

Android平板启动图标使用一个密度桶。

According to Google's developer Nick Butcher's Google+ post

谷歌的开发者Nick Butcher的谷歌+ post说

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密度桶。在平板电脑上,启动器使用一个密度桶上的图标来使它们稍微大一点。要确保你的启动图标(可以说是你的应用程序最重要的资产)是清晰的,你需要在draw- xxhdpi或draw- 480dpi文件夹中添加一个144*144px图标。

Find source here

在这里找到来源

#4


1  

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 article is helpful to create image resources using imagemagick, when there are multiple images.

当有多个图像时,我的短文有助于使用imagemagick创建图像资源。