疑惑了好久的问题,今天又遇到了。

由于没心情去读英语,所以尝试网上搜了一下,结果发现两种不同的说法:

http://blog.csdn.net/xiahao86/article/details/16823553

http://www.eoeandroid.com/thread-322627-1-1.html

一个是说getDimensionPixelSize对px是否乘以密度的问题,一个是说算出float进位的问题

所以打算自己验证一下

屏幕800x480,密度1.5

15dp 15sp 15px

结果如下:

15dp:  getDimension=22.5    getDimensionPixelOffset=22  getDimensionPixelSize=23

15sp:  getDimension=22.5    getDimensionPixelOffset=22  getDimensionPixelSize=23

15px:  getDimension=15.0   getDimensionPixelOffset=15 getDimensionPixelSize=15

可以看出getDimensionPixelSize还是能识别传入的是dp还是px的,

区别主要是结果进位的问题:

getDimensionPixelOffset是截断

getDimensionPixelSize是四舍五入



附:sdk原文

getDimension:

float android.content.res.Resources.getDimension(int id) throws NotFoundExceptionRetrieve a dimensional for a particular resource ID. Unit conversions are based on the current DisplayMetrics associated with the resources.Parameters:id The desired resource identifier, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier.Returns:Resource dimension value multiplied by the appropriate metric.Throws:NotFoundException - Throws NotFoundException if the given ID does not exist.


   

getDimensionPixelOffset:

int android.content.res.Resources.getDimensionPixelOffset(int id) throws NotFoundExceptionRetrieve a dimensional for a particular resource ID for use as an offset in raw pixels. This is the same as getDimension, except the returned value is converted to integer pixels for you. An offset conversion involves simply truncating the base value to an integer.Parameters:id The desired resource identifier, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier.Returns:Resource dimension value multiplied by the appropriate metric and truncated to integer pixels.Throws:NotFoundException - Throws NotFoundException if the given ID does not exist.
getDimensionPixelSize:

 int android.content.res.Resources.getDimensionPixelSize(int id) throws NotFoundExceptionRetrieve a dimensional for a particular resource ID for use as a size in raw pixels. This is the same as getDimension, except the returned value is converted to integer pixels for use as a size. A size conversion involves rounding the base value, and ensuring that a non-zero base value is at least one pixel in size.Parameters:id The desired resource identifier, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier.Returns:Resource dimension value multiplied by the appropriate metric and truncated to integer pixels.Throws:NotFoundException - Throws NotFoundException if the given ID does not exist.

这里有提到getDimensionPixelSize会至少保留1像素,这个要注意一下


更多相关文章

  1. Android(安卓)碎片化 适配问题
  2. Android系统密度的修改
  3. 关于Android的屏幕适配,看这篇就够了
  4. Android中尺寸单位杂谈
  5. [WebView学习之二]:使用Web Apps 支持不同分辨率屏
  6. 屏幕适配备忘录
  7. Android中的drawable像素密度适配详解
  8. android 对dp单位的理解
  9. Android(安卓)开发 res里面的drawable(ldpi、mdpi、hdpi、xhdpi

随机推荐

  1. android http-post方法简单实现
  2. android字符串 优化(一)
  3. Android动态关闭mainfest中的receiver
  4. android 图片处理
  5. DPCM.txt
  6. Android中Message参数传递
  7. Android(安卓)后台线程调用前台线程的几
  8. Android——实现在纯Java代码里写布局
  9. android cts and junit
  10. Android(安卓)kernel printk打印级别