// 获取屏幕密度(方法1)

  1. int screenWidth = getWindowManager().getDefaultDisplay().getWidth(); // 屏幕宽(像素,如:480px)
  2. int screenHeight = getWindowManager().getDefaultDisplay().getHeight(); // 屏幕高(像素,如:800p)
  3. Log.e(TAG + " getDefaultDisplay", "screenWidth=" + screenWidth + "; screenHeight=" + screenHeight);
  4. // 获取屏幕密度(方法2)
  5. DisplayMetrics dm = new DisplayMetrics();
  6. dm = getResources().getDisplayMetrics();
  7. float density = dm.density; // 屏幕密度(像素比例:0.75/1.0/1.5/2.0)
  8. int densityDPI = dm.densityDpi; // 屏幕密度(每寸像素:120/160/240/320)
  9. float xdpi = dm.xdpi;
  10. float ydpi = dm.ydpi;
  11. Log.e(TAG + " DisplayMetrics", "xdpi=" + xdpi + "; ydpi=" + ydpi);
  12. Log.e(TAG + " DisplayMetrics", "density=" + density + "; densityDPI=" + densityDPI);
  13. screenWidth = dm.widthPixels; // 屏幕宽(像素,如:480px)
  14. screenHeight = dm.heightPixels; // 屏幕高(像素,如:800px)
  15. Log.e(TAG + " DisplayMetrics(111)", "screenWidth=" + screenWidth + "; screenHeight=" + screenHeight);
  16. // 获取屏幕密度(方法3)
  17. dm = new DisplayMetrics();
  18. getWindowManager().getDefaultDisplay().getMetrics(dm);
  19. density = dm.density; // 屏幕密度(像素比例:0.75/1.0/1.5/2.0)
  20. densityDPI = dm.densityDpi; // 屏幕密度(每寸像素:120/160/240/320)
  21. xdpi = dm.xdpi;
  22. ydpi = dm.ydpi;
  23. Log.e(TAG + " DisplayMetrics", "xdpi=" + xdpi + "; ydpi=" + ydpi);
  24. Log.e(TAG + " DisplayMetrics", "density=" + density + "; densityDPI=" + densityDPI);
  25. int screenWidthDip = dm.widthPixels; // 屏幕宽(dip,如:320dip)
  26. int screenHeightDip = dm.heightPixels; // 屏幕宽(dip,如:533dip)
  27. Log.e(TAG + " DisplayMetrics(222)", "screenWidthDip=" + screenWidthDip + "; screenHeightDip=" + screenHeightDip);
  28. screenWidth = (int)(dm.widthPixels * density + 0.5f); // 屏幕宽(px,如:480px)
  29. screenHeight = (int)(dm.heightPixels * density + 0.5f); // 屏幕高(px,如:800px)
  30. Log.e(TAG + " DisplayMetrics(222)", "screenWidth=" + screenWidth + "; screenHeight=" + screenHeight);

Android系统中,默认的单位是像素(px)。也就是说,在没有明确说明的情况下,所有的大小设置都是以像素为单位。

如果以像素设置大小,会导致不同分辨率下出现不同的效果。那么,如何将应用中所有大小的单位都设置为’dp’呢?
实际上TextView.setTextSize()重载了根据单位设置大小的方法。

笔者在此基础上实现了以下方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
 * 获取当前分辨率下指定单位对应的像素大小(根据设备信息)
 * px,dip,sp -> px
 * 
 * Paint.setTextSize()单位为px
 * 
 * 代码摘自:TextView.setTextSize()
 * 
 * @param unit  TypedValue.COMPLEX_UNIT_*
 * @param size
 * @return
 */
public float getRawSize(int unit, float size) {
       Context c = getContext();
       Resources r;

if (c == null)
           r = Resources.getSystem();
       else
           r = c.getResources();
        
       return TypedValue.applyDimension(unit, size, r.getDisplayMetrics());
}

下面是网友提供的方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
* 根据手机的分辨率从 dp 的单位 转成为 px(像素)
*/
public static int dip2px(Context context, float dpValue) {
  final float scale = context.getResources().getDisplayMetrics().density;
  return (int) (dpValue * scale + 0.5f);
}

/**
* 根据手机的分辨率从 px(像素) 的单位 转成为 dp
*/
public static int px2dip(Context context, float pxValue) {
  final float scale = context.getResources().getDisplayMetrics().density;
  return (int) (pxValue / scale + 0.5f);
}

转载注明本文地址: http://orgcent.com/android-dpsppx-unit-conversion/

本文固定链接: http://orgcent.com/android-dpsppx-unit-conversion/ | 萝卜白菜的博客

转载于:https://www.cnblogs.com/zjqlogs/archive/2012/04/12/2444620.html

