How to adapt to the Android fragmented screen

Source: Internet
Author: User

There are also many types of screen sizes and densities for devices in fragmented Android devices, so Android helps us define

Four types of size: Small,normal,large,xlarge

Four screen densities: ldpi (120dpi), MDPI (160dpi), hdpi (240dpi), xhdpi (320dpi)

(The screen density can be understood as the number of pixels in the area of the unit area, the more pixels, the larger the screen density, the more clear display)


We typically use DP to define the length and use the SP to define the text size.

Why use DP to define length? The reason is simple, when the length of the DP definition is not used, the UI elements appear large on low-density screens or are displayed on a high-density screen, such as

650) this.width=650; "src=" Http://developer.android.com/images/screens_support/density-test-bad.png "style=" Border:none; "/>

But the effect is much better when you define the length with DP, such as

650) this.width=650; "src=" Http://developer.android.com/images/screens_support/density-test-good.png "style=" Border:none; "/>



In the actual work, the UI's small partners are identified by the PX length, so we need to convert PX to DP, using the following formula to convert:

PX = DP * (dpi/160);

(LDPI (120dpi), MDPI (160dpi), hdpi (240dpi), xhdpi (320dpi))

I see code like the following on the Web:

    1. // the gesture threshold expressed in  dp  private static final float GESTURE_THRESHOLD_DP = 16.0f;     // get the screen ' S density scale  final float  scale = 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. 

Using the above code to convert the DP to PX is because of a specific scene, for example, when the user's finger moved more than 16px is considered a scroll or fill gesture, but there is a problem, the higher the screen density, the shorter the moving distance, so on the xhdpi screen, This scroll or fill action is too sensitive for the user!

We should also pay attention to similar problems when we use pictures.

Here I first introduce two definitions

1.pre-scaling of resources: This is very simple, our project has drawable-ldpi,drawable-mdpi,drawable-hdpi, drawable-xhdpi These folders, there's a picture of pre-scaling in them.


2.auto-scaling of pixel dimensions and coordinates: When manifest "false" is set in the android:anydensity= file, Or if you set the bitmap inscaled to False by encoding, the system automatically converts the absolute px coordinates and the length when it is displayed, so that the UI elements can be displayed correctly in the size that should be displayed in the MDPI.

The next step is to describe how a bitmap object was converted during runtime.


When we create a bitmap object, the system first assumes that the bitmap object was designed on MDPI, and by default the system auto-scaling bitmap when it is displayed. If bitmap does not set the density property, the system uses auto-scaling to process it, and the result of auto-scaling processing is the same as if you did not set the resource to the appropriate resources at different DPI.


If you set the pre-scaling set to False, then the system will be displayed when the use of auto-scaling, in other words, this is to use auto-scaling to replace the pre-scaling, This wastes more CPU resources but consumes less storage space.


In addition, we can use viewconfiguration to get the public length, speed and time of the system, for example

<span class= "KWD"  style= "color: #0088;" >private</span><span class= "PLN"  style= "color: #000000;" > </span><span class= "KWD"  style= "color: #0088;" >static</span><span class= "PLN"  style= "color: #000000;" > </span><span class= "KWD"  style= "color: #0088;" >final</span><span class= "PLN"  style= "color: #000000;" > </span><span class= "KWD"  style= "color: #0088;" >int</span><span class= "PLN"  style= "color: #000000;" > gesture_threshold_dp </span><span class= "pun"  style= "color: #66660;" >=</span><span class= "PLN"  style= "color: #000000;" > </span><span class= "Typ"  style= "color: #66066;" >viewconfiguration</span><span class= "pun"  style= "color: #66660;" >.</span><span class= "KWD"  style= "color: #0088;" >get</span><span class= "pun"  style= "color: #66660;" > (</span><span class= "PLN"  style= "color: #000000;" >mycontext</span><span class= "pun"  style= "color: #66660;" >). </span><span class= "PLN"  style= "color: #000000;" >getscaledtouchslop</span><span class= "pun"  style= "color: #66660;" > ();</span>

The method that starts with getscaled in viewconfiguration ensures that the PX value returned is adapted to the current screen.

Conversion and differentiation of DP, SP, px in Android.

APK in the resource bundle

When the screen is density=240, resources using the hdpi tag when the screen density=160, use the resources of the MDPI tag when the screen density=120, using the ldpi tag resource. Resources that are not tagged are shared in a variety of resolution scenarios. Use unit dip as much as possible in layout, use less px

DP and PX Conversion formula:

pixs =dips * (densitydpi/160) . 

Dips= (pixs*160)/densitydpi

/** * According to the resolution of the phone from the DP unit to be PX (pixels) */public static int dip2px (context context, float Dpvalue) {final float scale = con  Text.getresources (). Getdisplaymetrics (). density; return (int) (Dpvalue * scale + 0.5f); }

How to convert the DP to PX:

public static int dip2px (context context, float Dipvalue) {final float scale = context.getresources (). Getdisplaymetrics (   ). Density; return (int) (Dipvalue * scale +0.5f); }public static int Px2dip (context context, float Pxvalue) {final float scale = Context.getresource (). Getdisplaymetrics ()   . density; return (int) (Pxvalue/scale +0.5f); }


Noun

Explain

Px

(Pixel pixels)

Different devices show the same effect. The "Same" here means that the number of pixels does not change, such as specifying a UI length of 100px, regardless of the resolution of the UI length is 100px. This is why the UI is magnified and distorted on small-resolution devices, and is scaled down at large resolutions.

screen size

(screen size)

resolution

(resolution)

refers to the number of pixels in the vertical and horizontal direction of the phone's screen. For example, the resolution is 480*320, which means that the device has a vertical orientation of 480 pixels and a horizontal 320 pixel point.

dpi

< Span style= "font-size:15px;" > (dots per inch

  pixel density)

refers to the number of pixels per inch. such as 160dpi refers to the mobile phone horizontal or vertical direction of 160 pixels per inch distance. Assuming a device resolution of 320*240, the screen is 2 inches wide and 1.5 inches long, dpi=320/2=240/1.5=160

< Span style= "FONT-SIZE:12PX;" > Note: This value corresponds to the value of the property densitydpi in the Displaymetrics class

density

(density)

refers to the number of pixels per square inch.

density=resolution/ Screen size

Note: The value of the property density in the Displaymetrics class is dpi/160, which can be used to convert PX to dip

dip

< Span style= "font-size:15px;" > (device-independent pixel, device independent pixels)

with DP, can be used as a unit of length, different devices have different display effect , this is related to the device hardware, generally we support WVGA, HVGA and QVGA recommend the use of this , and does not depend on pixels. The corresponding formula for dip and specific pixel values is the dip value  = device density/160* pixel value , which can be seen on devices with a dpi (pixel density) of 160dpi 1px=1dip

Sp

(Scaledpixels

Enlarge pixels)

Mainly used for font display (best for TEXTSIZE). According to Google's suggestion, TextView's font size is best to use SP to do units, and view the source of TextView know that Android by default using the SP as the font size units.


This article is from "Little book" blog, please make sure to keep this source http://8988940.blog.51cto.com/8978940/1561162

How to adapt to Android's fragmented screen

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.