Android ApiDemos example resolution (63): Graphics-& gt; Density

Source: Internet
Author: User

There are many types of mobile phones that support the Android system. The screen size and resolution of each mobile phone may be different (the screen pixel density). The Android system provides methods to have different sizes, the layout and size of the display page are automatically adjusted on the mobile phone with the pixel density of the screen, at the same time, Android also provides related APIs that allow developers to control the UI based on different screen sizes and display density so that applications can display well on the screens of devices with different configurations.

Although the Android system provides the screen adaptive function to solve most UI display problems on different screens, the application still needs to do some work that supports different screen configurations, in this way, end users can enjoy a good user experience with different screen configurations.

Before introducing this example, explain some terms:

Screen Size: the physical Size of a device Screen (diagonal line length), such as a 3-inch Screen or a 4-inch Screen. For simplicity, Android divides the screen size into four categories: small (small size), normal (normal size), large (large size), and extra large (super large size ).

Screen Density: number of pixels displayed per unit. Dpi is usually used (points/inches ). For example, the number of pixels displayed on a "high density" screen per unit is greater than that on a "low density" screen. For simplicity, Android also divides the screen density (hereinafter referred to as the screen density or density) into low (low density), medium (moderate density), high (high density ), extra high (ultra-high density ).

Orientation: from the user's point of view, the display screen can be landscape or portrait, and you can change the portrait or landscape of the screen when the application is running.

Screen Resolution: The total number of pixels that can be displayed on the screen. In terms of supporting multiple screen configurations, applications usually focus on the screen size and density instead of operating the screen resolution.

Density-independent pixel (Density-independent pixel-dp): indicates that Layout is a device-independent virtual pixel unit. Dp is defined as the size of one pixel on the 160 dpi screen. 160 dpi is regarded as a "moderate density" screen by the Android system. When dp is used, Android automatically scales up or down the controls in Layout based on the actual screen size and density when the application is running. The conversion from a dp value to an actual pixel value is simple: px = dp * (dpi/160 ).

Screen range supported by Android: the Android system supports multiple screen sizes and screen density from 1.6. As mentioned above, Android divides the screen size and screen definition into four categories, the first generation of Android phone T-Mobile G1 screen (HVGA) is used as the benchmark. Displays a rough classification of screen size and screen definition:

Applications can support different screen configurations by providing alternative resources. A typical practice is to provide different Layout and image resources for different screen sizes or screen density. When the Andorid system is running, you can select the appropriate Layout and resource Display User Interface Based on the actual configuration on the current screen, generally, you do not need to provide different Layout and resources for the combination of screen size and screen density. The Android system will be very "intelligent" to deal with the display of Lower Bounds without screen configuration.

Density independence: if an application has "screen Density independence", the actual physical size remains unchanged for different screen display Density values in the UI components of the application.

It is very important to keep the actual physical size of the interface. Without a figure, the physical size of a button on the "low density" screen is larger than that on the "high density" screen, the following two figures show the effects of "no screen density independence" and "screen density independence:



Android uses the following two methods to help applications achieve "density independence ":

The system automatically scales the Layout component based on the current screen density in the unit of dp to adapt to the screen.
The system scales based on the current screen density of Drawable resources.
Therefore, in most cases, you can specify the size of Components in Layout by using dp, and use the "wrap_content" attribute correctly to achieve "screen density independence ". However, for bitmap resources, automatic scaling may sometimes blur the enlarged or reduced image. This requires the application to provide different resources for different screen density configurations: provides high-definition images for high-density screens.

How to support multi-screen Configuration

The Android system automatically supports screen adaptation, but sometimes the application needs to provide screen configuration-related resources for a better user experience:

Layout is provided for different screen sizes.
Improves bitmap image resources for different screen density.
When the Android system is running, select the appropriate Layout and resource based on the current screen configuration. If no alternative resource is provided for the screen configuration, select the default resource for scaling.

The following table lists possible combinations of Android screen configurations:

Let's take a look at the Resource Directory defined in apidemos:

 

For example, for drawable, drawable is the default drawable resource without any suffix. drawable-hdpi is the drawable resource used for high-density screens, and drawable-ldpi is the drawable resource used for low-density screens.

Similarly, values defines resources used in different configurations.

Bitmapresource definition (.png .. jpg ,. gif, .9.png) In principle, the ratio of 3: 4: 6: 8 is used to provide image resources for four different screen density types, for example, for a resource with a pixel size of 48x48 under a "medium density,

Low-density: 36X36
Medium-density: 48X48
High-density: 72X72
Extra high-density: 96X96


Some additional considerations about Density

This section is not important to most applications, unless your applications have problems with different screen configurations.

To better understand how to support different screen density configurations when drawing images for applications, you should know the following support provided by the Android system:

Pre-scaling of resources (such as bitmap drawable resources)

The Android system selects resources related to device configuration based on the current device screen configuration without scaling (for example, selecting drawable resources under the Drawable-hdpi directory for high-density screens ). If the resource corresponding to the current screen configuration is not found, Android uses the default Resource (the resource under the drawable directory) to zoom in or out to adapt to the screen. Android applications think that the default resources (such as drawable and other directories without suffixes) correspond to the resources of the baseline screen configuration (mdpi. Pre-scaling means that the Android system automatically scales Bitmap for the current screen density to adapt to the screen process.

If you need to know the size of these pre-scaled resources, the Android system returns the scaled size, for example, an image of 50X50 pixels corresponding to mdpi will be enlarged to 75X75 pixels on the hdpi screen. In this case, the returned value of the image size query is 75X75. if you do not want Android to perform pre-scaling on some resources, you can put these resources in a directory with the nodpi suffix, such as/res/drawable-nodpi.

The no-dpi resource drawable in the Density example is shown as follows:

 

Automatic scaling of pixel size and coordinates

An application can also disable pre-scaling by setting android: anyDensity to "false" in the Manifest file or setting isScaled of bitmap to false in code. In this case, the Android system automatically scales the pixel values and coordinates in pixels. As a result, devices of different screen sizes can still display physical sizes similar to those of the baseline screen. If you need to return the Bitmap size, the system returns the scaled pixel value instead of the actual physical pixel value.

In most cases, "pre-scaling should not be disabled", but multiple screen configurations are supported by providing screen configuration resources.

Scaling Bitmap during runtime

If an application dynamically creates a Bitmap in the memory, the Android system determines that the Bitmap corresponds to the baseline screen configuration (moderate density ). By default, if the Bitmap is displayed on the screen, the Android system automatically scales to adapt to the current screen. You can use setDensity to set the display density for Bitmap.

If BitmapFactory is used to create a Bitmap object, you can use BitmapFactory. Options to define Bitmap attributes. If inScaled is set to false, the pre-sacling is disabled.

For the Density example, pre-scale and auto-scale are used to show the effects displayed on high-Density screens in low (120), medium (160), and high (240) images:

 

Note that the Title: Density: High in this example may have different running results:

[Java]
This. setTitle (R. string. density_title );

This. setTitle (R. string. density_title); the title resource is R. string. density_title, which is defined differently in values, values-large, and values-small. During application running, the Android system selects appropriate resources based on the current screen configuration, you can create several Simulators of different sizes to see if the results are different. You can also understand the resource selection mechanism of this example and Android.

The following table shows the combinations of different screen sizes and density supported by the simulator:

 


Author: mapdigit

 

 

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.