getDimension、getDimensionPixelOffset、getDimensionPixelSize初级篇

————字数统计 1.1k 字 | 阅读时长 5 分钟————

序言

在Android应用开发的过程中,我们肯定会遇到一个问题 - 适配 ,这是一个非常头疼的问题,因为你面对的是各式各样的、千奇百怪的屏幕尺寸和分辨率,所以你不可能适配所有机型,只能适配主流的机型及分辨率,所以我们会配置不同分辨率的values(如:values-sw600dp,values-sw720dp),在values下面会有这样一个文件:dimens.xml,我们一般会在里面定义一些视图的尺寸值,可以是不同的单位,如:dp、dip、sp、pt、px、mm、in ,那么我们怎么读取这些值得?

官方API

google给我们提供了三个api使用:getDimension,getDimensionPixelOffset和getDimensionPixelSize,你都使用过吗?它们有什么不同?有什么使用场景?接下来就带大家简单了解一下这三个api的不同。首先得还是先看一下官方文档,这才是最权威的不是嘛。

getDimension

包 : android.content.res.Resources

  • Added in API level 1
  • float getDimension (int id)
  • Retrieve a dimensional for a particular resource ID. Unit conversions are based on the current DisplayMetrics associated with the resources.
Parameters description
id int: 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 description
float Resource dimension value multiplied by the appropriate metric.
Throws description
Resources.NotFoundException Throws NotFoundException if the given ID does not exist.
  • See also:
    • getDimensionPixelOffset(int)
    • getDimensionPixelSize(int)

getDimensionPixelOffset

  • Added in API level 1
  • int getDimensionPixelOffset (int id)
  • Retrieve a dimensional for a particular resource ID for use as an offset in raw pixels. This is the same as getDimension(int), except the returned value is converted to integer pixels for you. An offset conversion involves simply truncating the base value to an integer.
Parameters description
id int: 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 description
int Resource dimension value multiplied by the appropriate metric and truncated to integer pixels.
Throws description
Resources.NotFoundException Throws NotFoundException if the given ID does not exist.
  • See also:
    • getDimension(int)
    • getDimensionPixelSize(int)

getDimensionPixelSize

  • Added in API level 1
  • int getDimensionPixelSize (int id)
  • Retrieve a dimensional for a particular resource ID for use as a size in raw pixels. This is the same as getDimension(int), 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 description
id int: 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 description
int Resource dimension value multiplied by the appropriate metric and truncated to integer pixels.
Throws description
Resources.NotFoundException Throws NotFoundException if the given ID does not exist.
  • See also:
    • getDimension(int)
    • getDimensionPixelOffset(int)

解析

下面通过代码来看看具体的使用:
dimens.xml

<dimen name="custom_margin">16dp</dimen>
<dimen name="custom_textsize">16sp</dimen>
<dimen name="test_margin">3.5dp</dimen>
<dimen name="test_textsize">3.5sp</dimen>

调用:

// dp - 16
float dp1 = getResources().getDimension(R.dimen.custom_margin);    
int dp2 = getResources().getDimensionPixelOffset(R.dimen.custom_margin);    
int dp3 = getResources().getDimensionPixelSize(R.dimen.custom_margin); 

// dp - 3.5
float dp4 = getResources().getDimension(R.dimen.test_margin);    
int dp5 = getResources().getDimensionPixelOffset(R.dimen.test_margin);    
int dp6 = getResources().getDimensionPixelSize(R.dimen.test_margin);    

// sp - 16   
float sp1 = getResources().getDimension(R.dimen.custom_textsize);    
int sp2 = getResources().getDimensionPixelOffset(R.dimen.custom_textsize);    
int sp3 = getResources().getDimensionPixelSize(R.dimen.custom_textsize);

// sp - 3.5
float sp4 = getResources().getDimension(R.dimen.custom_textsize);    
int sp5 = getResources().getDimensionPixelOffset(R.dimen.custom_textsize);    
int sp6 = getResources().getDimensionPixelSize(R.dimen.custom_textsize);

String log = "getDimension(16dp) = " + dp1 + "\n"  
            + "getDimensionPixelOffset(16dp) = " + dp2 + "\n"  
            + "getDimensionPixelSize(16dp) = " + dp3 + "\n" + "\n"  
            + "getDimension(dp3.5) = " + dp4 + "\n"  
            + "getDimensionPixelOffset(dp3.5) = " + dp5 + "\n"  
            + "getDimensionPixelSize(dp3.5) = " + dp6 + "\n" + "\n" 
            + "getDimension(sp16) = " + sp1 + "\n"  
            + "getDimensionPixelOffset(sp16) = " + sp2 + "\n"  
            + "getDimensionPixelSize(sp16) = " + sp3 + "\n" + "\n" 
            + "getDimension(sp3.5) = " + sp4 + "\n"  
            + "getDimensionPixelOffset(sp3.5) = " + sp5 + "\n"  
            + "getDimensionPixelSize(sp3.5) = " + sp6;

Log.d("LauncherActivity", log);

输出:

densityDpi:160
scale:1.0
fontScale:1.0

getDimension(16dp) = 16.0
getDimensionPixelOffset(16dp) = 16
getDimensionPixelSize(16dp) = 16

getDimension(3.5dp) = 3.5
getDimensionPixelOffset(3.5dp) = 3 
getDimensionPixelSize(3.5dp) = 4  

getDimension(16sp) = 16.0
getDimensionPixelOffset(16sp) = 16
getDimensionPixelSize(16sp) = 16

getDimension(3.5sp) = 3.5
getDimensionPixelOffset(3.5sp) = 3
getDimensionPixelSize(3.5sp) = 4

densityDpi:240
scale:1.5
fontScale:1.5

getDimension(16dp) = 24.0
getDimensionPixelOffset(16dp) = 24
getDimensionPixelSize(16dp) = 24

getDimension(3.5dp) = 5.25
getDimensionPixelOffset(3.5dp) = 5 
getDimensionPixelSize(3.5dp) = 5  

getDimension(16sp) = 24.0
getDimensionPixelOffset(16sp) = 24
getDimensionPixelSize(16sp) = 24

getDimension(3.5sp) = 5.25
getDimensionPixelOffset(3.5sp) = 5
getDimensionPixelSize(3.5sp) = 5

总结

通过上面的代码就可以说明问题了,我们可以看到以下几点:

  • 三个方法都是将源数据乘以scale
  • getDimension的返回值是float类型,getDimensionPixelOffset和getDimensionPixelSize的返回值是int类型
  • 三个方法返回的都是px值,即将dp或者sp等值转换为px值
  • getDimensionPixelOffset()是直接将小数位舍弃,而getDimensionPixelSize()则会进行四舍五入
  • 像setWidth(int)、setHeight(int)这种都是px,根据自己的情况选择getDimensionPixelSize()或getDimensionPixelOffset()
  • 像setTextSize():一个参数的是sp;两个参数的是unit和px

×

大吉大利,晚上吃鸡

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

文章目录
  1. 1. 序言
  2. 2. 官方API
  3. 3. getDimension
  4. 4. getDimensionPixelOffset
  5. 5. getDimensionPixelSize
  6. 6. 解析
  7. 7. 总结
| | 总字数统计:93.5k