您的位置:首页 > 其它

Supporting Multiple Screens

2015-12-25 22:52 330 查看
Android runs on a variety of devices that offer different screen sizes and densities. For applications, the Android system provides a consistent development environment across
devices and handles most of the work to adjust each application's user interface to the screen on which it is displayed.

Note: The information
in this document assumes that your application is designed for Android 1.6 (API Level 4) or higher. If your application supports Android 1.5 or lower, please first readStrategies
for Android 1.5.

The conversion of dp units to screen pixels is simple:
px
= dp * (dpi / 160)
. For example, on a 240 dpi screen, 1 dp equals 1.5 physical pixels. You should always use dp units when defining your application's UI, to ensure
proper display of your UI on screens with different densities.

>Starting with Android 1.6 (API Level 4), Android provides support for multiple screen sizes and densities,
reflecting the many different screen configurations that a device may have. You can use features of the Android system to optimize your application's user interface for each screen configuration and ensure that your application not only renders properly, but
provides the best user experience possible on each screen.

>Note: Beginning
with Android 3.2 (API level 13), these size groups are deprecated in favor of a new technique for managing screen sizes based on the available screen width. If you're developing for Android 3.2 and greater, see Declaring
Tablet Layouts for Android 3.2 for more information.

>These
minimum sizes are in "dp" units—the same units you should use when defining your layouts—which allows the system to avoid worrying about changes in screen density.

xlarge screens are at least 960dp x 720dp
large screens are at least 640dp x 480dp
normal screens are at least 470dp x 320dp
small screens are at least 426dp x 320dp

Maintaining density
independence is important because, without it, a UI element (such as a button) appears physically larger on a low-density screen and smaller on a high-density screen. Such density-related size changes can cause problems in your application layout and usability.

The
configuration qualifiers you can use to provide size-specific resources are
small
,
normal
,
large
,
and
xlarge
.

>By
default, Android scales your bitmap drawables (
.png
,
.jpg
,
and
.gif
files) and Nine-Patch drawables (
.9.png
files)
so that they render at the appropriate physical size on each device.

The configuration
qualifiers (described in detail below) that you can use for density-specific resources are
ldpi
(low),
mdpi
(medium),
hdpi
(high),
xhdpi
extra-high),
xxhdpi
(extra-extra-high),
and
xxxhdpi
(extra-extra-extra-high). For example, bitmaps for high-density screens should go in
drawable-hdpi/
.

Note: If
you're not familiar with configuration qualifiers and how the system uses them to apply alternative resources, read Providing
Alternative Resources for more information.

> the following application resource directories provide different layout designs for different screen sizes and different drawables. Use the
mipmap/
folders for launcher icons.
res/layout/my_layout.xml              // layout for normal screen size ("default")
res/layout-large/my_layout.xml        // layout for large screen size
res/layout-xlarge/my_layout.xml       // layout for extra-large screen size
res/layout-xlarge-land/my_layout.xml  // layout for extra-large in landscape orientation

res/drawable-mdpi/graphic.png         // bitmap for medium-density
res/drawable-hdpi/graphic.png         // bitmap for high-density
res/drawable-xhdpi/graphic.png        // bitmap for extra-high-density
res/drawable-xxhdpi/graphic.png       // bitmap for extra-extra-high-density

res/mipmap-mdpi/my_icon.png         // launcher icon for medium-density
res/mipmap-hdpi/my_icon.png         // launcher icon for high-density
res/mipmap-xhdpi/my_icon.png        // launcher icon for extra-high-density
res/mipmap-xxhdpi/my_icon.png       // launcher icon for extra-extra-high-density
res/mipmap-xxxhdpi/my_icon.png      // launcher icon for extra-extra-extra-high-density


To summarize, you should be sure that your application layout:

Fits on small screens (so users can actually use your application)
Is optimized for bigger screens to take advantage of the additional screen space
Is optimized for both landscape and portrait orientations

A Nine-Patch file is basically a PNG file in which you specify two-dimensional regions that are stretchable. When the system needs to scale the view in which the bitmap is used, the
system stretches the Nine-Patch bitmap, but stretches only the specified regions.

Note: You only need to provide density-specific drawables for bitmap files (
.png
,
.jpg
,
or
.gif
) and Nine-Patch files (
.9.png
). If
you use XML files to define shapes, colors, or other drawable
resources, you should put one copy in the default drawable directory (
drawable/
).

> To help
you target some of your designs for different types of devices, here are some numbers for typical screen widths:

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

For other cases in which you want to further customize your UI to differentiate between sizes such as 7” and 10” tablets, you can define additional smallest width layouts:
res/layout/main_activity.xml           # For handsets (smaller than 600dp available width)
res/layout-sw600dp/main_activity.xml   # For 7” tablets (600dp wide and bigger)
res/layout-sw720dp/main_activity.xml   # For 10” tablets (720dp wide and bigger)

<supports-screens android:requiresSmallestWidthDp="600" />

Caution: The Android system does not pay attention to this attribute, so it does not affect how
your application behaves at runtime. Instead, it is used to enable filtering for your application on services such as Google Play. However, Google Play currently does not
support this attribute for filtering (on Android 3.2), so you should continue using the other size attributes if your application does not support small screens.

o fix this issue, the gesture threshold must be expressed in code in
dp
and then converted to actual pixels. For example:
// The gesture threshold expressed in dp
private static final float GESTURE_THRESHOLD_DP = 16.0f;

// Get the screen's density scale
final float scale = [code]getResources()
.
getDisplayMetrics()
.density;
// Convert the dps to pixels, based on density scale
mGestureThreshold = (int) (GESTURE_THRESHOLD_DP * scale + 0.5f);

// Use mGestureThreshold as a distance in pixels...[/code]
>How to Test Your Application on Multiple Screens
To launch the Android SDK Manager, execute the
SDK Manager.exe
from
your Android SDK directory (on Windows only) or execute
android
from the
<sdk>/tools/
directory
(on all platforms).

We also recommend that you test your application in an emulator that is set up to run at a physical
size that closely matches an actual device. This makes it a lot easier to compare the results at various sizes and densities. To do so you need to know the approximate density, in dpi, of your computer monitor (for instance, a 30" Dell monitor has a density
of about 96 dpi). When you launch an AVD from the AVD Manager, you can specify the screen size for the emulator and your monitor dpi in the Launch Options, as shown in figure 7.

If you would like to test your application on a screen that uses a resolution or density not supported
by the built-in skins, you can create an AVD that uses a custom resolution or density. When creating the AVD from the AVD Manager, specify the Resolution, instead of selecting a Built-in Skin.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: