Android 尺寸单位转换和屏幕适配相关

各种尺寸单位的意义

  
  dp: Density-independent Pixels
  一个抽象的单元,基于屏幕的物理密度。
  (dp和dip的意义相同,所以不用区别对待)。
  这些单元是相对于160dpi(dots per inch)的屏幕说的,在160dpi的屏幕上,1dp粗略地等于1px。
 
  当运行在更高密度的屏幕上的时候,要绘制1dp的像素数量会放大一个比例,这个比例就是和屏幕密度(dpi)相关。
  类似的,在一个低密度的屏幕上,像素数目会缩小一个比例。
 
  dp到px的这个比例将会随着屏幕的密度变化,而不是直接的比例关系。
  用dp单位,而不是px,是一种简单的屏幕密度适配解决方式。
  换句话说,它提供了一种方式,可以在多种设备上维持真实尺寸一致性。
 
  sp:Scale-independent Pixels
  这个有点像dp单位,但是它也根据用户的字体设置(font preference)缩放尺寸。
  建议用这种尺寸单位来标注字体尺寸,这样它们将会因为屏幕密度和用户设定而调整。
 
  pt
  Points 1/72 inch(英寸),根据屏幕的物理尺寸。
 
  px: Pixels
  相应于真实的像素。
  这种单位不被建议,因为真实的表达会根据设备的不同相差很远。
  每个设备上每英寸的像素数不同(密度不同),并且屏幕上总的像素数也不同(整体大小不同)。
 
  mm:Millimeters
 

资源类型

  图片文件通常会分多个文件夹保存,这多个文件夹的后缀名其实表示的是不同的屏幕密度。
  以m为基准,屏幕密度(dots per inch)基准和需要图像资源的大小比例如下
  l: low density (120dpi) 0.75
  m: medium density (160dpi) 1.0 baseline
  h: high density (240dpi) 1.5
  x: extra-high density (320dpi) 2.0
      xx: extra-extra-high density (480dpi)
 

尺寸单位转换 工具类

  可以写工具类对尺寸单位进行转换,比如:

package com.mengdd.dimen;

import android.content.Context;

public class DimenUtils {

    public static int sp2px(Context context, float spValue) {
float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (spValue * fontScale + 0.5f);
} public static int px2sp(Context context, float pxValue) {
float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (pxValue / fontScale + 0.5f);
} public static int dip2px(Context context, int 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.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
}

  Android中的DisplayMetrics这个类描述了关于显示的各种信息,可以利用它查看设备的状态,上述关于屏幕密度的标准的常量也是从这个类中看到的。

  DisplayMetrics的toString()方法如下:

    @Override
public String toString() {
return "DisplayMetrics{density=" + density + ", width=" + widthPixels +
", height=" + heightPixels + ", scaledDensity=" + scaledDensity +
", xdpi=" + xdpi + ", ydpi=" + ydpi + "}";
}

  其中各个变量解释如下:

    /**
* The absolute width of the display in pixels.
*/
public int widthPixels;
/**
* The absolute height of the display in pixels.
*/
public int heightPixels;
/**
* The logical density of the display. This is a scaling factor for the
* Density Independent Pixel unit, where one DIP is one pixel on an
* approximately 160 dpi screen (for example a 240x320, 1.5"x2" screen),
* providing the baseline of the system's display. Thus on a 160dpi screen
* this density value will be 1; on a 120 dpi screen it would be .75; etc.
*
* <p>This value does not exactly follow the real screen size (as given by
* {@link #xdpi} and {@link #ydpi}, but rather is used to scale the size of
* the overall UI in steps based on gross changes in the display dpi. For
* example, a 240x320 screen will have a density of 1 even if its width is
* 1.8", 1.3", etc. However, if the screen resolution is increased to
* 320x480 but the screen size remained 1.5"x2" then the density would be
* increased (probably to 1.5).
*
* @see #DENSITY_DEFAULT
*/
public float density;
/**
* The screen density expressed as dots-per-inch. May be either
* {@link #DENSITY_LOW}, {@link #DENSITY_MEDIUM}, or {@link #DENSITY_HIGH}.
*/
public int densityDpi;
/**
* A scaling factor for fonts displayed on the display. This is the same
* as {@link #density}, except that it may be adjusted in smaller
* increments at runtime based on a user preference for the font size.
*/
public float scaledDensity;
/**
* The exact physical pixels per inch of the screen in the X dimension.
*/
public float xdpi;
/**
* The exact physical pixels per inch of the screen in the Y dimension.
*/
public float ydpi;

实际设备参数 

  小米2SDisplayMetrics中的toString()方法输出如下:

DisplayMetrics{density=2.0, width=720, height=1280, scaledDensity=2.0, xdpi=345.0566, ydpi=342.23157}

参考资料

  官网文档:
 
 
  本博客之前的讨论见:
 
  其他博客:
 
 
 

Android 尺寸单位转换和屏幕适配相关的更多相关文章

