Supporting Different Densities
This lesson shows you how to support different screen densities by providing different resources and using resolution-independent units of measurements.
这一课 讲解使用不同资源文件夹下不同文件夹内的图片资源支持不同密度的手机
Use Density-independent Pixels 使用dp单位
One common pitfall you must avoid when designing your layouts is using absolute pixels to define distances or sizes. Defining layout dimensions with pixels is a problem because different screens have different pixel densities, so the same number of pixels may correspond to different physical sizes on different devices. Therefore, when specifying dimensions, always use either dp
or sp
units. A dp
is a density-independent pixel that corresponds to the physical size of a pixel at 160 dpi. An sp
is the same base unit, but is scaled by the user's preferred text size (it’s a scale-independent pixel), so you should use this measurement unit when defining text size (but never for layout sizes).
在我们设计布局的时候一个常见的误区我们要避免的就是使用绝对的像素单位来定义不同view之间的间距或者大小。因为不同大小的屏幕有着不一样的密度所以使用像素单位是有问题的。就是说 同样大小的一个像素值在不同密度屏幕上的显示的大小是不一样的。所以 使用dp 和sp 是必须的。dp 就是在160dpi上一像素对应1dp (dpi解释 dpi 是dot per inch 每英寸上像素数)dp单位是与像素无关的。sp是和dp一样的基本单位 但是sp是根据用户喜好的文字大小可以缩放的(是与像素无关可缩放的单位)。所以sp单位用来定义字体大小而不能用在布局大小上。
For example, when you specify spacing between two views, use dp
rather than px
: 举例说明 使用dp作为View之间的间距 而不同sp。
When specifying text size, always use sp
: 字体单位一律使用sp。
Provide Alternative Bitmaps
Since Android runs in devices with a wide variety of screen densities, you should always provide your bitmap resources tailored to each of the generalized density buckets: low, medium, high and extra-high density. This will help you achieve good graphical quality and performance on all screen densities.
因为安卓系统运行在很多不同屏幕密度的手机上 我们应该提供 low, medium, high and extra-high 这4种不同的图片资源,android系统会根据屏幕密度从而自动选择对应的图片资源。
To generate these images, you should start with your raw resource in vector format and generate the images for each density using the following size scale:
xhdpi
: 2.0hdpi
: 1.5mdpi
: 1.0 (baseline)ldpi
: 0.75
This means that if you generate a 200x200 image for xhdpi
devices, you should generate the same resource in 150x150 for hdpi
, 100x100 for mdpi
and finally a 75x75 image for ldpi
devices.
这意思是 如果你做了一个200x200的图片 适用xhdpi的手机 那么 我们就需要制作 150x150 for hdpi
, 100x100 for mdpi
一句话就是 200x200 是适用密度为2.0,150x150 for hdpi 是密度为1.5 100x100 for
mdpi
1.0
Then, place the generated image files in the appropriate subdirectory under res/
and the system will pick the correct one automatically based on the screen density of the device your application is running on: 然后 把这些不同图片放到下面的文件夹中 程序运行时系统会自动根据实际设备的密度使用对应的图片资源。
MyProject/ res/ drawable-xhdpi/ awesomeimage.png drawable-hdpi/ awesomeimage.png drawable-mdpi/ awesomeimage.png drawable-ldpi/ awesomeimage.png
Then, any time you reference @drawable/awesomeimage
, the system selects the appropriate bitmap based on the screen's dpi.
For more tips and guidelines for creating icon assets for your application, see the . icon设计指导:
Icon Design Guidelines 地址:
设计者的新红宝书 里边的是关于设计的 包括 actionbar listView里边的子View的设计 等等 很专业。
现在引入 另外一个人的博客 讲的更加详细 请参考