Difference between dip, dp, sp, pt and px in Android, androiddip

Source: Internet
Author: User

Difference between dip, dp, sp, pt and px in Android, androiddip

1. Overview

In the past, programmers often designed computer user interfaces in pixels. For example, the image size is 80x32 pixels. The problem with this processing is that if you run the program on a new display with a dot (dpi) higher per inch, the user interface will look small. In some cases, the user interface may

<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: orientation = "vertical" android: padding = "3dip"> <! -- 0 fixed 1 can be extended --> <TableLayout android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: numColumns = "8" android: shrinkColumns = "0" android: stretchColumns = "1"> <TableRow android: layout_width = "fill_parent" android: layout_height = "wrap_content"> <TextView android: id = "@ + id/label" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Enter the User name:"/> <! -- This EditText is placed under the TextView with the label id above --> <EditText android: id = "@ + id/username" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_below = "@ id/label" android: background = "@ android: drawable/editbox_background"/> </TableRow> <TableRow android: layout_width = "wrap_content" android: layout_height = "match_parent"> </TableRow> </TableLayout> <Button android: layout_width = "800px" android: layout_height = "200px" android: text = "800px"/> <Button android: layout_width = "400dip" android: layout_height = "100dip" android: text = "400dip"/> </LinearLayout>
Package com. example. yanlei. my2; import android. support. v7.app. appCompatActivity; import android. OS. bundle; import android. util. displayMetrics; import android. util. log; import android. view. menu; import android. view. menuItem; import android. widget. textView; public class MainActivity extends AppCompatActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); TextView pTextView = (TextView) findViewById (R. id. label); // obtain the screen density (method 1) int screenWidth = getWindowManager (). getdefadisplay display (). getWidth (); // screen width (pixels, for example, 480px) int screenHeight = getWindowManager (). getdefadisplay display (). getHeight (); // screen height (pixels, for example: 800 p) String str = "getDefaultDisplay, screenWidth =" + screenWidth + "; screenHeight = "+ screenHeight +" \ n "; // obtain the screen density (method 2) DisplayMetrics dm = new DisplayMetrics (); dm = getResources (). getDisplayMetrics (); float density = dm. density; // screen density (pixel ratio: 0.75/1.0/1.5/2.0) int densityDPI = dm. densityDpi; // screen density (pixels per inch: 120/160/240/320) float xdpi = dm. xdpi; float ydpi = dm. ydpi; str = str + "DisplayMetrics, xdpi =" + xdpi + "; ydpi =" + ydpi + "\ n"; str = str + "DisplayMetrics, density = "+ density +"; densityDPI = "+ densityDPI +" \ n "; screenWidth = dm. widthPixels; // screen width (pixels, for example, 480px) screenHeight = dm. heightPixels; // screen height (pixels, for example, 800px) str = str + "DisplayMetrics screenWidth =" + screenWidth + "; screenHeight =" + screenHeight + "\ n "; // obtain the screen density (method 3) dm = new DisplayMetrics (); getWindowManager (). getdefadisplay display (). getMetrics (dm); density = dm. density; // screen density (pixel ratio: 0.75/1.0/1.5/2.0) densityDPI = dm. densityDpi; // screen density (pixels per inch: 120/160/240/320) xdpi = dm. xdpi; ydpi = dm. ydpi; str = str + "DisplayMetrics xdpi =" + xdpi + "; ydpi =" + ydpi + "\ n"; str = str + "DisplayMetrics, density = "+ density +"; densityDPI = "+ densityDPI +" \ n "; int screenWidthDip = dm. widthPixels; // screen width (dip, for example, 320dip) int screenHeightDip = dm. heightPixels; // screen width (dip, for example: 533dip) str = str + "DisplayMetrics screenWidthDip =" + screenWidthDip + "; screenHeightDip =" + screenHeightDip + "\ n "; screenWidth = (int) (dm. widthPixels * density + 0.5f); // screen width (px, for example: 480px) screenHeight = (int) (dm. heightPixels * density + 0.5f); // screen height (px, for example, 800px) str = str + "DisplayMetrics screenWidth =" + screenWidth + "; screenHeight = "+ screenHeight +" \ n "; pTextView. setText (str) ;}@ Override public boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. menu_main, menu); return true;} @ Override public boolean onOptionsItemSelected (MenuItem item) {// Handle action bar item clicks here. the action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest. xml. int id = item. getItemId (); // noinspection SimplifiableIfStatement if (id = R. id. action_settings) {return true;} return super. onOptionsItemSelected (item );}}

 

Small to difficult to see the content. Therefore, we can solve this problem by developing a program using a measurement unit unrelated to the resolution. Android Application Development supports different measurement units.

2. Meaning of measurement units

Dip: device independent pixels (device independent pixel ). different devices have different display effects, which are related to the hardware of the device. We recommend that you use this function to support WVGA, HVGA, and QVGA without pixels.

Dp: dip is the same

Px: pixels (pixels). Different devices have the same display effect. Generally, we use HVGA to represent 320x480 pixels, which is usually used.

Pt: point, a standard unit of length, 1pt = 1/72 inch, used in the printing industry, very easy to use;
Sp: scaled pixels (zoom in pixels). It is mainly used to display the best for textsize in fonts.

In (INCHES): the unit of length.
Mm (mm): the unit of length.

Related Article

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.