  1. Android 浅谈 设计与屏幕适配 【1.6235449734285716】

    extends: http://www.ui.cn/detail/45435.html http://www.2cto.com/kf/201501/372699.html http://www.cnb ...

  2. Android屏幕尺寸单位转换

    最近在看Android群英传这本书,书中有一节涉及到了,屏幕尺寸与单位.觉得以后可能会用到,做个笔记. PPI(pixels per inch) ,又称为DPI,它是由对角线的像素点数除以屏幕的大小得 ...

  3. [转]android – 多屏幕适配相关

    1.基本概念 屏幕大小(screen size) – 屏幕的实际大小,用屏幕对角线长度来衡量(比如3.4寸,3.8寸).android把屏幕分为以下4种:small,normal,large,extr ...

  4. android技巧(三)屏幕适配

    屏幕适配策略: 1.控件使用wrap_content.match_parent控制某些视图组件的宽度和高度,而不是硬编码的尺寸. “wrap_content”系统就会将视图的宽度或高度设置成所需的最小 ...

  5. Android尺寸单位

    px:pixels(像素),1px的长度对应屏幕一个像素点的大小. dp/dip:(density-independent pixels,设备无关像素) sp:scaled pixels(可缩放像素) ...

  6. iOS屏幕适配-iOS笔记

    学习目标 1.[了解]屏幕适配的发展史 2.[了解]autoResizing基本用法 3.[掌握]autoLayout 的基本用法 4.[掌握]autoLayout代码实现 5.[理解]sizeCla ...

  7. Android屏幕适配全攻略(最权威的官方适配指导)屏幕尺寸 屏幕分辨率 屏幕像素密度 dpdipdpisppx mdpihdpixdpixxdpi

    Android屏幕适配全攻略(最权威的官方适配指导)原创赵凯强 发布于2015-05-19 11:34:17 阅读数 153734 收藏展开 转载请注明出处:http://blog.csdn.net/ ...

  8. android屏幕适配之度量单位、屏幕分类、图标尺寸归类分析

    好久没有做android项目UI的适配了,好多基本概念都已经模糊了,于是萌生了将屏幕分辨率.常用单位.常用图标尺寸等信息规整的想法,一下就是通过查询资料,自己验证的一些随笔,如有失误之处,望大家及时予 ...

  9. 【Android 应用开发】Android屏幕适配解析 - 详解像素,设备独立像素,归一化密度,精确密度及各种资源对应的尺寸密度分辨率适配问题

    . 作者 :万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/19698511 . 最近遇到了一系列的屏幕适配问题, 以及 ...

随机推荐

  1. Windows Azure Virtual Network (7) 设置Azure Virtual Machine固定公网IP (Virtual IP Address, VIP) (2)

    <Windows Azure Platform 系列文章目录> 本文介绍的是,当用户在创建Azure Virtual Machine的时候,忘记绑定公网IP,需要重新绑定公网IP的具体操作 ...

  2. WinForm中DataGridView显示更新数据--人性版

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  3. LINQ多个操作嵌套实现

    获取集合,需要使用多个条件Where,排序OrderBy,查询Select一起. 先来分步实现: source code: string[] stringArray = { "hgdgh&q ...

  4. mysql 批量插入数据

    MySQL使用INSERT插入多条记录,应该如何操作呢?下面就为您详细介绍MySQL使用INSERT插入多条记录的实现方法,供您参考. 看到这个标题也许大家会问,这有什么好说的,调用多次INSERT语 ...

  5. 360手机卫士会影响Widget的运行

    最近开发了一个Widget,老是运行时间长了就会出现NullPointerException错误,一直不知道是什么原因造成的,后来把Widget加入360一键清理的保护名单,错误就少很多,但是仍然有错 ...

  6. 使用QRCode简单生成二维码

    // //  ViewController.m //  二维码 // //  Created by 123 on 16/9/4. //  Copyright © 2016年 彭洪. All right ...

  7. servlet基本原理(手动创建动态资源+工具开发动态资源)

    一.手动开发动态资源 1 静态资源和动态资源的区别 静态资源: 当用户多次访问这个资源,资源的源代码永远不会改变的资源. 动态资源:当用户多次访问这个资源,资源的源代码可能会发送改变. <scr ...

  8. ahjesus code simith 存储过程模板

    <%------------------------------------------------------------------------------------------ * Au ...

  9. Guava学习笔记:Range

    在Guava中新增了一个新的类型Range,从名字就可以了解到,这个是和区间有关的数据结构.从Google官方文档可以得到定义:Range定义了连续跨度的范围边界,这个连续跨度是一个可以比较的类型(C ...

  10. winform 固定splitContainer某一部分大小

    处于布局省事考虑,通常会用splitcontainer进行总体的布局,例如: 默认情况下,splitcontainer在运行时会根据上下文自动调整每个panel的大小,但大部分情况下,其实我们希望左边 ...