zoukankan      html  css  js  c++  java
  • Android中dip、dp、sp、pt和px的区别

    1、概述

            过去,程序员通常以像素为单位设计计算机用户界面。例如:图片大小为80×32像素。这样处理的问题在于,如果在一个每英寸点数(dpi)更高的新显示器上运行该程序,则用户界面会显得很小。在有些情况下,用户界面可能会

    <?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固定1可以扩展-->
    
        <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="请输入用户名:" />
                <!-- 这个EditText放置在上边id为label的TextView的下边 -->
                <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);
    
            // 获取屏幕密度(方法1)
            int screenWidth  = getWindowManager().getDefaultDisplay().getWidth();       // 屏幕宽(像素,如:480px)
            int screenHeight = getWindowManager().getDefaultDisplay().getHeight();      // 屏幕高(像素,如:800p)
    
            String str=  "  getDefaultDisplay ,screenWidth=" + screenWidth + "; screenHeight=" + screenHeight+"
    ";
    
            // 获取屏幕密度(方法2)
            DisplayMetrics dm = new DisplayMetrics();
            dm = getResources().getDisplayMetrics();
    
            float density  = dm.density;        // 屏幕密度(像素比例:0.75/1.0/1.5/2.0)
            int densityDPI = dm.densityDpi;     // 屏幕密度(每寸像素:120/160/240/320)
            float xdpi = dm.xdpi;
            float ydpi = dm.ydpi;
    
            str=str + "  DisplayMetrics, xdpi=" + xdpi + "; ydpi=" + ydpi+"
    ";
            str=str + "  DisplayMetrics, density=" + density + "; densityDPI=" + densityDPI+"
    ";
    
            screenWidth  = dm.widthPixels;      // 屏幕宽(像素,如:480px)
            screenHeight = dm.heightPixels;     // 屏幕高(像素,如:800px)
    
            str=str  + "  DisplayMetrics screenWidth=" + screenWidth + "; screenHeight=" + screenHeight+"
    ";
    
            // 获取屏幕密度(方法3)
            dm = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(dm);
    
            density  = dm.density;      // 屏幕密度(像素比例:0.75/1.0/1.5/2.0)
            densityDPI = dm.densityDpi;     // 屏幕密度(每寸像素:120/160/240/320)
            xdpi = dm.xdpi;
            ydpi = dm.ydpi;
    
            str=str +  "  DisplayMetrics xdpi=" + xdpi + "; ydpi=" + ydpi+"
    ";
            str=str  + "  DisplayMetrics, density=" + density + "; densityDPI=" + densityDPI+"
    ";
    
            int screenWidthDip = dm.widthPixels;        // 屏幕宽(dip,如:320dip)
            int screenHeightDip = dm.heightPixels;      // 屏幕宽(dip,如:533dip)
    
            str=str + "  DisplayMetrics screenWidthDip=" + screenWidthDip + "; screenHeightDip=" + screenHeightDip+"
    ";
    
            screenWidth  = (int)(dm.widthPixels * density + 0.5f);      // 屏幕宽(px,如:480px)
            screenHeight = (int)(dm.heightPixels * density + 0.5f);     // 屏幕高(px,如:800px)
    
            str=str + "  DisplayMetrics screenWidth=" + screenWidth + "; screenHeight=" + screenHeight+"
    ";
            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);
        }
    }

    小到难以看清内容。由此我们采用与分辨率无关的度量单位来开发程序就能够解决这个问题。Android应用开发支持不同的度量单位。

    2、度量单位含义

          dip: device independent pixels(设备独立像素). 不同设备有不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGA、HVGA和QVGA 推荐使用这个,不依赖像素。 

         dp: dip是一样的

         px: pixels(像素). 不同设备显示效果相同,一般我们HVGA代表320x480像素,这个用的比较多。

         pt: point,是一个标准的长度单位,1pt=1/72英寸,用于印刷业,非常简单易用;
         sp: scaled pixels(放大像素). 主要用于字体显示best for textsize。

        in(英寸):长度单位。 
        mm(毫米):长度单位。

  • 相关阅读:
    LeetCode 230. Kth Smallest Element in a BST
    LeetCode 114. Flatten Binary Tree to Linked List
    LeetCode 222. Count Complete Tree Nodes
    LeetCode 129. Sum Root to Leaf Numbers
    LeetCode 113. Path Sum II
    LeetCode 257. Binary Tree Paths
    Java Convert String & Int
    Java Annotations
    LeetCode 236. Lowest Common Ancestor of a Binary Tree
    LeetCode 235. Lowest Common Ancestor of a Binary Search Tree
  • 原文地址:https://www.cnblogs.com/gisoracle/p/5262183.html
Copyright © 2011-2022 走看看