Android根据分辨率进行单位转换-(dp,sp转像素px)相关推荐

  1. android中的尺寸单位是什么,Android中各种长度尺寸单位(dp,dip,px,sp,pt)的区别

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

  2. android 尺寸转换 dp sp in mm pt px转换为int

    在android开发中,在自定义view中有些函数只接受int参数,而我们经常使用的尺寸单位是dp.sp这些,那如何将它们转换为int型呢? int spToInt=TypedValue.applyD ...

  3. android中的长度单位介绍(dp,sp,px,in,pt,mm)

    作为小白学习了一段时间的android,在学习过程中却一直有个疑问,在设置控件大小时面对这几个单位却不知如何选择,查找相关资料后了解了其含义,今天来分享下 dp或dip(device independ ...

  4. 科普dip,dp,sp,dpi,ppi,px,Density

    一.概念 DPI:DPI是Dots Per Inch的缩写,也就是"每英寸点数",打印机分辨率.这里所说的"点",在屏幕上并不是不变的,并不是唯一对应屏幕上的像 ...

  5. Android中常见的单位ppi,dp,dpi,sp,px

    在android 开发过程中,我们使用的单位比较少,一般情况下在描述字体大小的时候我们通常用sp,而在设置间距的时候我们用dp,除此之外很少再用到其他单位,而且很多时候我们用着用着就习惯了,也不去探究 ...

  6. android 单位pt,Android APPUI设计师必知:pt sp dp之间的关系

    我们都知道APP UI设计师常用的工具是Ps或者Ai,但是里面都没有sp的单位的.我们先来普及下一些关于Android尺寸单位或者是分辨率一些知识点: px: pixels(像素). 不同设备显示效果 ...

  7. Android的单位px,pt,dp,sp总结和获取屏幕分辨率

    px: pixels(像素). 是屏幕中可以显示的最小单位元素,px是设备密度有关的,不同设备显示效果不同.因为由于不同的设备密度不同,密度就是单位英寸的像素不同,在不同的手机上显示的大小就不同 pt ...

  8. Android原生系统API自带dp、px、sp单位转换

    Android系统中自带的Api中可以使用TypedValue进行单位转换 1,调用系统api转换单位 // 获得转换后的px值 float pxDimension = TypedValue.appl ...

  9. android 字体像素转换工具类_android px,dp,sp大小转换工具

    package com.voole.playerlib.util; import android.content.Context; /** * Android大小单位转换工具类 * * float s ...

  10. Android 开发 -- 开发第一个安卓程序、Android UI开发(布局的创建:相对布局和线性布局、控件单位:px pt dp sp、常用控件 、常见对话框、ListView)

    文章目录 1. 开发第一个Hello World程序 1.1 开发程序 1.2 认识程序中的文件 1.3 Android程序结构 1.4 安卓程序打包 2. Android UI开发 2.1 布局的创 ...

最新文章

  1. 第十六届全国大学生智能车东北赛区线上比赛时间与直播信息
  2. Android网络优化之HttpClient
  3. 中国教育电视台的iEnglish英语风采秀 为我们揭示了“双减”的本质目标
  4. python单元测试框架unittest介绍和使用_Python+Selenium框架设计篇之-简单介绍unittest单元测试框架...
  5. 向.NET Core项目添加EntityFrameworkCore支持
  6. Maven学习总结(11)——Maven Tomcat7自动部署
  7. 学习Javascript闭包(Closure)(转载+理解心得)
  8. iphone:使用NSFileManager取得目录下所有文件(遍历所有文件)
  9. odoo 对 many2many one2many的操作
  10. 良好的XHTML编写习惯
  11. dev项目属性按钮是灰色_【网页特效】11 个文本输入和 6 个按钮操作 特效库
  12. Linux驱动开发: USB驱动开发
  13. Origin 2019b 64Bit 软件绘制出图的坐标刻度老是消失怎么解决
  14. java压缩图片大小_java压缩图片、等比例压缩图片
  15. Visual Studio C++/C 游戏——双人贪吃蛇 实现方法+代码讲解+算法优化+成功展示+源代码+总结(超详细,适合小白)
  16. 通过IMAP方式迁移U-Mail邮件到Exchange 2013之2008R2 AD搭建!
  17. 记一次oracle通过dblink连接mysql实施
  18. Weex 修改安卓生成apk默认的启动页面
  19. Alpine安装与使用
  20. Freshman的low游戏神犇勿喷

热门文章

  1. 欧氏距离、巴氏距离、马氏距离的区别是什么
  2. 亲测有效!实现Chrome浏览器下载速度提升3倍!
  3. Ubuntu 16.04 安装后网络、ssh禁止root、上传设置
  4. UITableView的复用过程
  5. nodejs基础 -- 全局对象
  6. 《3D数学基础》1.9 向量空间
  7. SWT/JFace常用组件----容器类
  8. 在OLT上查看SLAN
  9. 【调查 】DBA的压力究竟有多大?
  10. undefined reference to `create_module''