New UI-get cell phone screen size and resolution, screen adaptation, horizontal and vertical screen problems, ui-screen size

Source: Internet
Author: User

New UI-get cell phone screen size and resolution, screen adaptation, horizontal and vertical screen problems, ui-screen size


New UI-screen size of mobile phones and common resolutions, screen adaptation, horizontal and vertical screen problems


-- Reprinted please indicate the source: coder-pig. You are welcome to repost it. Please do not use it for commercial purposes!







The piggy Android development and exchange group has been established. You are welcome to join us. You can be a newbie, cainiao, or a great god.


After all, the power is limited. There will certainly be a lot of flaws in writing. You are welcome to point out, brainstorm, And let pig's blog post.


For more details, help more people, O (∩ _ ∩) O thank you!


Piggy Android Development Exchange Group: Piggy Android Development Exchange Group No.: 421858269


New Android UI instance Daquan Directory: http://blog.csdn.net/coder_pig/article/details/42145907





1) obtain the cell phone screen size and screen density:

First, let's talk about the cell phone screen size we can get through programming. It's just a resolution !!! Real screen size,


In this way, you can either test it yourself or check it online! The size obtained by programming is only the resolution. Remember !!!


The following provides a tool class that provides three methods for obtaining the resolution and screen density of mobile phones. method 1 is outdated and is not recommended.


Used!


Go up and down first:




Tool class:


GetScreenParameter. java




Package com. Jay. Example. Getscreendemo;


The import android. App. The Activity;

The import android. The content. The Context;

The import android. Util. DisplayMetrics;

The import android. Util. Log;

The import android. View. The Display;


Public class GetScreenParameter {


// method 1 is out of date, although it can be used, it is not recommended

Public static void getResolution1(Context mContext) {

Display mDisplay = ((Activity) mContext). GetWindowManager ()

GetDefaultDisplay ();

Int. W = mDisplay getWidth ();

Int H = mDisplay. GetHeight ();

Log. I ("getResolution1", "Width = "+ W + "px");

Log. I ("getResolution1", "Height = "+ H + "px");

}



// method 2 is to get the screen size through getWindowManager

Public static void getResolution2(Context mContext) {

DisplayMetrics mDisplayMetrics = new DisplayMetrics();

((Activity) mContext). GetWindowManager (.) getDefaultDisplay ()

GetMetrics (mDisplayMetrics);

Int W = mDisplayMetrics. WidthPixels;

Int H = mDisplayMetrics. HeightPixels;

Float density = mDisplayMetrics. Density; // screen density (0.75/1.0/1.5)

Int densityDpi = mDisplayMetrics. DensityDpi; // it's just screen density *160, screen density DPI (120/160/240)

Log. I ("getResolution2", "Width = "+ W + "px");

Log. I ("getResolution2", "Height = "+ H + "px");

Log. I ("getResolution2", "density = "+ density);

Log. I ("getResolution2", "densityDpi = "+ densityDpi");

}



// method 3 gets the screen size through getResources

Public static void getResolution3(Context mContext) {

DisplayMetrics mDisplayMetrics = new DisplayMetrics();

MDisplayMetrics = mContext. GetResources (). GetDisplayMetrics ();

Int W = mDisplayMetrics. WidthPixels;

Int H = mDisplayMetrics. HeightPixels;

Float density = mDisplayMetrics. Density; // screen density (0.75/1.0/1.5)

Int densityDpi = mDisplayMetrics. DensityDpi; // it's just screen density *160, screen density DPI (120/160/240)

Log. I ("getResolution3", "Width = "+ W + "px");

Log. I ("getResolution3", "Height = "+ H + "px");

Log. I ("getResolution3", "density = "+ density);

Log. I ("getResolution3", "densityDpi = "+ densityDpi");

}


}


MainActivity. java






package com.jay.example.getscreendemo;


import android.app.Activity;

import android.os.Bundle;



public class MainActivity extends Activity {


@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

GetScreenParameter.getResolution1(MainActivity.this);

GetScreenParameter.getResolution2(MainActivity.this);

GetScreenParameter.getResolution3(MainActivity.this);

}

}



The usage is very simple. It is only printed to logcat. You can modify it as needed. Note that:

For small-screen mobile phones with low screen density, such as 240*320 mobile phones, the calculated size may be 320*427, and there are solutions on the Internet,


The reason is: If multi-resolution is not set, the Android system will convert the low density (120) of x320 to medium density.


(160) the corresponding size. To obtain the correct physical size, you only need to add the following content to the AndroidManifest. xml file:





<supports-screens

android:smallScreens="true"

android:normalScreens="true"

android:largeScreens="true"

android:resizeable="true"

android:anyDensity="true"/>






You can solve this problem!







2) Resolution of mobile phones:

I believe that after creating the project, you can see several types of drawable under the res directory.



At this time, you may have some questions. What are these folders?Why are there so many?


A: All these folders are for drawable resources. You can think of them as for images.


For mobile phones with different screen density, this is because the same image is


Different results may appear under mobile phones of different screen density, which is obviously contrary to our original intention; therefore, Android


The image resources in the corresponding folder are loaded according to the dpi of the mobile phone!








The following xdpi will load the image resources in the corresponding folder!


Of course, this is not absolute. For example, if we leave all the images in drawable-hdpi


The image resources under the ldpi folder should be loaded, but not under the ldpi, so the images under the hdpi will be loaded!


In addition, there is another situation: for example, hdpi, which exists in the mdpi directory and does not exist in ldpi, the resources in the mdpi will be loaded!


The principle is to use the nearest density level!


Ps: If you want to disable Android from loading resources in different folders without following the screen density, you only need


Add the android: anyDensity = "false" field to the file!








3) screen adaptation problems:

I believe that screen adaptation is a headache for everyone. Here, let's talk about what you should pay attention to during interface development!


① Use the weight (weight) attribute of RelativeLayout + LinearLayout to compile the interface!




② Use match_parent and warp_content. If the size of the dead control must be written, use dp instead of px)


Sp is used for text size. If textSize is not set, the default size is 14sp!


(If the text size is greater than 15sp, you can also use dp. You like it !)




③ Images of different sizes are provided for mobile phones of different screen density. The ratio from low to high is as follows;


For example, if the image in ldpi is 36*36, the mdpi is 48*48. Similarly, the hdpi is 72*72, and the xhdpi is 96*96.




④ To use different la s for different mobile phones, you only need to create a layout folder named:


Layout-480x320 such format, and then write the corresponding layout file, layout-320x240 so that the Android system will


Different cell phone resolutions are loaded with different la s! In addition, you must add this item in AndroidManifest. xml:





<supports-screens

android:smallScreens="true"

android:normalScreens="true"

android:largeScreens="true"

android:xlargeScreens="true"

android:anyDensity="true" />


Otherwise it will be ineffective!





⑤ For partial stretch pictures, such as TextView in the most common chatting day, if the text is too long, it may cause tensile deformation.


In these cases, we should use the 9-patch tool to process the image!


Ps: for the use of the 9patch tool, you can use the 9 patch (9 sister ).








4. Load different la s on the horizontal and vertical screens:

Layout-port: landscape layout-land: Landscape


When the mobile phone is loaded, it will judge the horizontal and vertical screen, and then load different la s











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.