Android Official Development Document Training Series Course Chinese version: Various screens support different screen densities

Source: Internet
Author: User

Original address: http://android.xsoftlab.net/training/multiscreen/screendensities.html

This lesson will learn how to support different screen densities with different resources and independent units of measurement.

Use density-independent pixel units

You have to avoid a trap: When designing layouts, absolute pixel units are used to define distances and dimensions. It is a problem to define layout dimensions through the like, because different screens have different pixel densities, so using the same pixel length on different devices can cause different physical dimensions. Therefore, you should always use a DP or SP when specifying the size. DP is a density-independent pixel unit that coincides with the physical size of the pixel at 160dpi (that is, the 160dpi screen DP is exactly equal to the SP). An SP is also a base unit, but it is a scalable unit of text dimensions, so you should use that unit when defining text dimensions (never use that unit on a layout).

For example, if you specify a gap between two view, you should use DP instead of PX:

<Button android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="@string/clickme"    android:layout_marginTop="20dp" />

Always use the SP when specifying the text size:

<TextView android:layout_width="match_parent"    android:layout_height="wrap_content"    android:textSize="20sp" />
Provide alternate bitmaps

Because Android runs on a wide variety of devices, it should provide the appropriate bitmap resources for a wide range of densities: low, medium, high, and extra-high densities. This helps to improve the physical effects and performance of all screen densities.

In order to produce these bitmaps, multiple densities of images should be generated from native resources:

    • xhdpi:2.0
    • hdpi:1.5
    • mdpi:1.0 (Baseline)
    • ldpi:0.75

This means that if a 200x200 image is generated for a xhdpi device, the hdpi should be provided with a size of 150x150,mdpi 100x100,ldpi for 75x75.

These files are then placed in the appropriate directory and, at run time, the correct bitmap is automatically selected based on the current screen density:

MyProject/  res/    drawable-xhdpi/        awesomeimage.png    drawable-hdpi/        awesomeimage.png    drawable-mdpi/        awesomeimage.png    drawable-ldpi/        awesomeimage.png

Next, when referencing @drawable/awesomeimage at any time, the system selects the appropriate bitmap image based on the DPI of the screen.

Finally, put the boot icon into the mipmap/folder:

res/...    mipmap-ldpi/...        finished_launcher_asset.png    mipmap-mdpi/...        finished_launcher_asset.png    mipmap-hdpi/...        finished_launcher_asset.png    mipmap-xhdpi/...        finished_launcher_asset.png    mipmap-xxhdpi/...        finished_launcher_asset.png    mipmap-xxxhdpi/...        finished_launcher_asset.png

Note: you should put all the boot icons into the res/mipmap-[density]/folder instead of the drawable/folder, which ensures that the desktop app uses the best resolution icons. For more information, see Managing Projects Overview.

For more tips and guidelines for creating icon resources, see icon Design guidelines.

Android Official Development Document Training Series Course Chinese version: Various screens support different screen densities

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.