Supporting Different Densities 支持各种屏幕密度
2013-04-17 09:32
246 查看
Use Density-independent Pixels
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 pixelsmay correspond to different physical sizes on different devices. Therefore, when specifying dimensions, always use either
dpor
spunits. A
dpis a density-independent pixel that corresponds to the physical size of a pixel at 160 dpi. An
spis 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).
For example, when you specify spacing between two views, use
dprather than
px:
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/clickme" android:layout_marginTop="20dp" />
When specifying text size, always use
sp:
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp" />
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 graphicalquality and performance on all screen densities.
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.0
hdpi: 1.5
mdpi: 1.0 (baseline)
ldpi: 0.75
This means that if you generate a 200x200 image for
xhdpidevices, you should generate the same resource in 150x150 for
hdpi, 100x100 for
mdpiand finally a 75x75 image for
ldpidevices.
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.
相关文章推荐
- 支持不同屏幕密度——Supporting Different Densities
- 【官方】Android支持不同的密度或分辨率(Supporting Different Densities)
- 支持不同密度(Supporting Different Densities)
- Supporting Multiple Screens 翻译 支持各种屏幕(上)
- 支持不同屏幕尺寸——Supporting Different Screen Sizes
- 【官方】Android提供的支持不同屏幕大小的全部方法(Supporting Different Screen Sizes)
- 支持不同屏幕(Supporting Different Screens)
- 支持各种屏幕密度
- Android 针对多种屏幕进行设计 (二)支持各种屏幕密度
- Android应用开发之(支持各种密度的屏幕)
- 支持不同屏幕——Supporting Different Screens
- Supporting Multiple Screens(支持Android各种屏幕尺寸)
- Android屏幕适配之支持各种屏幕密度(三)
- Android 支持不同的屏幕 Supporting Different Screens
- Supporting Different Screen Sizes支持不同的屏幕尺寸
- 解决方案-支持各种屏幕密度
- 支持各种屏幕密度
- Android屏幕适配解析 - 详解像素,设备独立像素,归一化密度,精确密度及各种资源对应的尺寸密度分辨率适配问题
- iOS开发 强制竖屏。系统KVO 强制竖屏—>适用于支持各种方向屏幕启动时,竖屏展示广告
- 设定 <uses-sdk>以支持各种尺寸屏幕